-
Notifications
You must be signed in to change notification settings - Fork 4
/
postprocessing.py
1221 lines (1130 loc) · 59 KB
/
postprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from nltk.corpus import wordnet
import numpy as np
# from nltk import pos_tag
import nltk
import time
import datetime
import pickle
import parameters as param
import os
import gensim
import sklearn.metrics.pairwise
import sklearn
import utils
import testing
from scipy.stats import wasserstein_distance
import dataset_path
from ortools.graph import pywrapgraph
# sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer
# from stanfordcorenlp import StanfordCoreNLP
# from nltk.tag.stanford import NERTagger
# It converst general summary to specific summary, giving the specific article and general summary
class PostProcessing:
def __init__(self):
start_time = time.time()
mode = 'lg'
if mode == 'neg':
similarity_methods = {0: 'freq', 1: 'tfidf', 2: 'w2v', 3: 'edit', 4: 'jacard', 5: 'avg_w2v', 6: 'mix'}
center_word = {0: False, 1: True}
self.neg_postprocessing(input_general_summary_file_path='path/to/generalized_system_summaries',
input_general_article_file_path='path/to/generalized_articles',
input_specific_article_file_path='path/to/articles',
# input_golden_summary_file_path='docs/golden_sum',
output_summary_file_path='path/to/output_summaries',
word_embedding_file_path='path/to/word embeddings',
sum_window=3, art_window=5,
similarity_method=similarity_methods[6],
art_center_word=center_word[0],
sum_center_word=center_word[0],
# testing_mode='rouge_of_individual_files'
)
elif mode == 'lg':
similarity_methods = {0: 'freq', 1: 'tfidf', 2: 'w2v', 3: 'edit', 4: 'jacard', 5: 'avg_w2v', 6: 'mix'}
center_word = {0: False, 1: True}
self.lg_postprocessing(input_general_summary_file_path='path/to/generalized_system_summaries',
input_general_article_file_path='path/to/generalized_articles',
input_specific_article_file_path='path/to/articles',
# input_golden_summary_file_path='path/to/golden_sum',
output_summary_file_path='path/to/output_summaries',
word_embedding_file_path='path/to/word embeddings',
hypernyms_dict_file_path='path/to/hypernym_paths_pickle_file',
sum_window=3, art_window=5,
similarity_method=similarity_methods[6],
art_center_word=center_word[0],
sum_center_word=center_word[0],
# testing_mode='rouge_of_individual_files'
)
dt = time.time() - start_time
print('Time: {}sec'.format(dt))
print('Time: {}\n\nprocess finished'.format(datetime.timedelta(seconds=dt)))
def neg_postprocessing(self, input_general_summary_file_path,
input_general_article_file_path,
input_specific_article_file_path,
# input_golden_summary_file_path,
output_summary_file_path,
word_embedding_file_path,
sum_window, art_window,
similarity_method='w2v',
art_center_word=False, sum_center_word=False,
# testing_mode='rouge_of_individual_files'
):
t0 = time.time()
general_summary_per_line_list = []
general_article_per_line_list = []
specific_article_per_line_list = []
# specific_text_line_list = []
# specific_text_list = []
output_summary_text = ''
general_summary_text = ''
# specific_summary_text = ''
word2vec = None
if similarity_method == 'w2v' or similarity_method == 'avg_w2v' or similarity_method == 'mix':
word2vec = gensim.models.KeyedVectors.load_word2vec_format(word_embedding_file_path, binary=False)
stopword_list = nltk.corpus.stopwords.words('english')
stemmer = nltk.stem.snowball.EnglishStemmer()
# stemmer = nltk.stem.LancasterStemmer()
lemmatizer = nltk.stem.WordNetLemmatizer()
# vectorizer = sklearn.feature_extraction.text.CountVectorizer()
with open(input_general_summary_file_path, 'r', encoding='utf8') as f:
for line in f:
general_summary_per_line_list.append(line.split())
general_summary_text += line
with open(input_general_article_file_path, 'r', encoding='utf8') as f:
for line in f:
general_article_per_line_list.append(line.split())
with open(input_specific_article_file_path, 'r', encoding='utf8') as f:
for line in f:
specific_article_per_line_list.append(line.split())
# general_summary_text += line
print('Files have been read')
# article_word_name_entity_list = self.word_ner_list(
# input_specific_article_file_path=input_specific_article_file_path,
# stopword_list=stopword_list, lemmatizer=lemmatizer,
# print_per_line=100000)
stanford_ner_tags = ['PERSON_', 'LOCATION_', 'ORGANIZATION_']
wordnet_ner_tags = ['person_', 'location_', 'organization_']
ner_tags = stanford_ner_tags + wordnet_ner_tags
# count_gen_words = 0
output_summary_file = open(output_summary_file_path, 'w', encoding='utf8')
line_index = 0
for summary_line_word_list, general_article_line_word_list, specific_article_line_word_list in \
zip(general_summary_per_line_list, general_article_per_line_list, specific_article_per_line_list):
line_index += 1
print_flag = False
if line_index < 10:
print_flag = True
summary_line_word_list_ = [w for w in summary_line_word_list]
generalized_consepts = []
candidates_for_replacement = []
sum_index = -1
for token_s in summary_line_word_list:
sum_index += 1
if token_s in ner_tags:
generalized_consepts.append(sum_index)
art_index = -1
for token_a in general_article_line_word_list:
art_index += 1
if token_a == token_s:
similarity = self.similarity_of_text_v2(word2vec, lemmatizer, stemmer,
art_center_word, sum_center_word,
summary_line_word_list_,
specific_article_line_word_list,
sum_window, art_window,
art_index, sum_index,
stopword_list,
similarity_method=similarity_method,
decay_factor=0.2)
candidates_for_replacement.append((sum_index, art_index, similarity))
candidates_for_replacement = utils.sort_by_third(candidates_for_replacement, descending=True)
for (sum_index, art_index, similarity) in candidates_for_replacement:
if sum_index in generalized_consepts:
summary_line_word_list_[sum_index] = specific_article_line_word_list[art_index]
generalized_consepts.remove(sum_index)
#############
if print_flag:
print(general_article_line_word_list)
print(specific_article_line_word_list)
print(summary_line_word_list)
print(summary_line_word_list_)
# print(' '.join([w.replace('_', ' ') for w in summary_line_word_list_]))
output_summary_line_text = ''
for w in summary_line_word_list_:
if w not in ner_tags:
output_summary_line_text += w.replace('_', ' ') + ' '
else:
output_summary_line_text += w + ' '
if print_flag:
print(output_summary_line_text)
print()
output_summary_file.write(output_summary_line_text.strip() + '\n')
output_summary_text += output_summary_line_text + '\n'
output_summary_file.close()
print('Output file have been written.')
# t1 = time.time()
# with open(input_golden_summary_file_path, 'r', encoding='utf8') as f:
# specific_summary_text = f.read()
# cos_sim_of_initial_file = self.cos_similarity_based_on_tfidf(specific_summary_text, general_summary_text)[0][0]
# cos_sim_of_spec_and_output_file = \
# self.cos_similarity_based_on_tfidf(specific_summary_text, output_summary_text)[0][0]
# cos_sim_of_output_output_file = \
# self.cos_similarity_based_on_tfidf(output_summary_text, output_summary_text)[0][0]
# testing.Testing(testing_mode=testing_mode)
# print('\ncosine similarities:\n')
# print('\tcos_sim between golden and general summary: ', cos_sim_of_initial_file)
# print('\tcos_sim between golde and output summary: ', cos_sim_of_spec_and_output_file)
# print('\tcos_sim between the same files (checking): ', cos_sim_of_output_output_file)
# t2 = time.time()
# print('Time for converting, testing & overall: {}, {} & {}'.format(t1 - t0, t2 - t1, t2 - t0))
print('Time: {}'.format(time.time() - t0))
def lg_postprocessing(self, input_general_summary_file_path,
input_general_article_file_path,
input_specific_article_file_path,
# input_golden_summary_file_path,
output_summary_file_path,
word_embedding_file_path,
hypernyms_dict_file_path,
sum_window, art_window,
similarity_method='w2v',
art_center_word=False, sum_center_word=False,
# testing_mode='rouge_of_individual_files'
):
t0 = time.time()
general_summary_per_line_list = []
general_article_per_line_list = []
specific_article_per_line_list = []
# specific_text_line_list = []
# specific_text_list = []
output_summary_text = ''
general_summary_text = ''
# specific_summary_text = ''
word2vec = None
if similarity_method == 'w2v' or similarity_method == 'avg_w2v' or similarity_method == 'mix':
word2vec = gensim.models.KeyedVectors.load_word2vec_format(word_embedding_file_path, binary=False)
stopword_list = nltk.corpus.stopwords.words('english')
stemmer = nltk.stem.snowball.EnglishStemmer()
# stemmer = nltk.stem.LancasterStemmer()
lemmatizer = nltk.stem.WordNetLemmatizer()
# vectorizer = sklearn.feature_extraction.text.CountVectorizer()
with open(input_general_summary_file_path, 'r', encoding='utf8') as f:
for line in f:
general_summary_per_line_list.append(line.split())
general_summary_text += line
with open(input_general_article_file_path, 'r', encoding='utf8') as f:
for line in f:
general_article_per_line_list.append(line.split())
with open(input_specific_article_file_path, 'r', encoding='utf8') as f:
for line in f:
specific_article_per_line_list.append(line.split())
hypernyms_depth_dict = utils.read_pickle_file(hypernyms_dict_file_path)
hypernyms_dict = dict()
for key in hypernyms_depth_dict.keys():
hypernym_list = []
for (hyp, depth) in hypernyms_depth_dict[key]:
hypernym_list.append(hyp)
hypernyms_dict[key] = hypernym_list
del hypernyms_depth_dict
# general_summary_text += line
print('Files have been read')
# article_word_name_entity_list = self.word_ner_list(
# input_specific_article_file_path=input_specific_article_file_path,
# stopword_list=stopword_list, lemmatizer=lemmatizer,
# print_per_line=100000)
stanford_ner_tags = ['PERSON_', 'LOCATION_', 'ORGANIZATION_']
wordnet_ner_tags = ['person_', 'location_', 'organization_']
ner_tags = stanford_ner_tags + wordnet_ner_tags
# count_gen_words = 0
output_summary_file = open(output_summary_file_path, 'w', encoding='utf8')
line_index = 0
for summary_line_word_list, general_article_line_word_list, specific_article_line_word_list in \
zip(general_summary_per_line_list, general_article_per_line_list, specific_article_per_line_list):
line_index += 1
print_flag = False
if line_index < 10:
print_flag = True
summary_line_word_list_ = [w for w in summary_line_word_list]
generalized_consepts = []
candidates_for_replacement = []
sum_index = -1
for token_s in summary_line_word_list:
sum_index += 1
if token_s.find('_') > -1: # if token_s is generalized
generalized_consepts.append(sum_index)
art_index = -1
for token_a in general_article_line_word_list:
art_index += 1
hypernyms_list = hypernyms_dict.get(token_a, None)
if hypernyms_list and token_s in hypernyms_list:
similarity = self.similarity_of_text_v2(word2vec, lemmatizer, stemmer,
art_center_word, sum_center_word,
summary_line_word_list_,
specific_article_line_word_list,
sum_window, art_window,
art_index, sum_index,
stopword_list,
similarity_method=similarity_method,
decay_factor=0.2)
candidates_for_replacement.append((sum_index, art_index, similarity))
candidates_for_replacement = utils.sort_by_third(candidates_for_replacement, descending=True)
for (sum_index, art_index, similarity) in candidates_for_replacement:
if sum_index in generalized_consepts:
summary_line_word_list_[sum_index] = specific_article_line_word_list[art_index]
generalized_consepts.remove(sum_index)
#############
if print_flag:
print(general_article_line_word_list)
print(specific_article_line_word_list)
print(summary_line_word_list)
print(summary_line_word_list_)
# print(' '.join([w.replace('_', ' ') for w in summary_line_word_list_]))
output_summary_line_text = ''
for w in summary_line_word_list_:
if w not in ner_tags:
output_summary_line_text += w.replace('_', ' ') + ' '
else:
output_summary_line_text += w + ' '
if print_flag:
print(output_summary_line_text)
print()
output_summary_file.write(output_summary_line_text.strip() + '\n')
output_summary_text += output_summary_line_text + '\n'
output_summary_file.close()
print('Output file have been written.')
# t1 = time.time()
# with open(input_golden_summary_file_path, 'r', encoding='utf8') as f:
# specific_summary_text = f.read()
# cos_sim_of_initial_file = self.cos_similarity_based_on_tfidf(specific_summary_text, general_summary_text)[0][0]
# cos_sim_of_spec_and_output_file = \
# self.cos_similarity_based_on_tfidf(specific_summary_text, output_summary_text)[0][0]
# cos_sim_of_output_output_file = \
# self.cos_similarity_based_on_tfidf(output_summary_text, output_summary_text)[0][0]
# testing.Testing(testing_mode=testing_mode)
# print('\ncosine similarities:\n')
# print('\tcos_sim between golden and general summary: ', cos_sim_of_initial_file)
# print('\tcos_sim between golde and output summary: ', cos_sim_of_spec_and_output_file)
# print('\tcos_sim between the same files (checking): ', cos_sim_of_output_output_file)
# t2 = time.time()
# print('Time for converting, testing & overall: {}, {} & {}'.format(t1 - t0, t2 - t1, t2 - t0))
print('Time: {}'.format(time.time() - t0))
def similarity_of_text_v2(self, word2vec, lemmatizer, stemmer,
art_center_word, sum_center_word,
summary_line_word_list, specific_article_line_word_list,
sum_window, art_window,
art_index, sum_index, stopword_list, similarity_method,
decay_factor=0.2):
sum_end_index = len(summary_line_word_list) - 1
art_end_index = len(specific_article_line_word_list) - 1
similarity = 0.0
# pre_similarity = 0.0
for new_left_window in range(1):
# new_right_window = new_left_window
art_left_index = art_index - art_window # new_left_window
art_right_index = art_index + art_window # new_right_window
sum_left_index = sum_index - sum_window # new_left_window
sum_right_index = sum_index + sum_window # new_right_window
if art_left_index < 0:
art_left_index = 0
if art_right_index > art_end_index:
art_right_index = art_end_index
if sum_left_index < 0:
sum_left_index = 0
if sum_right_index > sum_end_index:
sum_right_index = sum_end_index
# art_text = ''
art_text1 = ''
# art_text2 = ''
art_offset_of_center_word = 1
sum_offset_of_center_word = 1
if art_center_word:
art_offset_of_center_word = 0
if sum_center_word:
sum_offset_of_center_word = 0
for w in specific_article_line_word_list[
art_left_index:art_index] + specific_article_line_word_list[
art_index + 1:art_right_index + 1]:
art_text1 += w + ' '
art_central_w = specific_article_line_word_list[art_index]
# art_text = art_text.replace('_', ' ')
art_text1 = art_text1.replace('_', ' ')
# art_text2 = art_text2.replace('_', ' ')
# sum_text = ''
sum_text1 = ''
# sum_text2 = ''
for w in summary_line_word_list[
sum_left_index:sum_index] + summary_line_word_list[
sum_index + 1:sum_right_index + 1]:
sum_text1 += w + ' '
sum_text1 = sum_text1.replace('_', ' ')
temp_txt = ''
for w in art_text1.split():
temp_txt += lemmatizer.lemmatize(w) + ' '
art_text1 = temp_txt
temp_txt = ''
for w in sum_text1.split():
temp_txt += lemmatizer.lemmatize(w) + ' '
sum_text1 = temp_txt
similarity = None
if similarity_method == 'freq':
similarity = self.cos_similarity_based_on_freq(sum_text1, art_text1)[0][0]
elif similarity_method == 'tfidf':
similarity = self.cos_similarity_based_on_tfidf(sum_text1, art_text1)[0][0]
elif similarity_method == 'w2v':
similarity = self.word_mover_distance(word2vec, sum_text1, art_text1)
elif similarity_method == 'edit':
similarity = self.edit_similarity(sum_text1, art_text1)
# Levenshtein edit-distance between two strings
elif similarity_method == 'jacard':
similarity = nltk.jaccard_distance(set(art_text1.split()), set(sum_text1.split()))
elif similarity_method == 'avg_w2v':
similarity = self.cos_sim_of_avg_w2v(word2vec, art_text1, sum_text1,
art_index - art_left_index,
art_right_index - art_index,
sum_index - sum_left_index,
sum_right_index - sum_index,
art_center=False, sum_center=False)
print(similarity)
elif similarity_method == 'mix':
similarity = max(self.cos_similarity_based_on_freq(sum_text1, art_text1)[0][0],
self.cos_similarity_based_on_freq(sum_text1, art_text1 + ' ' + art_central_w)[0][0]) + \
0.1 * max(self.cos_sim_of_avg_w2v(word2vec, art_text1, sum_text1,
art_index - art_left_index,
art_right_index - art_index,
sum_index - sum_left_index,
sum_right_index - sum_index,
art_center=False, sum_center=False),
self.cos_sim_of_avg_w2v(word2vec, art_text1 + ' ' + art_central_w,
sum_text1,
art_index - art_left_index,
art_right_index - art_index,
sum_index - sum_left_index,
sum_right_index - sum_index,
art_center=False, sum_center=False))
return similarity
def cos_sim_of_avg_w2v(self, w2v_model, art_text, sum_text,
art_left_window, art_right_window, sum_left_window, sum_right_window,
art_center, sum_center, decay=0.0):
text1_list = art_text.split()
text2_list = sum_text.split()
w2v_1_list = []
w2v_2_list = []
count_unk = 0
index = -1
right = art_right_window
for w in text1_list:
index += 1
if art_left_window > 0:
weight = (1 - decay) ** art_left_window
art_left_window -= 1
elif art_left_window == 0 and art_center:
art_left_window -= 1
weight = (1 - decay)
else:
weight = (1 - decay) ** (right - art_right_window + 1)
art_right_window -= 1
try:
d = np.multiply(w2v_model.word_vec(w), weight)
w2v_1_list.append(d) # append(d)
except KeyError:
count_unk += 1
_ = None
if count_unk > 1:
print('Edit Sim')
return self.edit_similarity(art_text, sum_text)
index = -1
right = sum_right_window
for w in text2_list:
index += 1
# weight = 1.0
if sum_left_window > 0:
weight = (1 - decay) ** sum_left_window
sum_left_window -= 1
elif sum_left_window == 0 and sum_center:
sum_left_window -= 1
weight = (1 - decay)
else:
weight = (1 - decay) ** (right - sum_right_window + 1)
sum_right_window -= 1
# weight = 1.0 / (abs(c2 - index)*2 + 1)
try:
d = np.multiply(w2v_model.word_vec(w), weight)
w2v_2_list.append(d)
except KeyError:
count_unk += 1
_ = None
if count_unk > 1:
print('Edit Sim')
return self.edit_similarity(art_text, sum_text)
avg_1 = np.average(w2v_1_list, axis=0)
avg_2 = np.average(w2v_2_list, axis=0)
try:
return sklearn.metrics.pairwise.cosine_similarity(avg_1.reshape(1, -1), avg_2.reshape(1, -1))[0][0]
except ValueError:
print('Except', art_text, sum_text)
return self.edit_similarity(art_text, sum_text)
@staticmethod
def edit_similarity(art_text, sum_text):
str_len = max(len(art_text), len(sum_text))
dist = nltk.edit_distance(art_text, sum_text, substitution_cost=1, transpositions=False)
return 1.0 - (dist / str_len)
def word_mover_distance(self, model, text1, text2):
# model = gensim.models.KeyedVectors.load_word2vec_format(param.word2vec_file_path, binary=False)
return model.wmdistance(text1, text2)
@staticmethod
# it returns a list of tuples: (word, pos)
def wordnet_pos_tag(text_list):
pos_tag_list = nltk.tag.pos_tag(text_list)
word_pos_list = []
for (w, pos) in pos_tag_list:
wordnet_pos = pos
if pos.startswith('J'):
wordnet_pos = 'a' # nltk.corpus.wordnet.ADJ
elif pos.startswith('V'):
wordnet_pos = 'v' # nltk.corpus.wordnet.VERB
elif pos.startswith('N'):
wordnet_pos = 'n' # nltk.corpus.wordnet.NOUN
elif pos.startswith('R'):
wordnet_pos = 'r' # nltk.corpus.wordnet.ADV
word_pos_list.append((w, wordnet_pos))
return word_pos_list
def word_ner_list(self, input_specific_article_file_path,
stopword_list, lemmatizer,
print_per_line=100000):
# word_freq_hypernyms_dict = utils.read_pickle_file(input_word_freq_hypernyms_pickle_file_path)
stanford_ner_tags = ['PERSON', 'LOCATION', 'ORGANIZATION']
wordnet_ner_tags = ['person', 'location', 'organization']
ner_tags = stanford_ner_tags + wordnet_ner_tags
# lemmatizer = nltk.stem.WordNetLemmatizer()
specific_word_ner_list = []
specific_word_pos_list = []
text_word_list = []
with open(input_specific_article_file_path, 'r', encoding='utf8') as f:
for line in f:
text_word_list += line.split() + ['_NL_']
# stanford_ner_tagger_dir = "C:/Stanford_NLP_Tools/stanford-ner-2018-10-16/"
stanford_ner_tagger_dir = dataset_path.stanford_nlp_tools + 'stanford-ner-2018-10-16/'
# os.environ['CLASSPATH'] = "/home/pkouris/Stanford_NLP_Tools/stanford-ner-2018-10-16/*"
model = ['english.all.3class.distsim.crf.ser.gz', 'english.all.3class.distsim.crf.ser.gz']
ner = nltk.tag.stanford.StanfordNERTagger(
stanford_ner_tagger_dir + 'classifiers/' + model[0],
stanford_ner_tagger_dir + 'stanford-ner-3.9.2.jar')
specific_word_ner_list = ner.tag(text_word_list)
output_word_name_entity_list = []
output_line_list = []
for (word, ner) in specific_word_ner_list:
flag = True
index_lineIndex = (-1, -1)
if word == '_NL_':
output_word_name_entity_list.append(output_line_list)
output_line_list = []
flag = False
elif word not in stopword_list:
if ner in stanford_ner_tags: # and word not in stopword_list:
output_line_list.append((word, ner.lower()))
flag = False
else:
token_lemma = lemmatizer.lemmatize(word, pos='n')
synset = self.make_synset(token_lemma, category='n')
if synset is not None:
synset.max_depth()
merged_synset_list = self.merge_lists(synset.hypernym_paths())
hypernym_list = []
for synset in merged_synset_list:
hypernym_list.append(self.synset_word(synset))
for hyp in hypernym_list:
if hyp in wordnet_ner_tags: # and word not in stopword_list:
output_line_list.append((word, hyp))
flag = False
break
if flag:
output_line_list.append((word, ''))
new_output_word_name_entity_list = []
for line in output_word_name_entity_list:
index = -1
new_line_list = []
prev_ner = ''
prev_word = ''
for (word, ner) in line:
index += 1
new_word = word
if ner in ner_tags:
if ner == prev_ner:
new_word = prev_word + '_' + word
new_line_list.remove((prev_word, prev_ner))
# print('ner == prev_ner ', new_word)
prev_ner = ner
prev_word = new_word
new_line_list.append((new_word, ner))
# print(new_line_list)
new_output_word_name_entity_list.append(new_line_list)
#############################
# for line in output_word_name_entity_list:
# print(line)
return new_output_word_name_entity_list
def cos_similarity_based_on_tfidf(self, text1, text2):
text_list = [text1, text2]
vectorizer = TfidfVectorizer(token_pattern=r"(?u)\w+\b|['#/]")
tfidf_vectors_list = vectorizer.fit_transform(text_list)
vector1 = tfidf_vectors_list[0].A[0]
vector2 = tfidf_vectors_list[1].A[0]
cos_sim = sklearn.metrics.pairwise.cosine_similarity([vector1], [vector2])
# print(vectorizer.get_feature_names())
# print(vector1)
# print(vector2)
# print(cos_sim)
return cos_sim
def cos_similarity_based_on_freq(self, text1, text2):
# import sklearn.feature_extraction
# import sklearn.metrics.pairwise
# text1 = 'w bush leave for residence_ 2 $ weekday_'
# text2 = 'president george w'
# token_pattern = r"(?u)\w+\b" # ignore the punctuation
text_list = [text1, text2]
vectorizer = sklearn.feature_extraction.text.CountVectorizer(token_pattern=r"(?u)\w+\b|['#/]")
freq_vectors_list = vectorizer.fit_transform(text_list)
# vector1 = freq_vectors_list[0].A
# vector2 = freq_vectors_list[1].A
cos_sim = sklearn.metrics.pairwise.cosine_similarity(freq_vectors_list[0].A, freq_vectors_list[1].A)
# print(vectorizer.get_feature_names())
# print(vector1)
# print(vector2)
# print(cos_sim)
return cos_sim
# it returns an dictionayr of hyperonym paths of its word
def hyperonyms_paths_dict(self, input_article_file_path, input_summary_file_path,
output_article_file_path, output_summary_file_path,
output_hypernyms_dict_pickle_file_path, output_hypernyms_dict_txt_file_path,
print_per_line=2, hypernym_offset=1, min_depth=5, max_depth=6,
lines_per_pos_application=2000):
t0 = time.time()
general_hypernyms = ['abstraction', 'entity', 'attribute', 'whole', 'physical',
'entity', 'physical_entity', 'matter', 'object', 'relation', 'natural_object',
'psychological_feature']
stopword_list = nltk.corpus.stopwords.words('english')
general_categories = ['PERSON_', 'LOCATION_', 'ORGANIZATION_']
except_words = stopword_list + general_categories
word_set = set()
article_pos_list = []
summary_pos_list = []
print('Building dataset with hypernyms...')
print('Input files:\n\t{}\n\t{}'.format(input_article_file_path, input_summary_file_path))
print('Building dictionary...')
input_article_batch_text_list = []
input_summary_batch_text_list = []
with open(input_article_file_path, 'r', encoding='utf8') as f:
line_index = 0
input_temp_list = []
for line in f:
line_index += 1
input_temp_list += line.split() + ['NL_']
if line_index == lines_per_pos_application:
input_article_batch_text_list.append(input_temp_list)
input_temp_list = []
line_index = 0
if line_index > 0:
input_article_batch_text_list.append(input_temp_list)
f.close()
with open(input_summary_file_path, 'r', encoding='utf8') as f:
line_index = 0
input_temp_list = []
for line in f:
line_index += 1
input_temp_list += line.split() + ['NL_']
if line_index == lines_per_pos_application:
input_summary_batch_text_list.append(input_temp_list)
input_temp_list = []
line_index = 0
if line_index > 0:
input_summary_batch_text_list.append(input_temp_list)
f.close()
count_words = 0
# with open(input_article_file_path, 'r', encoding='utf8') as f:
t1 = time.time()
line_index = 0
for batch_text_list in input_article_batch_text_list:
line_index += lines_per_pos_application
pos_list = self.wordnet_pos_n_tag(batch_text_list)
article_pos_list.append(pos_list)
for (word, pos) in pos_list:
if pos == 'n' and word not in except_words and word != 'NL_':
word_set.add(word)
count_words += 1
if line_index % print_per_line == 0:
t = time.time()
print('{} line, Time (overall and per {} lines): {} & {:.2f}'.format(
line_index, 1000, datetime.timedelta(seconds=t - t0),
(t - t1) * 1000 / print_per_line))
t1 = t
del input_article_batch_text_list
print('Article vocabulary has been loaded.')
t1 = time.time()
line_index = 0
for batch_text_list in input_summary_batch_text_list:
line_index += lines_per_pos_application
pos_list = self.wordnet_pos_n_tag(batch_text_list)
summary_pos_list.append(pos_list)
for (word, pos) in pos_list:
if pos == 'n' and word not in except_words and word != 'NL_':
word_set.add(word)
count_words += 1
if line_index % print_per_line == 0:
t = time.time()
print('{} line, Time (overall and per {} lines): {} & {:.2f}'.format(
line_index, 1000, datetime.timedelta(seconds=t - t0),
(t - t1) * 1000 / print_per_line))
t1 = t
del input_summary_batch_text_list
print('Summary vocabulary has been loaded.')
print('Dictionary has been built (count_words: {}).'.format(count_words))
print('Extracting hypernyms...')
word_hypernym_path_dict = dict()
for token in word_set:
synset = self.make_synset(token, category='n')
if synset is not None:
synset.max_depth()
merged_synset_list = self.merge_lists(synset.hypernym_paths())
sorted_synsets = self.syncet_sort_accornding_max_depth(merged_synset_list)
word_depth_list = self.word_depth_of_synsents(sorted_synsets)
if word_depth_list[0][0] != token:
word_depth_list = [(token, word_depth_list[0][1] + 1)] + word_depth_list
word_hypernym_path_dict[token] = word_depth_list
del word_set
###############
with open(output_hypernyms_dict_txt_file_path, 'w', encoding='utf8') as f:
for k, v in word_hypernym_path_dict.items():
f.write('{} {}\n'.format(k, v))
with open(output_hypernyms_dict_pickle_file_path, 'wb') as f:
pickle.dump(word_hypernym_path_dict, f)
print('Hypernyms have been written to files:\n\t{}\n\t{}'.format(
output_hypernyms_dict_pickle_file_path, output_hypernyms_dict_txt_file_path))
print('writing article file...')
t1 = time.time()
# depth_greater_than = min_depth - 1
with open(output_article_file_path, 'w', encoding='utf8') as f:
line_index = 0
for pos_list in article_pos_list:
line_index += lines_per_pos_application
for (word, pos) in pos_list:
if word == 'NL_':
f.write('\n')
elif pos == 'n' and word not in except_words:
try:
hypernyms_path_list = word_hypernym_path_dict[word]
# print(hypernyms_path_list)
# hypernym_token = hypernyms_path_list[hypernym_offset][0]
depth = hypernyms_path_list[0][1]
if depth > max_depth:
for el in hypernyms_path_list:
if el[1] <= max_depth:
# print(el[2])
f.write(el[0] + '_ ')
break
else:
f.write(word + ' ')
except KeyError:
f.write(word + ' ')
except IndexError:
f.write(word + ' ')
else:
f.write(word + ' ')
# f.write('\n')
if line_index % print_per_line == 0:
t = time.time()
print('{} line, Time (overall and per {} lines): {} & {:.2f}'.format(
line_index, 1000, datetime.timedelta(seconds=t - t0),
(t - t1) * 1000 / print_per_line))
t1 = t
del article_pos_list
print('writing summary file...')
t1 = time.time()
with open(output_summary_file_path, 'w', encoding='utf8') as f:
line_index = 0
for pos_list in summary_pos_list:
line_index += lines_per_pos_application
for (word, pos) in pos_list:
if word == 'NL_':
f.write('\n')
elif pos == 'n' and word not in except_words:
try:
hypernyms_path_list = word_hypernym_path_dict[word]
# hypernym_token = hypernyms_path_list[hypernym_offset][0]
depth = hypernyms_path_list[0][1]
if depth > max_depth:
for el in hypernyms_path_list:
if el[1] <= max_depth:
f.write(el[0] + '_ ')
break
else:
f.write(word + ' ')
except KeyError:
f.write(word + ' ')
except IndexError:
f.write(word + ' ')
else:
f.write(word + ' ')
# f.write('\n')
if line_index % print_per_line == 0:
t = time.time()
print('{} line, Time (overall and per {} lines): {} & {:.2f}'.format(
line_index, 1000, datetime.timedelta(seconds=t - t0),
(t - t1) * 1000 / print_per_line))
t1 = t
print('Output files:\n\t{}\n\t{}'.format(output_article_file_path, output_summary_file_path))
# return word_hypernym_path_dict
@staticmethod
# it returns a list of tuples: (word, pos)
def wordnet_pos_n_tag(text_list):
# wordnet pos: (ADJ, ADJ_SAT, ADV, NOUN, VERB) = ('a', 's', 'r', 'n', 'v')
pos_tag_list = nltk.tag.pos_tag(text_list)
word_pos_list = []
for (w, pos) in pos_tag_list:
wordnet_pos = 'other'
if pos.startswith('N'):
wordnet_pos = 'n' # nltk.corpus.wordnet.NOUN
word_pos_list.append((w, wordnet_pos))
return word_pos_list
def min_common_hyperonym_of_vocabulary(self, input_file_path='path/to/file',
output_dictionary_pickle_file="", output_dict_txt_file="", time_of_pass=1):
stopword_list = nltk.corpus.stopwords.words('english')
general_categories = ['PERSON_', 'LOCATION_', 'ORGANIZATION_']
general_hypernyms = ['abstraction', 'entity', 'attribute', 'whole', 'physical',
'entity', 'physical_entity', 'matter', 'object', 'relation', 'natural_object']
except_words = stopword_list + general_categories
# print(except_words)
word_pos_freq_dict = dict()
print('Building dictionary')
count_words = 0
with open(input_file_path, 'r', encoding='utf8') as f:
for line in f:
line_list = line.split()
pos_list, _ = self.wordnet_pos_tag(line_list)
for word, pos in pos_list:
if pos == 'n' and word not in except_words:
if word_pos_freq_dict.get((word, pos), None):
new_freq = word_pos_freq_dict[(word, pos)] + 1
word_pos_freq_dict[(word, pos)] = new_freq
else:
word_pos_freq_dict[(word, pos)] = 1
count_words += 1
print('dictionary is built. count_words: {}'.format(count_words))
word_pos_list = []
for k, v in word_pos_freq_dict.items():
word_pos_list.append((k, v))
##################
# print(k, v)
word_pos_list = sorted(word_pos_list, key=lambda tup: -tup[1])
#############
# print(word_pos_list)
del word_pos_freq_dict
word_hypernym_dict = dict()
# word2_start_index = 0
for ((word1, pos1), freq1) in word_pos_list:
# word2_start_index += 1
hypernym_freq_dict = dict()
try:
synset1 = self.make_synset(word1)
for ((word2, pos2), freq2) in word_pos_list:
synset2 = self.make_synset(word2)
# try:
common_hypernyms = synset1.lowest_common_hypernyms(synset2)
# except Exception:
if common_hypernyms != []:
for ch in common_hypernyms:
ch_word = self.synset_word(ch)
if ch_word not in general_hypernyms:
if hypernym_freq_dict.get(ch_word, None):
new_freq = hypernym_freq_dict[ch_word] + freq2
hypernym_freq_dict[ch_word] = new_freq
else:
hypernym_freq_dict[ch_word] = freq2
else:
word_hypernym_dict[word1] = word1
except nltk.corpus.reader.wordnet.WordNetError:
word_hypernym_dict[word1] = word1
max_freq = 0
for k, v in hypernym_freq_dict.items():
############
# print(k, v)
if v > max_freq:
word_hypernym_dict[word1] = k
################
for k, v in word_hypernym_dict.items():
if k != v:
print(k, v)
def convert_dataset_with_hypernyms(self, input_article_file_path, output_article_file_path,
input_summary_file_path, output_summary_file_path,
lines_per_ner_application=2500, print_per_lines=10000):
for (input_file_path, output_file_path) in [(input_article_file_path, output_article_file_path),
(input_summary_file_path, output_summary_file_path)]:
self.convert_text_with_hypernyms(input_file_path, output_file_path,
lines_per_ner_application=lines_per_ner_application,
print_per_lines=print_per_lines)
def convert_dataset_with_ner(self, input_article_file_path, output_article_file_path,
input_summary_file_path, output_summary_file_path,
lines_per_ner_application=2500, print_per_lines=10000):
for (input_file_path, output_file_path) in [(input_article_file_path, output_article_file_path),
(input_summary_file_path, output_summary_file_path)]:
self.convert_text_with_ner(input_file_path, output_file_path,
lines_per_ner_application=lines_per_ner_application,
print_per_lines=print_per_lines)
def convert_text_with_hypernyms(self, input_file_path, output_file_path,
lines_per_ner_application=2500, print_per_lines=10000):
print('Named Entity Recognition and convert the dataset')
print('Input file: {}\n'.format(input_file_path))
input_batch_text_list = []
with open(input_file_path, 'r', encoding='utf8') as f:
line_index = 0
input_temp_list = []
for line in f:
line_index += 1
input_temp_list += line.split() + ['NL_']
if line_index == lines_per_ner_application:
input_batch_text_list.append(input_temp_list)
input_temp_list = []
line_index = 0
if line_index > 0:
input_batch_text_list.append(input_temp_list)
f.close()
print('Input data loaded.')
ner_list = []
lines_index = 0
t0 = time.time()
for el_list in input_batch_text_list:
ner_list += self.stanford_ner(el_list)
lines_index += lines_per_ner_application
if lines_index % print_per_lines == 0:
dt = time.time() - t0
print('NER: {} lines, Time (total and avg per 1000 lines) {} & {:.3f} sec,'.format(
lines_index, datetime.timedelta(seconds=dt), dt * 1000 / lines_index))
del input_batch_text_list
print('NER have been run')
ner_freq_dict = dict()
ner_tag_list = ['LOCATION', 'PERSON', 'ORGANIZATION']
output_file = open(output_file_path, 'w', encoding='utf8')
previous_text = ''
for (token, ner) in ner_list:
# temp_text = token
if token == 'NL_':
output_file.write('\n')
previous_text = ''
elif ner in ner_tag_list:
if ner != previous_text:
output_file.write(ner + '_ ')
previous_text = ner
if ner_freq_dict.get(ner, None):
new_freq = ner_freq_dict[ner] + 1
ner_freq_dict[ner] = new_freq
else:
ner_freq_dict[ner] = 1
else:
output_file.write(token + ' ')
previous_text = token
output_file.close()
del ner_list
for k, v in ner_freq_dict.items():
print(k, v)
print('Output file: {}\n'.format(output_file_path))
def convert_text_with_ner(self, input_file_path, output_file_path,
lines_per_ner_application=2500, print_per_lines=10000):
print('Named Entity Recognition and convert the dataset')
print('Input file: {}\n'.format(input_file_path))
input_batch_text_list = []
with open(input_file_path, 'r', encoding='utf8') as f:
line_index = 0
input_temp_list = []
for line in f:
line_index += 1