-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathspacy_utils.py
More file actions
1175 lines (1100 loc) · 58.9 KB
/
spacy_utils.py
File metadata and controls
1175 lines (1100 loc) · 58.9 KB
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
import spacy
from process_utils import *
spacy_nlp = spacy.load("en_core_web_lg")
spacy_nlp.add_pipe("merge_entities")
punctions = ["–", "—", ";", ",", ".", ":", "\""]
def get_verb_phrases(sent, hyp_words, spill_words_list, sbar_list):
basic_elements = []
vp_list = []
spacy_nlp.disable_pipe("merge_entities")
doc = spacy_nlp(sent)
s_word = [tok.text for tok in doc]
pos_list = [tok.pos_ for tok in doc]
i = 0
dep_map = {}
root_verb = ""
root_idx = -1
for token in doc:
if (token.dep_ == "ROOT") & (token.pos_ in ["VERB", "AUX"]) & (token.text not in ["Said"]):
basic_elements.append((i, token.dep_, token.text, token.pos_, token.text))
root_verb = token.text
root_idx = i
break
i += 1
if (root_idx == -1) & (("VERB" in pos_list) | ("AUX" in pos_list)):
root_idx = 0
while pos_list[root_idx] not in ["VERB", "AUX"]:
root_idx += 1
root_verb = doc[root_idx].text
basic_elements.append((root_idx, "ROOT", root_verb, doc[root_idx].pos_, token.head.text))
i = 0
for token in doc:
if ("subj" in token.dep_) & (token.head.pos_ in ["VERB", "AUX"]) & (
"ROOT" in [token.head.dep_, token.head.head.dep_]):
subj_words = [tok.orth_ for tok in token.subtree]
subj_str = del_sbar_in_phrase(subj_words, hyp_words, sent)
basic_elements.append((i, token.dep_, token.text, subj_str, token.head.text))
if ("expl" in token.dep_) & (token.head.pos_ in ["VERB", "AUX"]) & (
"ROOT" in [token.head.dep_, token.head.head.dep_]):
basic_elements.append((i, token.dep_, token.text, token.pos_, token.head.text))
if ("advmod" in token.dep_) & (token.head.dep_ == "ROOT") & (token.text in ["here", "there"]):
if root_idx < i:
adv_words = s_word[root_idx:i + 1]
else:
adv_words = s_word[i: root_idx + 1]
adv_str = del_sbar_in_phrase(adv_words, hyp_words, sent)
basic_elements.append((i, token.dep_, adv_str, token.pos_, token.head.text))
if ("obj" in token.dep_) & (token.head.pos_ in ["VERB", "AUX"]) & (token.head.dep_ == "ROOT"):
if root_idx < i:
obj_words = s_word[root_idx:i + 1]
else:
obj_words = s_word[i: root_idx + 1]
obj_str = del_sbar_in_phrase(obj_words, hyp_words, sent)
basic_elements.append((i, token.dep_, obj_str, token.pos_, token.head.text))
if ("dative" in token.dep_) & (token.head.pos_ in ["VERB", "AUX"]) & (token.head.dep_ == "ROOT"):
if root_idx < i:
dat_words = s_word[root_idx:i + 1]
else:
dat_words = s_word[i: root_idx + 1]
dat_str = del_sbar_in_phrase(dat_words, hyp_words, sent)
basic_elements.append((i, token.dep_, dat_str, token.pos_, token.head.text))
if ("comp" in token.dep_) & (token.head.pos_ in ["VERB", "AUX"]) & (token.head.dep_ == "ROOT"):
if root_idx < i:
vp_words = s_word[root_idx:i + 1]
comp_str = process_hyp_words(" ".join(vp_words), hyp_words, sent, -1).replace("et al .", "et al.")
if (basic_elements[-1][2] not in comp_str) | (basic_elements[-1][1] == "ROOT"):
basic_elements.append((i, token.dep_, comp_str, token.pos_, token.head.text))
# print("comp_str:", comp_str)
if ("attr" in token.dep_) & (token.head.pos_ in ["VERB", "AUX"]) & (token.head.dep_ == "ROOT"):
if root_idx < i:
vp_words = s_word[root_idx:i + 1]
else:
vp_words = s_word[i: root_idx + 1]
attr_str = process_hyp_words(" ".join(vp_words), hyp_words, sent, -1).replace("et al .", "et al.")
if basic_elements[-1][2] not in attr_str:
basic_elements.append((i, token.dep_, attr_str, token.pos_, token.head.text))
# print("attr_str:", attr_str)
key = token.text + "-" + str(i)
if key not in dep_map.keys():
dep_map[key] = []
dep_map[key].append((token.head.text, token.head.pos_, token.dep_, token.text))
i += 1
subj_list = [elem for elem in basic_elements if ((("subj" in elem[1]) | ("expl" in elem[1])) & (elem[0] < root_idx))]
if len(subj_list) == 2:
new_subj = subj_list[0][3] + " " + subj_list[1][3]
if check_continuity(new_subj.split(), sent.split(), -1) != -1:
basic_elements.remove(subj_list[0])
basic_elements.remove(subj_list[1])
basic_elements.append((subj_list[0][0], subj_list[0][1], subj_list[0][2], new_subj, subj_list[0][4]))
noun_subj_elem = get_subj_by_noun(root_idx, pos_list, sbar_list, doc, hyp_words, sent)
if len([elem for elem in basic_elements if ((("subj" in elem[1]) | ("expl" in elem[1])) & (elem[0] < root_idx))]) == 0:
basic_elements.append(noun_subj_elem)
i = 0
for w in doc:
if (i > 0) & (i + 1 < len(s_word)):
if ((s_word[i - 1] in ["-", "—"]) | (s_word[i + 1] in ["-", "—"])) & (w.text in spill_words_list):
i += 1
continue
if (w.dep_ == "acl") & (w.pos_ == "VERB"):
network = [t.text for t in list(w.children)]
if len(network) != 0:
acl_word = [tok.orth_ for tok in w.subtree]
for f in formulations:
nf = f.replace(" ", "")
if acl_word[-1] == nf[:len(acl_word[-1])]:
vp_for = acl_word[-1]
for idx in range(i + len(acl_word), len(s_word)):
vp_for = vp_for + s_word[idx]
if vp_for == nf:
comp_f = f
acl_word[-1] = comp_f
break
break
acl_str = del_sbar_in_phrase(acl_word, hyp_words, sent)
save_flag = True
if acl_word[0] == w.text:
for j in range(len(vp_list)):
vp = vp_list[j]
if vp[1] in acl_str:
save_flag = False
break
elif acl_str in vp[1]:
new_acl = vp[1].split(acl_str)[0].strip()
if new_acl != '':
vp_list[j] = (vp[0], new_acl)
else:
save_flag = False
break
if save_flag & (acl_str != '') & (acl_str in sent):
vp_list.append(("acl", acl_str))
if (w.dep_ == "xcomp") & (w.pos_ == "VERB"):
network = [t.text for t in list(w.children)]
if len(network) != 0:
xcomp_word = [tok.orth_ for tok in w.subtree]
for f in formulations:
nf = f.replace(" ", "")
if xcomp_word[-1] == nf[:len(xcomp_word[-1])]:
vp_for = xcomp_word[-1]
for idx in range(i + len(xcomp_word), len(s_word)):
vp_for = vp_for + s_word[idx]
if vp_for == nf:
comp_f = f
xcomp_word[-1] = comp_f
break
break
if i > 1:
if (s_word[i - 1] == "\'") & (s_word[i] in ['re', 've', 's', 'm']):
xcomp_word.insert(0, "\'")
xcomp_str = del_sbar_in_phrase(xcomp_word, hyp_words, sent)
save_flag = True
if (xcomp_word[0] == w.text) | (xcomp_word[0] == "\'"):
for vp in vp_list:
if vp[1] in xcomp_str:
save_flag = False
break
if save_flag & (xcomp_str != ''):
vp_list.append(("xcomp", xcomp_str))
key = w.text + "-" + str(i)
if key in dep_map.keys():
if len(dep_map[key]) != 0:
dep_list = dep_map[key]
for dep_re in dep_list:
if dep_re[1] in ["VERB", "AUX"]:
if (dep_re[2] == "acomp") & (dep_re[0] in s_word[:i]):
s_idx = i - 1
while s_word[s_idx] != dep_re[0]:
s_idx -= 1
if s_idx > 1:
## Completing the abbreviation of be verbs
if (s_word[s_idx - 1] == "\'") & (s_word[s_idx] in ['re', 've', 's', 'm']):
s_idx = s_idx - 1
vp_word = s_word[s_idx:i + 1]
aco_str = process_hyp_words(" ".join(vp_word), hyp_words, sent, -1)
if len(vp_list) > 0:
if vp_list[-1][1] in aco_str:
continue
if aco_str in vp_list[-1][1]:
vp_list.pop()
else:
temp = (vp_list[-1][0],
vp_list[-1][1] + " " + process_hyp_words(" ".join(vp_word[1:]), hyp_words,
sent, -1))
if temp[1] in sent:
vp_list.pop()
vp_list.append(temp)
continue
save_flag = True
for vp in vp_list:
if vp[1] in aco_str:
save_flag = False
break
if save_flag & (aco_str != ''):
vp_list.append(("acomp", aco_str))
if (dep_re[2] in ["auxpass", "aux"]) & (dep_re[0] in s_word[i:]):
s_idx = i
e_idx = s_word.index(dep_re[0], i)
if e_idx + 1 < len(s_word):
if s_word[e_idx + 1] == "/":
ne_idx = e_idx + 1
ne_flag = True
while pos_list[ne_idx] != pos_list[s_word.index(dep_re[0], i)]:
ne_idx += 1
if ne_idx == len(s_word):
ne_flag = False
break
if ne_flag:
e_idx = ne_idx
vp_word = s_word[s_idx:e_idx + 1]
pass_str = process_hyp_words(" ".join(vp_word), hyp_words, sent, -1).replace(
"et al .", "et al.")
if len(vp_list) > 0:
if (vp_list[-1][1] in pass_str) | (
(s_word[s_idx] == "m") & (s_word[s_idx - 1] != "\'")):
continue
if (s_word[s_idx] in ["m", "re", "ve", "s"]) & (s_word[s_idx - 1] == "\'") & (pass_str in vp_list[-1][1]):
continue
if pass_str in vp_list[-1][1]:
vp_list.pop()
save_flag = True
for vp in vp_list:
if pass_str in vp[1]:
save_flag = False
break
if save_flag & (pass_str != ''):
vp_list.append(("aux", pass_str))
if (dep_re[2] == "oprd") & (dep_re[0] in s_word[:i]):
s_idx = i - 1
while s_word[s_idx] != dep_re[0]:
s_idx -= 1
vp_word = s_word[s_idx:i + 1]
oprd_str = process_hyp_words(" ".join(vp_word), hyp_words, sent, -1)
## acl and oprd can be same or included
if len(vp_list) > 0:
if vp_list[-1][1] in oprd_str:
continue
if oprd_str in vp_list[-1][1]:
vp_list.pop()
else:
last_vp_words = vp_list[-1][1].split(" ")
if (vp_word[0] in last_vp_words) & (vp_word[0] != last_vp_words[0]):
c_idx = last_vp_words.index(vp_word[0])
last_vp = " ".join(last_vp_words[:c_idx])
temp = (
vp_list[-1][0],
last_vp + " " + process_hyp_words(" ".join(vp_word), hyp_words,
sent, -1))
if temp[1] in sent:
vp_list.pop()
vp_list.append(temp)
continue
save_flag = True
for vp in vp_list:
if vp[1] in oprd_str:
save_flag = False
break
if save_flag & (oprd_str != ''):
vp_list.append(("oprd", oprd_str))
i += 1
return vp_list, basic_elements, root_verb, root_idx, noun_subj_elem
## Determine whether the prepositional phrase contains a prepositional phrase
def exist_pp(pos_list, pp_word, dictionary, key_pp, to_flag, spill_words_list, pp_list):
count = 0
key = -1
comp_flag = False
sent = " ".join(pp_word).replace(" - ", "-").replace(" – ", "–")
spacy_nlp.disable_pipe("merge_entities")
doc = spacy_nlp(sent)
i = 0
p_idx = pp_word.index(key_pp)
if p_idx == len(pp_word) - 1:
return False, key, comp_flag
for pp in pp_list:
if pp[2] == key_pp:
temp_idx = pp[1].split(" ").index(pp[2])
if (" ".join(pp[1].split(" ")[:temp_idx]) in " ".join(pp_word[:p_idx])) & (temp_idx != 0) & (key_pp in pp_word[p_idx+1:]):
p_idx = pp_word.index(key_pp, p_idx + 1)
if p_idx + 1 < len(pp_word):
if pp_word[p_idx + 1] in ["-", "–"]:
p_idx = pp_word.index(key_pp, p_idx + 1)
if p_idx - 1 >= 0:
if pp_word[p_idx - 1] in ["-", "–"]:
p_idx = pp_word.index(key_pp, p_idx + 1)
for pp in dictionary["comp"]:
if pp in " ".join(pp_word).lower():
if key_pp.lower() == pp.split(" ")[0]:
p_idx = check_continuity(pp.split(" "), pp_word, -1)
if p_idx == -1:
pp = pp[0].upper() + pp[1:]
p_idx = check_continuity(pp.split(" "), pp_word, -1)
p_idx = p_idx + len(pp.split(" ")) - 1
break
else:
if key_pp in pp.split(" ")[1:]:
comp_flag = True
first_to = -1
if key_pp == "from":
if "to" in pp_word[p_idx:]:
first_to = pp_word[p_idx:].index("to")
for w in doc:
if i > p_idx:
if (pos_list[i] == "ADP") & (w.text not in ["of", "v", "than"]):
if (i - 1 >= 0) & (i + 1 < len(pp_word)) & (w.text in spill_words_list):
if (pp_word[i - 1] in ["-", "–", "−"]) | (pp_word[i + 1] in ["-", "–", "−"]):
i += 1
continue
network = [t.text for t in list(w.children)]
if len(network) != 0:
if w.head.pos_ in ["VERB", "AUX"]:
## ing modify noun
if w.head.dep_ == "acl":
count += 1
key = pp_word.index(w.head.text)
if (pos_list[key - 1] == "PRON") | (key - 1 == p_idx):
count -= 1
i += 1
continue
## already
if pos_list[key - 1] == "ADV":
key = key - 1
break
elif w.text in ["during", "after", "before"]:
count += 1
## on Monday in March
elif (network[0] in dictionary["on"].keys()) | (network[0] in dictionary["in"].keys()):
count += 1
else:
i += 1
continue
elif w.text == "as":
if "as" in pp_word[:i]:
as_idx = i - 1
while pp_word[as_idx] != "as":
as_idx -= 1
if pos_list[as_idx] == "ADV":
i += 1
continue
elif pp_word[i - 1] == "such":
count += 1
i -= 1
else:
count += 1
else:
if (key_pp == "from") & ((first_to + p_idx) == i):
i += 1
continue
count += 1
words = [tok.orth_ for tok in w.subtree]
if words[0] != w.text:
p_idx = words.index(w.text)
key = i - p_idx
break
elif (pp_word[0] == "among") & (pos_list[i] == "VERB"):
count += 1
elif w.text in ["while", "when", "where", "which", "who", "that", "during", "according"]:
if i - 1 != p_idx:
count += 1
elif (w.text == "to") & (not to_flag):
if ((key_pp == "from") & ((first_to + p_idx) == i)) | (pos_list[i - 1] == "VERB"):
i += 1
continue
count += 1
elif i < len(pp_word) - 1:
if (w.text == ",") & ((pp_word[i + 1] in ["called"]) | (key_pp in ["by"])):
count += 1
if count == 1:
key = i
break
i += 1
if count == 1:
if key - 1 != p_idx:
return True, key, comp_flag
return False, key, comp_flag
## obtain the prefix of "of" phrase
def get_prep_of(doc, dictionary, all_pos_list, s_word, spill_words_list):
i = 0
prep_of = []
for token in doc:
if token.text == "of":
head_text = s_word[i - 1]
if head_text[-3:] == "ies":
of_type = 1
of_key = head_text[:-3].lower() + "y"
elif head_text[-1:] == "s":
of_type = 2
of_key = head_text[:-1].lower()
else:
of_type = 3
of_key = head_text.lower()
if (token.dep_ in ["prep", "cc"]) & (s_word[i - 1] != ","):
if (token.head.pos_ in ["ADJ", "VERB"]) & (head_text in s_word[:i]):
if head_text == s_word[i - 1]:
prep_of.append(token.head.text)
else:
prefix = ""
j = i - 1
while s_word[j] != head_text:
prefix = s_word[j] + " " + prefix
j -= 1
prefix = head_text + " " + prefix
prep_of.append(prefix.strip().rstrip())
elif of_key in dictionary["of"].keys():
comp_prep_word = dictionary["of"][of_key].split(" ")
if comp_prep_word[0] in ["a", "an"]:
if (of_type in [1, 2]) & (comp_prep_word[1] not in ["series"]):
if "NUM" in all_pos_list[0:i]:
search_idx = i - 1
while (all_pos_list[search_idx:i].count("NUM") == 0) & (
s_word[search_idx] not in ["all", ","]):
search_idx = search_idx - 1
if search_idx - i < 8:
prep_of.append(" ".join(s_word[search_idx:i]))
else:
if (s_word[i - 1] in spill_words_list) & (s_word[i - 2] in spill_words_list):
prep_of.append(s_word[i - 2] + " " + s_word[i - 1])
else:
prep_of.append(s_word[i - 1])
else:
prep_of.append(s_word[i - 1])
elif "DET" in all_pos_list[0:i]:
search_idx = i - 1
noun_count = 0
if all_pos_list[search_idx] == "NOUN":
while all_pos_list[search_idx] == "NOUN":
noun_count += 1
search_idx -= 1
while all_pos_list[search_idx] not in ["DET", "PUNCT"]:
search_idx = search_idx - 1
if s_word[search_idx] == ",":
search_idx += 1
if (search_idx - i < 8) & \
(all_pos_list[search_idx:i].count("NOUN") + all_pos_list[search_idx:i].count(
"PROPN") - noun_count == 0):
prep_of.append(" ".join(s_word[search_idx:i]))
else:
if (s_word[i - 1] in spill_words_list) & (s_word[i - 2] in spill_words_list):
prep_of.append(s_word[i - 2] + " " + s_word[i - 1])
else:
prep_of.append(s_word[i - 1])
elif ("one" in s_word[0:i]) | ("One" in s_word[0:i]):
search_idx = i - 1
while all_pos_list[search_idx:i].count("NUM") == 0:
search_idx = search_idx - 1
if search_idx - i < 8:
prep_of.append(" ".join(s_word[search_idx:i]))
else:
if (s_word[i - 1] in spill_words_list) & (s_word[i - 2] in spill_words_list):
prep_of.append(s_word[i - 2] + " " + s_word[i - 1])
else:
prep_of.append(s_word[i - 1])
else:
if (s_word[i - 1] in spill_words_list) & (s_word[i - 2] in spill_words_list):
prep_of.append(s_word[i - 2] + " " + s_word[i - 1])
else:
prep_of.append(s_word[i - 1])
else:
if s_word[i - 1] in ["percent", "%"]:
search_idx = i - 2
while (all_pos_list[search_idx:i].count("NUM") == 0) & (search_idx > 0):
search_idx = search_idx - 1
if all_pos_list[search_idx:i].count("NUM") != 0:
prep_of.append(" ".join(s_word[search_idx:i]))
else:
while (all_pos_list[search_idx:i].count("DET") == 0) & (search_idx > 0):
search_idx = search_idx - 1
prep_of.append(" ".join(s_word[search_idx:i]))
elif (all_pos_list[i - 1] == "NOUN") & (i - 2 > 0):
search_idx = i - 2
noun_count = 0
if all_pos_list[search_idx] == "NOUN":
while all_pos_list[search_idx] == "NOUN":
noun_count += 1
search_idx -= 1
while (all_pos_list[search_idx] != "DET") & (s_word[search_idx] != ","):
search_idx = search_idx - 1
if search_idx < 0:
break
if (search_idx > 0) & (search_idx - i < 8) & \
(all_pos_list[search_idx:i].count("NOUN") + all_pos_list[search_idx:i].count(
"PROPN") - noun_count == 0):
prep_of.append(" ".join(s_word[search_idx:i]))
else:
if (s_word[i - 1] in spill_words_list) & (s_word[i - 2] in spill_words_list):
prep_of.append(s_word[i - 2] + " " + s_word[i - 1])
else:
prep_of.append(s_word[i - 1])
else:
prep_of.append(s_word[i - 1])
elif s_word[i - 1] in ["percent", "%"]:
search_idx = i - 2
while all_pos_list[search_idx:i].count("NUM") == 0:
search_idx = search_idx - 1
prep_of.append(" ".join(s_word[search_idx:i]))
elif all_pos_list[i - 1] == "NUM":
prep_of.append(s_word[i - 1])
else:
prep_of.append('X')
elif token.dep_ == "conj":
prep_of.append(s_word[i - 1])
else:
prep_of.append('X')
i += 1
return prep_of
def complement_pp_word(pp_word, s_word, pos_list, all_pos_list, abbr_words, spill_words_list):
comp_f = ""
comp_abbr = ""
s_idx = check_continuity(pp_word, s_word, -1)
e_idx = s_idx + len(pp_word) - 1
## process punct adj
if (s_word[e_idx] == "–") & (s_word[e_idx - 1] in spill_words_list):
pp_word.append(s_word[e_idx + 1])
pos_list.append(all_pos_list[e_idx + 1])
if e_idx + 1 < len(s_word):
if (s_word[e_idx + 1] == "–") & (s_word[e_idx] in spill_words_list):
pp_word.extend(s_word[e_idx + 1:e_idx + 3])
pos_list.extend(all_pos_list[e_idx + 1:e_idx + 3])
## process formulation
for f in formulations:
nf = f.replace(" ", "")
if pp_word[-1] == nf[:len(pp_word[-1])]:
pp_for = pp_word[-1]
for idx in range(e_idx + 1, len(s_word)):
pp_for = pp_for + s_word[idx]
if pp_for == nf:
comp_f = f
break
break
## process abbr word
for abbr in abbr_words:
if pp_word[-1] == abbr[:len(pp_word[-1])]:
pp_abbr = pp_word[-1]
for idx in range(e_idx + 1, len(s_word)):
pp_abbr = pp_abbr + s_word[idx]
if pp_abbr == abbr:
comp_abbr = abbr
break
break
if e_idx + 1 < len(s_word):
if (s_word[e_idx + 1] == "\"") & (pp_word.count("\"") % 2 != 0):
pp_word.append(s_word[e_idx + 1])
pos_list.append(all_pos_list[e_idx + 1])
return pp_word, pos_list, comp_f, comp_abbr
def merge_strings_considering_duplicates(s1, s2):
list1 = s1.split()
list2 = s2.split()
# 初始化最长公共子串的起始索引和长度
max_len = 0
index1 = index2 = 0
# 动态规划表初始化
dp = [[0] * (len(list2) + 1) for _ in range(len(list1) + 1)]
# 填充动态规划表
for i in range(1, len(list1) + 1):
for j in range(1, len(list2) + 1):
if list1[i - 1] == list2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
if dp[i][j] > max_len:
max_len = dp[i][j]
index1 = i - max_len
index2 = j - max_len
print(index1, index2)
# 如果没有找到满足条件的公共子序列,返回错误信息
if max_len >= min(len(list1), len(list2)) - 2:
merged_list = list1[:index1] + list2[index2:index2 + max_len] + list2[index2 + max_len:] + list1[
index1 + max_len:]
return True, " ".join(merged_list)
return False, "No suitable common substring found."
## obtain all prepositional phrases in one sentence by dependency relation
def get_prep_list_by_dependency(sent, hyp_words, spill_words_list, abbr_words, basic_elems):
# print(sent)
pp_list = []
spacy_nlp.disable_pipe("merge_entities")
doc = spacy_nlp(sent)
dictionary = load_dictionary("./tools/Dictionary.txt")
# noun_chunks = []
all_pos_list = [tok.pos_ for tok in doc]
s_word = [tok.text for tok in doc]
pp_flag = [0] * len(sent.split(" "))
## save the word before "of"
prep_of = get_prep_of(doc, dictionary, all_pos_list, s_word, spill_words_list)
i = 0
s_idx = -1
last_s_idx = -1
##Traversal preposition
for w in doc:
if w.text in ["v", "-", "–", "−"]:
i += 1
continue
if (i > 0) & (i + 1 < len(s_word)):
if ((s_word[i - 1] in ["-", "—"]) | (s_word[i + 1] in ["-", "—"])) & (w.text in spill_words_list):
i += 1
continue
vp_flag = False
if (w.pos_ == "ADP") | (w.text in ["to"]) & (" ".join(s_word[i - 2:i + 1]) not in dictionary["comp"]):
network = [t.text for t in list(w.children)]
if len(network) != 0:
pp_word = [tok.orth_ for tok in w.subtree]
pos_list = [tok.pos_ for tok in w.subtree]
if pp_word[-1] in ["and", "or"]:
pp_word.pop(len(pp_word) - 1)
pos_list.pop(len(pos_list) - 1)
if (w.text == "between") & ("," in pp_word):
c_idx = pp_word.index(",")
pp_word = pp_word[:c_idx]
pos_list = pos_list[:c_idx]
pp_str = process_hyp_words(" ".join(pp_word), hyp_words, sent, last_s_idx)
if pp_str in sent:
pp_word, pos_list, comp_f, comp_abbr = complement_pp_word(pp_word, s_word, pos_list, all_pos_list,
abbr_words, spill_words_list)
elif pp_str.split(",")[0] in sent:
c_idx = pp_word.index(",")
pp_word = pp_word[:c_idx]
pos_list = pos_list[:c_idx]
pp_word, pos_list, comp_f, comp_abbr = complement_pp_word(pp_word, s_word, pos_list, all_pos_list,
abbr_words, spill_words_list)
else:
i += 1
continue
alternative = list(pp_word)
old_pos_list = list(pos_list)
if w.text == "of":
if prep_of[0] != 'X':
if len(pp_list) > 0:
last_pp = pp_list[-1][1].replace("-", " - ").replace("–", " – ")
if " ".join(pp_word) in last_pp:
prep_of.pop(0)
i += 1
continue
last_pp_word = last_pp.split(" ")
prefix = prep_of[0].split(" ")
for j in range(len(prefix) - 1, -1, -1):
pp_word.insert(0, prefix[j])
pos_list.insert(0, all_pos_list[i - j])
if prefix[-1] == last_pp_word[-1]:
#s_idx = check_continuity(last_pp_word, s_word, last_s_idx - 1)
s_idx = check_continuity(last_pp_word, sent.split(" "), last_s_idx - 1)
if s_word[s_idx + len(last_pp_word)] == "of":
for i in range(len(last_pp_word)):
pp_flag[s_idx + i] = 0
for i in range(len(prefix)):
if len(last_pp_word) > 0:
last_pp_word.pop()
else:
break
last_pp_word.extend(pp_word)
last_pos_list = list(all_pos_list[s_idx:s_idx + len(last_pp_word)])
last_pos_list.extend(pos_list)
pp_word = last_pp_word
pos_list = last_pos_list
if pp_list[-1][0] == "v":
vp_flag = True
else:
vp_flag = False
pp_list.pop()
else:
prefix = prep_of[0].split(" ")
for j in range(len(prefix) - 1, -1, -1):
pp_word.insert(0, prefix[j])
pos_list.insert(0, all_pos_list[i - j])
prep_of.pop(0)
if (w.head.head.pos_ == "VERB") & (w.head.dep_ in ["prep", "dobj", "advcl"]):
pp_word, pos_list, h_idx = get_the_complete_phrase(w.head.text, w.head.head.text, s_word,
pp_word,
pos_list, all_pos_list, pp_list, hyp_words,
sent, last_s_idx)
if h_idx != -1:
vp_flag = True
if pp_flag[h_idx] == 1:
pp_word = alternative
pos_list = old_pos_list
vp_flag = False
else:
## save verb phrase
if (w.head.pos_ in ["VERB", "AUX"]) & (w.dep_ in ["prep", "agent"]):
if (pp_word[0] not in ["during", "after", "before", "via", "due", "among"]) & (
"in order " not in " ".join(pp_word)):
pp_word, pos_list, h_idx = get_the_complete_phrase(w.text, w.head.text, s_word, pp_word,
pos_list, all_pos_list, pp_list,
hyp_words,
sent, last_s_idx)
if h_idx != -1:
vp_flag = True
if pp_flag[h_idx] == 1:
pp_word = alternative
pos_list = old_pos_list
vp_flag = False
if len([prep for prep in dictionary["verb"] if prep == " ".join(pp_word[:2])]) != 0:
vp_flag = False
else:
if w.head.dep_ == "acl":
tmp_str = w.head.text + " " + w.text
if tmp_str in sent:
v_idx = check_continuity(tmp_str.split(" "), s_word, -1)
pp_word.insert(0, w.head.text)
pos_list.insert(0, all_pos_list[v_idx])
pp_flag[v_idx] = 0
vp_flag = False
elif (w.head.pos_ in ["ADV", "ADJ", "NOUN"]) & (w.head.dep_ in ["advmod", "attr", "acomp", "conj", "npadvmod"]) \
& (w.head.head.pos_ in ["VERB", "AUX"]) & (" ".join(pp_word) != "as well as"):
pp_word, pos_list, h_idx = get_the_complete_phrase(w.head.text, w.head.head.text, s_word,
pp_word,
pos_list, all_pos_list, pp_list, hyp_words,
sent, last_s_idx)
if h_idx != -1:
vp_flag = True
if pp_flag[h_idx] == 1:
pp_word = alternative
pos_list = old_pos_list
vp_flag = False
elif (w.head.pos_ in ["ADJ", "ADV"]) & (w.text == "than"):
s_idx = check_continuity(pp_word, s_word, last_s_idx)
h_idx = s_idx
while s_word[h_idx] not in [w.head.text, ","]:
h_idx -= 1
pp_word.insert(0, s_word[h_idx])
pos_list.insert(0, all_pos_list[h_idx])
if s_word[h_idx - 1] in ["more", "less"]:
pp_word.insert(0, s_word[h_idx - 1])
pos_list.insert(0, all_pos_list[h_idx - 1])
vp_flag = True
## process on Monday...in March
if (alternative[0] in ["on", "in"]) & (len(alternative) == 2):
if (alternative[1] in dictionary["on"].keys()) | (alternative[1] in dictionary["in"].keys()):
pp_str = " ".join(alternative)
pp_list.append(('p', pp_str, w.text))
#s_idx = check_continuity(alternative, s_word, last_s_idx)
s_idx = check_continuity(alternative, sent.split(" "), last_s_idx)
#pp_flag = fill_pp_flag(pp_str, s_word, pp_flag, s_idx)
pp_flag = fill_pp_flag(pp_str, sent.split(" "), pp_flag, s_idx)
i += 1
continue
if (w.text == "as") & (alternative[0] in dictionary["as"].keys()):
pp_word = alternative
pos_list = old_pos_list
if h_idx != -1:
vp_flag = False
if (w.text == "as") & (pp_word[0] == "as") & (pp_word.count("as") == 1):
if "as" in s_word[:i]:
as_idx = i - 1
while s_word[as_idx] != "as":
as_idx -= 1
if (all_pos_list[as_idx] == "ADV") & (as_idx > last_s_idx):
pp_word = s_word[as_idx:i] + pp_word
pos_list = all_pos_list[as_idx:i] + pos_list
if "ADJ" in all_pos_list[as_idx + 1 : i]:
vp_flag = True
if (pp_word[0] == "to") & (s_word[i - 1] in dictionary["to"].keys()):
pp_word.insert(0, s_word[i - 1])
pos_list.insert(0, all_pos_list[i - 1])
vp_flag = True
if (pp_word[0] == "from") & (s_word[i - 1] in dictionary["from"].keys()):
pp_word.insert(0, s_word[i - 1])
pos_list.insert(0, all_pos_list[i - 1])
## cut long prep
flag, key, comp_flag = exist_pp(pos_list, pp_word, dictionary, w.text, False, spill_words_list, pp_list)
if flag & (key > 1):
if pp_word[key - 1] in ["and", "or", "but"]:
pp_word = pp_word[:key - 1]
else:
pp_word = pp_word[:key]
if comp_flag:
i += 1
continue
## del ","
if pp_word[-1] in [",", "."]:
pp_word.pop(len(pp_word) - 1)
pos_list.pop(len(pp_word) - 1)
##Supplement of phrase
if pp_word[0] in ["+"]:
pp_word.pop(0)
pos_list.pop(0)
if len(pp_word) == 1:
i += 1
continue
if (pp_word[-1] in ["which", "what", "that"]) | (pp_word[0].lower() in ["v","what", "why", "how", "that", "which"]) | ((pp_word[-2] == w.text) & (pp_word[-1] in punctions)):
i += 1
continue
if len(comp_f) != 0:
pp_str = " ".join(pp_word[:-1]) + " " + comp_f
elif len(comp_abbr) != 0:
pp_str = " ".join(pp_word[:-1]) + " " + comp_abbr
else:
pp_str = " ".join(pp_word)
if len(pp_str.split(" ")) == 1:
i += 1
continue
pp_str = process_hyp_words(pp_str, hyp_words, sent, last_s_idx)
pp_str = get_complete_last_word(pp_str.split(" "), sent.split(" "))
pp_str = cut_sub_sent_in_pp_sbar(pp_str, pp_str.split(" "), w.text)
if len(pp_list) > 0:
if pp_str in pp_list[-1][1]:
i += 1
continue
if (pp_list[-1][1] in pp_str):
for j in range(last_s_idx, last_s_idx + len(pp_list[-1][1].split(" "))):
pp_flag[j] = 0
pp_list.pop()
last_s_idx = -1
elif min(len(pp_str.split()),len(pp_list[-1][1].split(" "))) > 4:
flag, new_pp_str = merge_strings_considering_duplicates(pp_str, pp_list[-1][1])
if flag:
for j in range(last_s_idx, last_s_idx + len(pp_list[-1][1].split(" "))):
pp_flag[j] = 0
pp_list.pop()
last_s_idx = -1
pp_str = new_pp_str
if pp_str in sent:
#s_idx = check_continuity(pp_word, s_word, last_s_idx)
s_idx = check_continuity(pp_str.split(" "), sent.split(" "), last_s_idx)
if pp_flag[s_idx] != 1:
if len(pp_list) > 0:
if pp_list[-1][1] in pp_str:
pp_list.pop()
if vp_flag:
pp_list.append(('v', pp_str, w.text))
else:
pp_list.append(('p', pp_str, w.text))
pp_flag = fill_pp_flag(pp_str, sent.split(" "), pp_flag, s_idx)
elif pp_flag[s_idx + 1] != 1:
if pp_list[-1][1].split(" ")[-1] == pp_word[0]:
new_pp_str = pp_list[-1][1] + " " + " ".join(pp_str.split(" ")[1:])
pp_list[-1] = (pp_list[-1][0], new_pp_str, pp_list[-1][2])
pp_flag = fill_pp_flag(" ".join(pp_str.split(" ")[1:]), sent.split(" "), pp_flag, s_idx + 1)
last_s_idx = s_idx
elif (w.text == "to") & (" ".join(s_word[i - 2:i + 1]) not in dictionary["comp"]):
if (w.head.pos_ in ["VERB", "AUX"]) & (w.dep_ == "aux"):
if w.head.dep_ != "ROOT":
pp_word = [tok.orth_ for tok in w.head.subtree]
pos_list = [tok.pos_ for tok in w.head.subtree]
elif len([ele for ele in basic_elems if (ele[1] == "dobj") & (w.head.text in ele[2])]) != 0:
dobj_elem = [ele for ele in basic_elems if (ele[1] == "dobj") & (w.head.text in ele[2])][0]
temp_tree = spacy_nlp(w.text + " " + dobj_elem[2])
pp_word = [tok.orth_ for tok in temp_tree]
pos_list = [tok.pos_ for tok in temp_tree]
else:
i += 1
continue
if (check_continuity(pp_word, s_word, -1) == 0) & (len(pp_word) > len(s_word)/3 * 2) & (w.head.text in s_word[i:]):
p_count = pp_word.count(w.text)
j = 0
p_idx = -1
while j < p_count:
p_idx = pp_word.index(w.text, p_idx + 1)
h_idx = pp_word.index(w.head.text, p_idx + 1)
if pp_word[p_idx:h_idx].count(w.text) == 1:
pp_word = pp_word[p_idx:]
pos_list = pos_list[p_idx:]
break
j += 1
pp_str = process_hyp_words(" ".join(pp_word), hyp_words, sent, last_s_idx)
if pp_str in sent:
pp_word, pos_list, comp_f, comp_abbr = complement_pp_word(pp_word, s_word, pos_list,
all_pos_list,
abbr_words, spill_words_list)
elif pp_str.split(",")[0] in sent:
c_idx = pp_word.index(",")
pp_word = pp_word[:c_idx]
pos_list = pos_list[:c_idx]
pp_word, pos_list, comp_f, comp_abbr = complement_pp_word(pp_word, s_word, pos_list,
all_pos_list,
abbr_words, spill_words_list)
else:
i += 1
continue
alternative = list(pp_word)
old_pos_list = list(pos_list)
## 状语从句修饰词 从句补充
if w.head.dep_ in ["ccomp", "xcomp", "advcl"]:
pp_word, pos_list, h_idx = get_the_complete_phrase(w.head.text, w.head.head.text, s_word,
pp_word, pos_list, all_pos_list, pp_list,
hyp_words, sent, last_s_idx)
if h_idx != -1:
vp_flag = True
if pp_flag[h_idx] == 1:
pp_word = alternative
pos_list = old_pos_list
vp_flag = False
if len([prep for prep in dictionary["verb"] if prep == " ".join(pp_word[:2])]) != 0:
vp_flag = False
if (pp_word[0] == "to") & (s_word[i - 1] + " to" in dictionary["to"]):
pp_word.insert(0, s_word[i - 1])
pos_list.insert(0, all_pos_list[i - 1])
## cut long prep
flag, key, comp_flag = exist_pp(pos_list, pp_word, dictionary, w.text, True, spill_words_list, pp_list)
if comp_flag:
i += 1
continue
if flag & (key > 1):
pp_word = pp_word[:key]
if w.text in " ".join(pp_word).split(" , ")[0].split():
pp_str = " ".join(pp_word).split(" , ")[0]
else:
pp_str = " ".join(pp_word)
else:
if len(comp_f) != 0:
pp_str = " ".join(pp_word[:-1]) + " " + comp_f
elif len(comp_abbr) != 0:
pp_str = " ".join(pp_word[:-1]) + " " + comp_abbr
else:
pp_str = " ".join(pp_word)
pp_str = process_hyp_words(pp_str, hyp_words, sent, last_s_idx)
if (len(pp_str.split(" ")) == 1) | (pp_str.split(" ")[0] == "v") | (pp_str.split(" ")[-1] == "to"):
i += 1
continue
pp_str = get_complete_last_word(pp_str.split(" "), sent.split(" "))
pp_str = cut_sub_sent_in_pp_sbar(pp_str, pp_str.split(" "), w.text)
if len(pp_list) > 0:
if pp_str in pp_list[-1][1]:
i += 1
continue
if pp_list[-1][1] in pp_str:
for j in range(last_s_idx, last_s_idx + len(pp_list[-1][1].split(" "))):
pp_flag[j] = 0
pp_list.pop()
last_s_idx = -1
if pp_str in sent:
#s_idx = check_continuity(pp_word, s_word, last_s_idx)
s_idx = check_continuity(pp_str.split(" "), sent.split(" "), last_s_idx)
if sent.split(" ")[s_idx - 1] in dictionary["to"].keys():
pp_str = " ".join(sent.split(" ")[s_idx - 2:s_idx]) + " " + pp_str
for j in range(s_idx - 2, s_idx):
pp_flag[j] = 0
s_idx = s_idx - 2
if pp_flag[s_idx] != 1:
if vp_flag:
pp_list.append(('v', pp_str, w.text))
else:
pp_list.append(('p', pp_str, w.text))
pp_flag = fill_pp_flag(pp_str, sent.split(" "), pp_flag, s_idx)
elif pp_flag[s_idx + 1] != 1:
if pp_list[-1][1].split(" ")[-1] == pp_word[0]:
new_pp_str = pp_list[-1][1] + " " + " ".join(pp_str.split(" ")[1:])
pp_list[-1] = (pp_list[-1][0], new_pp_str, pp_list[-1][2])
pp_flag = fill_pp_flag(" ".join(pp_str.split(" ")[1:]), sent.split(" "), pp_flag, s_idx + 1)
last_s_idx = s_idx
i += 1
return pp_list
def extract_conj(text):