-
Notifications
You must be signed in to change notification settings - Fork 4
/
feature_functions.py
1123 lines (973 loc) · 42.9 KB
/
feature_functions.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
__author__ = 'keelan'
import re
from nltk.corpus import wordnet as wn
from nltk.corpus.reader.wordnet import WordNetError as wn_error
from nltk.tree import Tree,ParentedTree
from file_reader import RAW_SENTENCES, SYNTAX_PARSE_SENTENCES, POS_SENTENCES, PRONOUN_SET, \
entity_types, RELATIONSHIPS_AND_GROUPS, COUNTRIES, NATIONALITIES, OFFICIALS, PROFESSIONS, \
TITLE_SET, POSSESSIVE_PRONOUNS, COREF, DEPENDENCIES, AUGMENTED_TREES
phrase_heads = {"PP":["IN"],
"NP":['NN', 'NNS', 'NNP', 'NNPS', 'JJ', "PRP"], #JJ as NP for examples like "many of...".
"VP":["VBD","VBZ","VB", "VBP","MD", "VBN", "VBP"],
"ADJP": ["JJ"],
"NP-TMP":['NN', 'NNS', 'NNP', 'NNPS'],
"WHADVP":["WRB"],
"WHNP":["WDT", "WP",],
"ADVP":["RB"],
}
###################
# basic functions #
###################
def relation_type(fr):
return fr.relation_type.split(".")[0]
def _get_words_in_between_(fr):
"""return the words between m1 and m2"""
sent=POS_SENTENCES[fr.article][int(fr.i_sentence)]
mention1 = _get_mentions_in_order_(fr)[0]
mention2 = _get_mentions_in_order_(fr)[1]
first_token_index = int(mention1[2])
later_token_index = int(mention2[1])
w_in_between = sent[first_token_index:later_token_index]
return w_in_between
def _get_mentions_in_order_(fr):
"""return a pair of tuples. The first one corresponds to mention1 and its info,
the second one to mention2 and its info. i = mention1 and j=mention2 don't always hold"""
if int(fr.i_offset_begin)<int(fr.j_offset_begin):
mention1 = (fr.i_token,int(fr.i_offset_begin),
int(fr.i_offset_end), fr.i_entity_type, int(fr.i_sentence))
mention2 = (fr.j_token,int(fr.j_offset_begin),
int(fr.j_offset_end), fr.j_entity_type, int(fr.j_sentence))
else:
mention2 = (fr.i_token,int(fr.i_offset_begin),
int(fr.i_offset_end), int(fr.i_entity_type), int(fr.i_sentence))
mention1 = (fr.j_token,int(fr.j_offset_begin),
int(fr.j_offset_end), int(fr.j_entity_type), int(fr.j_sentence))
return (mention1,mention2)
def _get_lowest_common_ancestor_(fr,s_tree):
"""return the lowest common ancestor tree """
s_tree = SYNTAX_PARSE_SENTENCES[fr.article][int(fr.i_sentence)]
mention1 = _get_mentions_in_order_(fr)[0]
mention2= _get_mentions_in_order_(fr)[1]
first_entity_index = int(mention1[1])
later_entity_index = int(mention2[2])-1
lwca_tuple=s_tree.treeposition_spanning_leaves(first_entity_index, later_entity_index+1)
lowest_common_ancestor = s_tree[lwca_tuple]
return lowest_common_ancestor
def _find_head_of_tree_(tree):
"""given a tree, return its head word"""
result = "None"
if tree.node not in phrase_heads.keys():
for child in tree:
if child.node in ["WHNP", "MD", "VP", "S", "SQ", "SBAR"]:
result= _find_head_of_tree_(child)
break
else:
for child in tree:
if isinstance(child,ParentedTree):
sibling = child.right_sibling()
next_is_not_head = isinstance(sibling,ParentedTree) and \
sibling.node not in phrase_heads[tree.node]
if child.node in phrase_heads[tree.node]:
if next_is_not_head:
result= child[0]
elif not isinstance(sibling,ParentedTree):
result= child[0]
break
elif child.node == tree.node and next_is_not_head:
result = _find_head_of_tree_(child)
break
else:
result = child
return result
####################
# Anya's functions #
####################
def i_pos_j_pos(fr):
"""Returns the POS of the two mentions, comma-separated: [NNP,VBD]"""
i_pos=POS_SENTENCES[fr.article][fr.i_sentence][fr.i_offset_begin][1]
j_pos=POS_SENTENCES[fr.article][fr.j_sentence][fr.j_offset_begin][1]
return "i_pos_j_pos=[{},{}]".format(i_pos,j_pos)
def general_pos_ij(fr):
"""
Returns the shortened POS of the two mentions, comma-separated.
I've decided that the first letter of the POS is good enough.
"""
i_pos=POS_SENTENCES[fr.article][fr.i_sentence][fr.i_offset_begin][1][0]
j_pos=POS_SENTENCES[fr.article][fr.j_sentence][fr.j_offset_begin][1][0]
return "general_pos_ij=[{},{}]".format(i_pos,j_pos)
def _is_pronoun(word):
return word is not None and word.lower() in PRONOUN_SET
def same_hypernym(fr):
"""
True if the two mentions have the same hypernym in WordNet.
In multiword mentions, considering only the last word (I'm assuming last word=head).
Not considering pronouns.
Most of the logic was borrowed from Julia's WN function in the coref project - thank you.
"""
try:
i_final=wn.morphy(re.sub(r"\W", r"",fr.i_token.split('_')[-1]))
j_final=wn.morphy(re.sub(r"\W", r"",fr.j_token.split('_')[-1]))
if i_final is None or j_final is None:
return "same_hypernym={}".format(False)
if _is_pronoun(i_final) or _is_pronoun(j_final):
return "same_hypernym={}".format(False)
i_synsets=wn.synsets(i_final)
j_synsets=wn.synsets(j_final)
for i_synset in i_synsets:
i_hypernym_set=set(i_synset.hypernyms())
for j_synset in j_synsets:
j_hypernym_set=set(j_synset.hypernyms())
if i_hypernym_set.intersection(j_hypernym_set):
return "same_hypernym={}".format(True)
return "same_hypernym={}".format(False)
except wn_error:
return "same_hypernym={}".format(False)
def lowest_common_hypernym(fr):
"""
Returns the lowest common hypernym of the two mentions (based on WordNet).
Again assuming that the last word = head word, and that it represents the phrase.
Also considering only the first sense.
"""
try:
i_final=wn.morphy(re.sub(r"\W", r"",fr.i_token.split('_')[-1]))
j_final=wn.morphy(re.sub(r"\W", r"",fr.j_token.split('_')[-1]))
if i_final is None or j_final is None:
return "lowest_common_hypernym={}".format(False)
if _is_pronoun(i_final) or _is_pronoun(j_final):
return "lowest_common_hypernym={}".format(False)
i_synsets=wn.synsets(i_final)
j_synsets=wn.synsets(j_final)
lowest_common_hypernym=i_synsets[0].lowest_common_hypernyms(j_synsets[0])[0]
return "lowest_common_hypernym={}".format(lowest_common_hypernym)
except wn_error:
return "lowest_common_hypernym={}".format(False)
def et12(fr):
"""Returns the entity types of the two mentions, comma-separated."""
return "et12=[{},{}]".format(fr.i_entity_type,fr.j_entity_type)
def num_mentions_inbetween(fr):
"""Returns the number of other mentions between mention 1 and mention 2. Uses the entity_types dict."""
i_end=fr.i_offset_end
j_begin=fr.j_offset_begin
all_mention_tuples=entity_types[fr.article][int(fr.i_sentence)]
in_between_tuples=[tpl for tpl in all_mention_tuples if tpl[0]>i_end and tpl[0]<j_begin]
return "num_mentions_inbetween={}".format(len(in_between_tuples))
def num_words_inbetween(fr):
"""Number of words between m1 and m2. Relies on Julia's functions."""
return "num_words_inbetween={}".format(len(_get_words_in_between_(fr)))
def mention_overlap(fr):
"""
m1 contains m2 or m2 contains m1.
(This is true only 17 times in the training set. In all cases, the mentions are exactly the same,
so this feature will probably not be very useful.)
"""
result=(int(fr.i_offset_begin)<=int(fr.j_offset_begin) and int(fr.i_offset_end)>=int(fr.j_offset_end)) or \
(int(fr.i_offset_begin)>=int(fr.j_offset_begin) and int(fr.i_offset_end)<=int(fr.j_offset_end))
return "mention_overlap={}".format(result)
#### gazetter features start here; might be useless, but they're fun to try ####
def _is_rel_or_group(token):
result=False
for word in token.split('_'):
if word.lower() in RELATIONSHIPS_AND_GROUPS:
result=True
return result
def _is_country(token):
result=False
for word in token.split('_'):
if word.title() in COUNTRIES:
result=True
return result
def _is_nationality(token):
result=False
for word in token.split('_'):
if word.title() in NATIONALITIES:
result=True
return result
def _is_official(token):
result=False
for word in token.split('_'):
if word.lower() in OFFICIALS:
result=True
return result
def _is_profession(token):
result=False
for word in token.split('_'):
if word.lower() in PROFESSIONS:
result=True
return result
def _is_title(token):
result=False
for word in token.split('_'):
if word.lower() in TITLE_SET:
result=True
return result
def _is_possessive_pronoun(token):
result=False
for word in token.split('_'):
if word.lower() in POSSESSIVE_PRONOUNS:
result=True
return result
def poss_pronoun_per(fr):
"""
True if i is a possessive pronoun and j is PER.
From what I can see in the data, they always occur in that order and never in reverse order.
"""
result=_is_possessive_pronoun(fr.i_token) and fr.j_entity_type=='PER'
#print fr.i_token, fr.j_token, "poss_pronoun_per={}".format(result)
return "poss_pronoun_per={}".format(result)
def poss_pronoun_relword(fr):
"""
True if i is a possessive pronoun and j is a word for a family relationship or group.
From what I can see in the data, they always occur in that order and never in reverse order.
"""
result=_is_possessive_pronoun(fr.i_token) and _is_rel_or_group(fr.j_token)
#print fr.i_token, fr.j_token, "poss_pronoun_relword={}".format(result)
return "poss_pronoun_relword={}".format(result)
def per_relword(fr):
"""
True if i is a PER and j is a word for a family relationship or group,
or the other way around.
"""
result=(fr.i_entity_type=="PER" and _is_rel_or_group(fr.j_token)) or \
(fr.j_entity_type=="PER" and _is_rel_or_group(fr.i_token))
#print fr.i_token, fr.j_token, "per_relword={}".format(result)
return "per_relword={}".format(result)
def per_org(fr):
"""
True if i is PER and j is ORG or the other way around.
"""
result=(fr.i_entity_type=='PER' and fr.j_entity_type=='ORG') or \
(fr.i_entity_type=='ORG' and fr.j_entity_type=='PER')
#print fr.i_token, fr.j_token, "per_org={}".format(result)
return "per_org={}".format(result)
def per_nns(fr):
"""
True if i is PER and j is a plural noun.
The hope is that plural nouns will describes groups like "friends" and "neighbors."
Of course, that doesn't cover things like "team", "party", or "Beatles", but we can try it.
"""
j_pos=POS_SENTENCES[fr.article][fr.j_sentence][fr.j_offset_begin][1]
result=fr.i_entity_type=='PER' and j_pos=='NNS'
#print fr.i_token, fr.j_token, "per_nns={}".format(result)
return "per_nns={}".format(result)
def poss_title(fr):
"""
poss + profession OR title OR official
"""
result=_is_possessive_pronoun(fr.i_token) and (_is_profession(fr.j_token) or _is_official(fr.j_token) or _is_title(fr.j_token))
#print fr.i_token, fr.j_token, "poss_title={}".format(result)
return "poss_title={}".format(result)
def per_title(fr):
"""
per + profession OR title OR official
"""
result=fr.i_entity_type=='PER' and (_is_profession(fr.j_token) or _is_official(fr.j_token) or _is_title(fr.j_token))
#print fr.i_token, fr.j_token, "per_title={}".format(result)
return "per_title={}".format(result)
def nnp_title(fr):
"""
nnp + profession OR title OR official
or the other way around
"""
i_pos=POS_SENTENCES[fr.article][fr.i_sentence][fr.i_offset_begin][1]
j_pos=POS_SENTENCES[fr.article][fr.j_sentence][fr.j_offset_begin][1]
result=(i_pos.startswith("NNP") and (_is_profession(fr.j_token) or _is_official(fr.j_token) or _is_title(fr.j_token))) or \
(j_pos.startswith("NNP") and (_is_profession(fr.i_token) or _is_official(fr.i_token) or _is_title(fr.i_token)))
#print fr.i_token, fr.j_token, "nnp_title={}".format(result)
#print
return "nnp_title={}".format(result)
def et1_country(fr):
"""the entity type of M1 when M2 is a country name"""
result=False
if _is_country(fr.j_token):
result=fr.i_entity_type
#print fr.i_token, fr.j_token, "et1_country={}".format(result)
return "et1_country={}".format(result)
def country_et2(fr):
"""the entity type of M2 when M1 is a country name"""
result=False
if _is_country(fr.i_token):
result=fr.j_entity_type
#print fr.i_token, fr.j_token, "country_et2={}".format(result)
return "country_et2={}".format(result)
# dependency features
def et1_dw1(fr):
"""combination of the entity type and the dependent word(s) for M1"""
#print fr.article
#print DEPENDENCIES[fr.article].has_key(int(fr.i_sentence))
et1_dependencies=DEPENDENCIES[fr.article][int(fr.i_sentence)+1]
dep_list=[dep_word for (dep_index,dep_word),(gov_index,gov_word,dep_type) in et1_dependencies.items() if int(fr.i_offset_end)==gov_index]
#print fr.i_token, fr.j_token, "et1_dw1={},{}".format(fr.i_entity_type,dep_list)
#print et1_dependencies.items()
#print
return "et1_dw1={},{}".format(fr.i_entity_type,dep_list)
def h1_dw1(fr):
"""combination of the head word (=last word for now) and the dependent word(s) for M1"""
#print fr.article
#print DEPENDENCIES[fr.article].has_key(int(fr.i_sentence))
et1_dependencies=DEPENDENCIES[fr.article][int(fr.i_sentence)+1]
dep_list=[dep_word for (dep_index,dep_word),(gov_index,gov_word,dep_type) in et1_dependencies.items() if int(fr.i_offset_end)==gov_index]
#print fr.i_token, fr.j_token, "h1_dw1={},{}".format(fr.i_token.split('_')[-1],dep_list)
#print et1_dependencies.items()
#print
return "h1_dw1={},{}".format(fr.i_token.split('_')[-1],dep_list)
def et2_dw2(fr):
"""combination of the entity type and the dependent word(s) for M2"""
#print fr.article
#print DEPENDENCIES[fr.article].has_key(int(fr.i_sentence))
et2_dependencies=DEPENDENCIES[fr.article][int(fr.i_sentence)+1]
dep_list=[dep_word for (dep_index,dep_word),(gov_index,gov_word,dep_type) in et2_dependencies.items() if int(fr.j_offset_end)==gov_index]
#print fr.i_token, fr.j_token, "et2_dw2={},{}".format(fr.j_entity_type,dep_list)
#print et1_dependencies.items()
#print
return "et2_dw2={},{}".format(fr.j_entity_type,dep_list)
def h2_dw2(fr):
"""combination of the head word (=last word for now) and the dependent word(s) for M1"""
#print fr.article
#print DEPENDENCIES[fr.article].has_key(int(fr.i_sentence))
et2_dependencies=DEPENDENCIES[fr.article][int(fr.i_sentence)+1]
dep_list=[dep_word for (dep_index,dep_word),(gov_index,gov_word,dep_type) in et2_dependencies.items() if int(fr.j_offset_end)==gov_index]
#print fr.i_token, fr.j_token, "h2_dw2={},{}".format(fr.i_token.split('_')[-1],dep_list)
#print et1_dependencies.items()
#print
return "h2_dw2={},{}".format(fr.j_token.split('_')[-1],dep_list)
def _dep_path_to_root(offset_end):
"""Returns a list of token"""
pass
######################
# Keelan's functions #
######################
def rule_resolve(fs):
dcoref = COREF[fs.article]
for group in dcoref:
found_i = False
found_j = False
for referent in group:
if _coref_helper(referent, fs.sentence, fs.offset_begin, fs.offset_end, fs.i_cleaned):
found_i = True
if _coref_helper(referent, fs.sentence_ref, fs.offset_begin_ref, fs.offset_end_ref, fs.j_cleaned):
found_j = True
if found_i and found_j:
return "rule_resolve=True"
return "rule_resolve=False"
def _coref_helper(i, sentence, offset_begin, offset_end, cleaned):
"""heuristically, i[2] and i[3] will have a later index, especially i[3]"""
cleaned = cleaned.replace("_", " ")
return i[1] == sentence and \
i[2]-2 <= offset_begin <= i[2] and \
i[3]-3 <= offset_end <= i[3]
#####################
# Julia's functions #
#####################
def i_token(fr):
"""return the i_token or the antecedent if i is a pronoun"""
i_mention = (fr.i_token,int(fr.i_offset_begin),
int(fr.i_offset_end), fr.i_entity_type, int(fr.i_sentence))
antecedent = _get_antecedent_(i_mention,fr.article)[0]
token = "_".join(antecedent.split())
return "i_token={}".format(token)
def j_token(fr):
""" return the j_token or the antecedent if j is a pronoun"""
j_mention = (fr.j_token,int(fr.j_offset_begin),
int(fr.j_offset_end), fr.j_entity_type, int(fr.j_sentence))
antecedent = _get_antecedent_(j_mention,fr.article)[0]
token = "_".join(antecedent.split())
return "j_token={}".format(token)
def i_entity_type(fr):
"""return i_entity type"""
return "i_entity_type={}".format(fr.i_entity_type)
def j_entity_type(fr):
"""return j_i_entity_type"""
return "j_entity_type={}".format(fr.i_entity_type)
def _get_mentions_in_order_(fr):
"""return a pair of tuples. The first one corresponds to mention1 and its info,
the second one to mention2 and its info. i = mention1 and j=mention2 don't always hold"""
if int(fr.i_offset_begin)<int(fr.j_offset_begin):
mention1 = (fr.i_token,int(fr.i_offset_begin),
int(fr.i_offset_end), fr.i_entity_type, int(fr.i_sentence))
mention2 = (fr.j_token,int(fr.j_offset_begin),
int(fr.j_offset_end), fr.j_entity_type,int(fr.j_sentence))
else:
mention2 = (fr.i_token,int(fr.i_offset_begin),
int(fr.i_offset_end),fr.i_entity_type, int(fr.i_sentence))
mention1 = (fr.j_token,int(fr.j_offset_begin),
int(fr.j_offset_end), fr.j_entity_type, int(fr.j_sentence))
return (mention1,mention2)
def bow_mention1(fr):
"""return the words in mention2 eg. [George,Bush]"""
mention1 = _get_mentions_in_order_(fr)[0]
mention_token = _get_antecedent_(mention1,fr.article)[0]
token = mention_token.split()
return "bow_mention1={}".format(token)
def bow_mention2(fr):
"""return the words in mention2"""
mention2 = _get_mentions_in_order_(fr)[1]
mention_token = _get_antecedent_(mention2,fr.article)[0]
token = mention_token.split()
return "bow_mention2={}".format(token)
def _get_words_in_between_(fr):
"""return the words between m1 and m2"""
sent=POS_SENTENCES[fr.article][int(fr.i_sentence)]
mention1 = _get_mentions_in_order_(fr)[0]
mention2 = _get_mentions_in_order_(fr)[1]
first_token_index = int(mention1[2])
later_token_index = int(mention2[1])
w_in_between = sent[first_token_index:later_token_index]
return w_in_between
def first_word_in_between(fr):
"""return the first word between m1 and m2"""
words_in_between = _get_words_in_between_(fr)
if len(words_in_between)>=1:
first = words_in_between[0][0]
else:
first = "None"
return "first_word_in_between={}".format([first])
def last_word_in_between(fr):
"""return the last word between m1 and m2"""
words = _get_words_in_between_(fr)
if len(words)>=1:
last = words[len(words)-1][0]
else:
last = "None"
return "last_word_in_between={}".format([last])
def bow_tree(fr):
""" return words between m1 and m2 excluding the first and last words"""
words = _get_words_in_between_(fr)
if len(words)>=1:
words.pop()
if len(words)>=1:
words.pop()
children = [ParentedTree(w,["*"]) for w,pos in words]
bow_tree = ParentedTree("BOW",children)
return bow_tree
def first_word_before_m1(fr):
"""return first word before m1"""
mention1 = _get_mentions_in_order_(fr)[0]
sent=POS_SENTENCES[fr.article][int(mention1[4])]
return "first_word_before_m1={}".format([sent[int(mention1[1])-1][0]])
def first_word_before_m2(fr):
"""return first word before m2"""
mention2 = _get_mentions_in_order_(fr)[1]
sent=POS_SENTENCES[fr.article][int(mention2[4])]
return "first_word_before_m2={}".format([sent[int(mention2[1])-1][0]])
def second_word_before_m1(fr):
"""return second word before m1"""
mention1 = _get_mentions_in_order_(fr)[0]
sent=POS_SENTENCES[fr.article][int(mention1[4])]
try:
return "second_word_before_m1={}".format([sent[int(mention1[1])-2][0]])
except IndexError:
return "second_word_before_m1=[None]"
def second_word_before_m2(fr):
"""return second word before m2"""
mention2 = _get_mentions_in_order_(fr)[1]
sent=POS_SENTENCES[fr.article][int(mention2[4])]
try:
return "second_word_before_m2={}".format([sent[int(mention2[1])-2][0]])
except IndexError:
return "second_word_before_m2=[None]"
def head_of_m1_coref(fr):
"""return the head of the NP in which M1 occurs"""
mention1 = _get_mentions_in_order_(fr)[0]
pos=POS_SENTENCES[fr.article][mention1[4]][mention1[1]][1]
if pos == "PRP": #don't check antecedent with possessive pronouns, just personal pronouns
antecedent = _get_antecedent_(mention1,fr.article)
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][antecedent[3]]
m1_tuple = s_tree.leaf_treeposition(antecedent[1])
else:
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][mention1[4]]
m1_tuple = s_tree.leaf_treeposition(mention1[1])
parent = s_tree[m1_tuple[0:-2]]
return "head_of_m1_coref={}".format([_find_head_of_tree_(parent)])
def head_of_m2_coref(fr):
"""return the head of the NP in which M2 occurs"""
mention2 = _get_mentions_in_order_(fr)[1]
pos=POS_SENTENCES[fr.article][mention2[4]][mention2[1]][1]
if pos == "PRP": #pronoun, do everyting for antecedent
antecedent = _get_antecedent_(mention2,fr.article)
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][antecedent[3]]
m2_tuple = s_tree.leaf_treeposition(antecedent[1])
else:
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][mention2[4]]
m2_tuple = s_tree.leaf_treeposition(mention2[1])
parent = s_tree[m2_tuple[0:-2]]
return "head_of_m2_coref={}".format([_find_head_of_tree_(parent)])
def _head_of_m1_(fr):
"""return the head of the NP in which M1 occurs"""
mention1 = _get_mentions_in_order_(fr)[0]
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][mention1[4]]
m1_tuple = s_tree.leaf_treeposition(mention1[1])
parent = s_tree[m1_tuple[0:-2]]
return _find_head_of_tree_(parent)
def _head_of_m2_(fr):
"""return the head of the NP in which M1 occurs"""
mention2 = _get_mentions_in_order_(fr)[1]
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][mention2[4]]
m1_tuple = s_tree.leaf_treeposition(mention2[1])
parent = s_tree[m1_tuple[0:-2]]
return _find_head_of_tree_(parent)
def same_head(fr):
"""return whether both entities have the same head"""
mention1_head = head_of_m1_coref(fr).split("=")[1]
mention2_head = head_of_m2_coref(fr).split("=")[1]
return "same_head={}".format(mention1_head == mention2_head)
def first_np_head_in_between(fr):
"""
if there are other NP between both entities,
return the head of the first one
"""
heads = boh_np_tree(fr)
if len(heads)>=1:
head = heads[0].node
else:
head = None
return "first_np_head_in_between={}".format([head])
def first_head_in_between(fr):
"""
if there are other phrases between both entities,
return the head of the first one
"""
heads = boh_tree(fr)
if len(heads)>=1:
head = heads[0].node
else:
head = None
return "first_head_in_between={}".format([head])
def last_np_head_in_between(fr):
"""
if there are other NP phrases in-between both entities,
return the head of the last one
"""
heads = boh_np_tree(fr)
if len(heads)>=1:
head = heads[-1].node
else:
head = None
return "last_np_head_in_between={}".format([head])
def last_head_in_between(fr):
"""
if there are other phrases in_between both entities,
return the head of the last one
"""
heads = boh_tree(fr)
if len(heads)>=1:
head = heads[-1].node
else:
head = None
return "last_head_in_between={}".format([head])
def boh_np_tree(fr):
"""
return a bag of heads tree with the heads of the NPs in_between
mention1 and mention2
"""
mention1= _get_mentions_in_order_(fr)[0]
mention2 = _get_mentions_in_order_(fr)[1]
head_of_m1= _head_of_m1_(fr)
head_of_m2= _head_of_m2_(fr)
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][mention1[4]]
i = mention1[1]+1
heads = []
while i < mention2[1]:
word_tuple = s_tree.leaf_treeposition(i)
pos_index = word_tuple[-2]
parent = s_tree[word_tuple[0:-2]]
head = None
sum = 0
for j,child in enumerate(parent[pos_index:]):
if child.node in ['NN', 'NNS', 'NNP', 'NNPS', 'WHNP', "PRP"] and \
child[0] != head_of_m1 and child[0]!= head_of_m2:
head = child[0]
sum = j
if isinstance(head,unicode):
heads.append(head)
i+=sum + 1
children = [ParentedTree(w,["*"]) for w in heads]
boh_tree = ParentedTree("BOH-NPs",children)
return boh_tree
def boh_tree(fr):
"""Return a flatten tree with all heads between m1 and m2"""
mention1= _get_mentions_in_order_(fr)[0]
mention2 = _get_mentions_in_order_(fr)[1]
head_of_m1= _head_of_m1_(fr)
head_of_m2= _head_of_m2_(fr)
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][mention1[4]]
i = mention1[1]+1
heads = []
while i < mention2[1]:
word_tuple = s_tree.leaf_treeposition(i)
pos_index = word_tuple[-2]
parent = s_tree[word_tuple[0:-2]]
head = None
sum = 0
for j,child in enumerate(parent[pos_index:]):
if parent.node in phrase_heads.keys():
if parent.node in phrase_heads.keys():
candidate_head = child.node in phrase_heads[parent.node]
not_head_of_m1 = child[0] != head_of_m1
not_head_of_m2 = child[0] != head_of_m2
if not (isinstance(child.right_sibling(), ParentedTree) and
child.right_sibling().node in phrase_heads[parent.node]):
if candidate_head and not_head_of_m1 and not_head_of_m2:
head = child[0]
sum = j
if isinstance(head,unicode):
heads.append(head)
i+=sum +1
children = [ParentedTree(w,["*"]) for w in heads]
boh_tree = ParentedTree("BOH",children)
return boh_tree
def first_np_head_before_m1(fr):
"""
return the head of the first NP before mention1
"""
mention1= _get_mentions_in_order_(fr)[0]
head_of_m1= _head_of_m1_(fr)
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][mention1[4]]
i = 0
head = None
while i < mention1[1]:
word_tuple = s_tree.leaf_treeposition(i)
pos_index = word_tuple[-2]
parent = s_tree[word_tuple[0:-2]]
sum = 0
for j,child in enumerate(parent[pos_index:]):
if child.node in ['NN', 'NNS', 'NNP', 'NNPS'] and \
child[0] != head_of_m1:
head = child[0]
sum = j
i+=sum + 1
return "first_np_head_before_m1={}".format([head])
def first_head_before_m1(fr):
"""
return the head of the first phrase before mention1
"""
mention1= _get_mentions_in_order_(fr)[0]
head_of_m1= _head_of_m1_(fr)
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][mention1[4]]
i = 0
head = None
while i < mention1[1]:
word_tuple = s_tree.leaf_treeposition(i)
pos_index = word_tuple[-2]
parent = s_tree[word_tuple[0:-2]]
sum = 0
for j,child in enumerate(parent[pos_index:]):
if parent.node in phrase_heads.keys():
if child.node in phrase_heads[parent.node] and \
child[0] != head_of_m1:
head = child[0]
sum = j
i+=sum + 1
return "first_head_before_m1={}".format([head])
def second_np_head_before_m1(fr):
"""return the second to last NP head before m1"""
mention1= _get_mentions_in_order_(fr)[0]
head_of_m1= _head_of_m1_(fr)
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][mention1[4]]
first_head_before_m1 = eval(first_np_head_before_m1(fr).split("=")[1])[0]
i = 0
head = None
while i < mention1[1]:
word_tuple = s_tree.leaf_treeposition(i)
pos_index = word_tuple[-2]
parent = s_tree[word_tuple[0:-2]]
sum = 0
for j,child in enumerate(parent[pos_index:]):
if child.node in ['NN', 'NNS', 'NNP', 'NNPS'] and \
child[0] != head_of_m1 and child[0]!=first_head_before_m1:
head = child[0]
sum = j
i+=sum + 1
return "second_np_head_before_m1={}".format([head])
def second_head_before_m1(fr):
"""return the second to last head before m1"""
mention1= _get_mentions_in_order_(fr)[0]
head_of_m1= _head_of_m1_(fr)
s_tree=SYNTAX_PARSE_SENTENCES[fr.article][mention1[4]]
first_before_m1 = eval(first_head_before_m1(fr).split("=")[1])[0]
i = 0
head = None
while i < mention1[1]:
word_tuple = s_tree.leaf_treeposition(i)
pos_index = word_tuple[-2]
parent = s_tree[word_tuple[0:-2]]
sum = 0
for j,child in enumerate(parent[pos_index:]):
if parent.node in phrase_heads.keys():
if child.node in phrase_heads[parent.node] and \
child[0] != head_of_m1 and child[0]!=first_before_m1:
head = child[0]
sum = j
i+=sum+1
return "second_head_before_m1={}".format([head])
def second_np_head_before_m2(fr):
"""
return the second to last NP head before m2
"""
heads = boh_np_tree(fr)
if len(heads)>=2:
head = heads[-2].node
return "second_np_head_before_m2={}".format([head])
else:
return "second_np_head_before_m2=None"
def second_head_before_m2(fr):
"""
return the second to last head before m2
"""
heads = boh_tree(fr)
if len(heads)>=2:
head = heads[-2].node
return "second_head_before_m2={}".format([head])
else:
return "second_head_before_m2=None"
def no_words_in_between(fr):
"""return whether there are words between m1 and m2"""
return "no_words_in_between={}".format(len(_get_words_in_between_(fr))==0)
def no_phrase_in_between(fr):
"""return whether there are phrases between both entities"""
no_phrase = len(boh_tree(fr).leaves()) == 0
return "no_phrase_in_between={}".format(no_phrase)
def lp_tree(fr):
"""return a flatten tree with the nodes of the phrases in the path from m1
to m2 (duplicates removed)"""
s_tree = SYNTAX_PARSE_SENTENCES[fr.article][int(fr.i_sentence)]
lwca=_get_lowest_common_ancestor_(fr,s_tree)
mention1 = _get_mentions_in_order_(fr)[0]
mention2 = _get_mentions_in_order_(fr)[1]
left_tree = s_tree[s_tree.leaf_treeposition(int(mention1[1]))[0:-1]]
right_tree= s_tree[s_tree.leaf_treeposition(int(mention2[2])-1)[0:-1]]
nodes_left_branch = []
nodes_right_branch=[]
curr_tree = left_tree
while curr_tree!=lwca.parent():
if not (len(nodes_left_branch)>0 and nodes_left_branch[-1]== curr_tree.node):
nodes_left_branch.append(curr_tree.node)
curr_tree = curr_tree.parent()
curr_tree = right_tree
while curr_tree!=lwca:
if not (len(nodes_right_branch)>0 and nodes_right_branch[-1]== curr_tree.node):
nodes_right_branch.append(curr_tree.node)
curr_tree = curr_tree.parent()
nodes_right_branch.reverse()
path = nodes_left_branch + nodes_right_branch
children = [ParentedTree(node,["*"]) for node in path]
label_path = ParentedTree("LP",children)
return label_path
def lp_head_tree(fr):
"""
return a flatten tree with the nodes of the phrases in the path from m1
to m2 (duplicates removed) augmented with the head word of the lowest
common ancestor
"""
s_tree = SYNTAX_PARSE_SENTENCES[fr.article][int(fr.i_sentence)]
lwca=_get_lowest_common_ancestor_(fr,s_tree)
mention1 = _get_mentions_in_order_(fr)[0]
mention2 = _get_mentions_in_order_(fr)[1]
left_tree = s_tree[s_tree.leaf_treeposition(int(mention1[1]))[0:-1]]
right_tree= s_tree[s_tree.leaf_treeposition(int(mention2[2])-1)[0:-1]]
nodes_left_branch = []
nodes_right_branch=[]
curr_tree = left_tree
while curr_tree!=lwca:
if not (len(nodes_left_branch)>0 and nodes_left_branch[-1].node== curr_tree.node):
nodes_left_branch.append(ParentedTree(curr_tree.node,["*"]))
curr_tree = curr_tree.parent()
if nodes_left_branch[-1].node == lwca.node: nodes_left_branch.pop()
nodes_left_branch.append(ParentedTree(lwca.node,[_find_head_of_tree_(lwca)])) #add head of lwca
curr_tree = right_tree
while curr_tree!=lwca:
if not (len(nodes_right_branch)>0 and nodes_right_branch[-1].node== curr_tree.node):
nodes_right_branch.append(ParentedTree(curr_tree.node,["*"]))
curr_tree = curr_tree.parent()
nodes_right_branch.reverse()
path = nodes_left_branch + nodes_right_branch
label_path = ParentedTree("LP-head",path)
return label_path
#i'm writing this right now....
#def _get_antecedent_(mention_tuple):
def _get_antecedent_(mention_tuple, article):
"""If the token is a pronound, return its antecedent. Else,
return the pronoun.
Return as (token,start,end,sentence)"""
# return token
target_group = None
mention_referent = None
if _is_pronoun(mention_tuple[0]):
dcoref = COREF[article]
for group in dcoref:
for referent in group:
if referent[1] == mention_tuple[4] and referent[2] == mention_tuple[1]:
target_group = group
break
if isinstance(target_group,set):
break
try:
for referent in target_group:
if referent == mention_referent:
continue
else:
text = referent[0]
sent = referent[1]
end = referent[3]-1
start = referent[2]
text_tag = POS_SENTENCES[article][sent][end][1]
if text_tag in ["NNP","NNPS"]:
antecedent = (text,start,end,sent)
break
elif text_tag in ["NN","NNS"]:
antecedent = (text,start,end,sent)
return antecedent
except TypeError: #sometimes sentence indices in COREF and our data don't match
return (mention_tuple[0], mention_tuple[1], mention_tuple[2],mention_tuple[4])
else:
return (mention_tuple[0], mention_tuple[1], mention_tuple[2],mention_tuple[4])
def path_enclosed_tree(fr):
if fr.i_sentence!=fr.j_sentence:
return ParentedTree("None",["*"]) #just in case
else:
s_tree = SYNTAX_PARSE_SENTENCES[fr.article][int(fr.i_sentence)]
return _generate_enclosed_tree(fr,s_tree)
def path_enclosed_tree_augmented(fr):
if fr.i_sentence!=fr.j_sentence:
return ParentedTree("None",["*"]) #just in case
else:
s_tree = ParentedTree.convert(AUGMENTED_TREES[fr.article][int(fr.i_sentence)])
return _generate_enclosed_tree(fr,s_tree)
def _generate_enclosed_tree(fr,s_tree):
"""****MONSTER FUNCTION!!!!****
Return the path enclosed tree between m1 and m2 as PatentedTree
The path enclosed tree is the smallest common
sub-tree including the two entities [JB:but not necessary the lowest_common_ancestor]. In other
words, the sub-tree is enclosed by the shortest
path linking the two entities in the parse tree (this
path is also commonly-used as the path tree feature in the feature-based methods)
[Zhang et al. 2006]
[JB]:
That is, the path enclosed tree includes mention1, and every branch to the right of it, until
mention2. In this function, the path enclosed tree is built in the following way:
the left branch of it includes mention1 and branches to the right of it that still are on the left child
of the lowest common ancestor. The right branch of the path-enclosed tree includes mention2, and the
branches to the left of it that are on the right child of the lowest common ancestor.
Both branches are merged in one tree, with the lowest_common_ancestor node, yielding the
path enclosed tree.
"""
mention1 = _get_mentions_in_order_(fr)[0]
mention2= _get_mentions_in_order_(fr)[1]
first_entity_index = int(mention1[1])
later_entity_index = int(mention2[2])-1
first_token = mention1[0]
later_token = mention2[0]
i_tuple = s_tree.leaf_treeposition(first_entity_index)
j_tuple = s_tree.leaf_treeposition(later_entity_index)
first_tree = s_tree[i_tuple[0:-1]]