forked from dbamman/book-nlp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
adjectives.py
1634 lines (1342 loc) · 54.5 KB
/
adjectives.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 tuple_methods import *
from wordnet_methods import *
from string_methods import *
from print_methods import *
from dict_methods import *
##############################################################################
##############################################################################
##############################################################################
#################### LIST METHODS ############################################
##############################################################################
##############################################################################
##############################################################################
##############################################################################
def bigger(li1, li2):
return li1 if len(li1) > len(li2) else li2
def parent(parse_tree, tup, i):
return parse_tree[i][parent_index(tup)]
def is_ancestor(parse_tree, tup1, tup2, i):
if parent_index(tup1) == -1:
return False
elif is_parent(tup1, tup2):
return True
else:
return is_ancestor(parse_tree, parent(parse_tree, tup1, i), tup2, i)
def has_VB_in_path(parse_tree, tup1, tup2, i):
if not is_ancestor(parse_tree, tup1, tup2, i):
return False
elif tup1 == tup2:
return False
elif is_pos_of(tup1, "VB"):
return True
else:
return has_VB_in_path(parse_tree,
parent(parse_tree, tup1, i),
tup2,
i)
def is_a_be_or_have(tup, bes, haves):
return is_norm_word_of_any(tup, (bes+haves))
def is_g_nibling(parse_tree, tup1, tup2, i):
return (is_ancestor(parse_tree, tup1, parent(parse_tree, tup2, i), i) and
not is_ancestor(parse_tree, tup1, tup2, i))
def count_people(people):
return len(set([char_id(tup) for tup in people]))
def get_people(parse_tree, i):
return [tup for tup in parse_tree[i] if is_person(tup)]
def get_nsubj_people(parse_tree, i):
people = sorted([tup for tup in parse_tree[i] if
is_dependency_of(tup, "nsubj") and
is_person(tup)])
return people
def get_adjectives(parse_tree, i):
return [tup for tup in parse_tree[i] if is_adjective(tup)]
def get_nmod_tup(parse_tree, word, a, li, i):
return ([tup for tup in parse_tree[i] if
is_lower_word_equal(tup, word) and
(any(is_parent(tup, x) for x in li) or
is_parent(tup, a))])
def is_subset(li1, li2):
return set(li1) <= set(li2)
def is_substring(s1, s2):
return s1 in s2
def superset(li1, li2):
if is_subset(li1, li2):
return li2
else:
return li1
def filter_duplicates(li):
return sorted(list(set(li)))
def all_words_in_between(parse_tree, li, i):
if li:
start = li[0][0]
end = li[-1][0]+1
index_range = range(start, end)
list_indices = [index(word) for word in li]
if [r_index for r_index in index_range if r_index not in list_indices]:
return sorted([word for word in parse_tree[i]
if index(word) in index_range])
return li
def remove_stop_words(li, stopwords):
if li:
if li[0][5] in stopwords:
li = li[1:]
if li:
if li[-1][5] in stopwords:
li = li[:-1]
return li
def find_parent_and_prev_negs(parse_tree, adj_list, p, a, i, people, conjunctions):
def parent_negs(parse_tree, li, i):
return ([tup for tup in parse_tree[i] if
is_dependency_equal(tup, "neg") and
any(is_parent(tup, x) for x in li) and
tup not in li])
def prev_negs(parse_tree, li, p, a, i, people, conjunctions):
def not_first_person(tup, li):
def prev_person_is_same(li, tup):
if previous_item_exists(li, tup):
return char_id(tup) == char_id(previous_item(li, tup))
return False
if tup in li:
return (li.index(tup) != 0 and
not prev_person_is_same(li, tup))
return False
return ([tup for tup in parse_tree[i] if
is_dependency_equal(tup, "neg")
and parse_tree[i].index(tup) < parse_tree[i].index(a)
and no_conj_in_between(parse_tree, tup, a, p, i, conjunctions) and
no_punct_in_between(parse_tree, tup, a, p, i, conjunctions) and
tup not in li if
not not_first_person(p, people)])
return (parent_negs(parse_tree, adj_list, i) +
prev_negs(parse_tree, adj_list, p, a, i, people, conjunctions))
def is_near_word_JJ_or_RB(parse_tree, tup, i, k):
if near_word_exists(parse_tree, tup, i, k):
return is_pos_of_any(near_word(parse_tree, tup, i, k), ["JJ", "RB"])
return False
def is_prev_word_a_be_or_have(parse_tree, tup, i, bes, haves):
if prev_word_exists(parse_tree, tup, i):
return is_a_be_or_have(prev_word(parse_tree, tup, i), bes, haves)
return False
def is_prev_of_id(parse_tree, tup1, tup2, i):
if prev_word_exists(parse_tree, tup2, i):
return char_id(prev_word(parse_tree, tup2, i)) == char_id(tup1)
return False
def next_word_had_or_is_conditions(parse_tree, p, i, bes, haves):
if next_word_exists(parse_tree, p, i):
next = next_word(parse_tree, p, i)
if is_word_equal(next, ["is", "had", "has"]):
return is_root(next) or not is_pos_of(parent(parse_tree, next, i), "VB")
return False
def is_next_of_id(parse_tree, tup1, tup2, i):
if next_word_exists(parse_tree, tup1, i):
return char_id(next_word(parse_tree, tup1, i)) == char_id(tup2)
return False
def is_next_word_conj(parse_tree, tup, i, conjunctions):
if next_word_exists(parse_tree, tup, i):
return word(next_word(parse_tree, tup, i)) in conjunctions
return False
def is_next_word_comma(parse_tree, tup, punctuation, i):
if next_word_exists(parse_tree, tup, i):
return is_word_equal(next_word(parse_tree, tup, i), ",")
return False
def is_next_to_next_word_another_person(parse_tree, tup, p, i):
if near_word_exists(parse_tree, tup, i, 2):
id = char_id(near_word(parse_tree, tup, i, 2))
return id != -1 and id != char_id(p)
return False
def is_prev_word_a_person(parse_tree, tup, i):
if prev_word_exists(parse_tree, tup, i):
return char_id(prev_word(parse_tree, tup, i)) != -1
return False
def find_nearby_vbs(parse_tree, tup, i, vb_strings):
vibs = []
if prev_word_exists(parse_tree, tup, i):
prev = prev_word(parse_tree, tup, i)
if (is_pos_equal_any(prev, vb_strings)):
vibs.append(prev)
if next_word_exists(parse_tree, tup, i):
next = next_word(parse_tree, tup, i)
if (is_pos_equal_any(next, vb_strings)):
vibs.append(next)
return vibs
def add_prevs_of_negs(parse_tree, negs, i):
prevs = []
for neg in negs:
if prev_word_exists(parse_tree, neg, i):
prev = prev_word(parse_tree, neg, i)
if (has_common_parent(prev, neg) and
not is_dependency_equal(prev, "punct")):
prevs.append(prev)
return negs + prevs
def have_condition(parse_tree, p, i, conjunctions):
return [tup for tup in parse_tree[i] if is_word_equal(tup, "have") and
(is_g_nibling(parse_tree, tup, p, i) or is_ancestor(parse_tree, tup, p, i)) and
is_pos_of(parent(parse_tree, p, i), "VB") and
no_punct_in_between(parse_tree, tup, p, p, i, conjunctions)]
def surrounding_words_linked(parse_tree, tup, i):
return (
(
is_near_word_JJ_or_RB(parse_tree, tup, i, -1) and
is_near_word_JJ_or_RB(parse_tree, tup, i, 1)
) or
(
have_same_dependency(prev_word(parse_tree, tup, i),
next_word(parse_tree, tup, i))
)
)
def descriptive_comma(parse_tree, tup, p, i, conjunctions):
return (
is_word_equal(tup, ",") and
(is_dependency_of_any(next_word(parse_tree, tup, i), ["amod", "conj", "advcl"]) or
(is_word_equal_any(next_word(parse_tree, tup, i),
conjunctions) and
is_near_word_JJ_or_RB(parse_tree, tup, i, 2)
) or
is_next_of_id(parse_tree, tup, p, i)
)
)
def remove_invalid_splitters(parse_tree, li, p, i, conjunctions):
return [tup for tup in li if
not surrounding_words_linked(parse_tree, tup, i) and
not descriptive_comma(parse_tree, tup, p, i, conjunctions)]
def remove_linking_splitters(parse_tree, li, p, i, conjunctions):
return [tup for tup in li if
not surrounding_words_linked(parse_tree, tup, i)]
def find_as(parse_tree, li, i):
def is_there_one_as(parse_tree, li, i):
as_words = [tup for tup in li if is_word_equal(tup, "as")]
if len(as_words) == 1:
return parse_tree[i].index(as_words[0])
return -1
first_as = is_there_one_as(parse_tree, li, i)
if first_as != -1:
second_as = is_there_one_as(parse_tree, parse_tree[i][first_as+1:], i)
if second_as != -1:
second_as_tup = parse_tree[i][second_as]
return [second_as_tup, parent(parse_tree, second_as_tup,i)]
return []
def find_punctuations(parse_tree, li, i):
#assumes list is sorted
indices = range(li[0][0], li[-1][0])
puncts = ([tup for tup in parse_tree[i] if
is_dependency_equal(tup, "punct") and
index(tup) in indices])
if not (len(puncts) == 2 and is_same_norm_word(puncts[0], puncts[1])):
return puncts
else:
return []
##############################################################################
##############################################################################
##############################################################################
#################### ADJECTIVE METHODS #######################################
##############################################################################
##############################################################################
##############################################################################
##############################################################################
def has_direct_dependency(parse_tree, a, i):
return not is_dependency_of_any(a, ["det", "case"])
def find_advmods_and_dets(parse_tree, a, i, bes, haves):
return ([tup for tup in parse_tree[i] if
is_dependency_equal_any(tup, ["advmod", "det"]) and
(has_common_parent(tup, a) or
is_parent(tup, a)) and
tup != a and
is_prev_word_a_be_or_have(parse_tree, tup, i, bes, haves)])
def find_RBs(parse_tree, a, i):
return ([tup for tup in parse_tree[i] if
is_adverb(tup) and
is_parent(tup, a)])
def find_valid_adj_parents(parse_tree, p, a, i, conjunctions):
def add_parent_of_adj_conds(parse_tree, a, i):
return is_dependency_of_any(a,
["nmod:", "conj:", "xcomp",
"dobj", "amod", "advmod"]
)
parent_tup = parent(parse_tree, a, i)
if (add_parent_of_adj_conds(parse_tree, a, i) and
no_punct_in_between(parse_tree, a, parent_tup, p, i, conjunctions) and
no_conj_in_between(parse_tree, a, parent_tup, p, i, conjunctions)):
return [parent_tup]
else:
return []
def find_preps(parse_tree, a, i):
return ([tup for tup in parse_tree[i] if
is_pos_equal(tup, "IN") and
not is_dependency_equal(tup, "mark") and
is_parent(tup, a)])
def get_trait_advmods(parse_tree, a, i):
return ([tup for tup in parse_tree[i] if
is_dependency_equal(tup, "advmod") and
is_parent(tup, a) and
tup != a])
def find_adj_advcls(parse_tree, a, i):
return ([tup for tup in parse_tree[i] if
is_dependency_equal(tup, "advcl") and
is_adjective(tup) and
(is_parent(tup, a) or
has_common_parent(tup, a)) and
tup != a])
def find_adj_acl_relcls(parse_tree, p, i):
return ([tup for tup in parse_tree[i] if
is_dependency_equal(tup, "acl:relcl") and
is_adjective(tup) and
(is_parent(tup, p) or
has_common_parent(tup, p)) and
tup != p])
def check_for_in(parse_tree, p, a, i, conjunctions):
def get_in_words(parse_tree, p, a, i):
return ([tup for tup in parse_tree[i] if
is_word_equal(tup, "in") and
parse_tree[i].index(tup) < parse_tree[i].index(a) and
parse_tree[i].index(p) < parse_tree[i].index(tup)])
in_words = get_in_words(parse_tree, p, a, i)
if in_words:
word = sorted(in_words)[-1]
return (no_conj_in_between(parse_tree, word,
a, p, i, conjunctions) and
no_punct_in_between(parse_tree, word, a, p, i, conjunctions))
return False
def no_punct_in_between(parse_tree, tup1, tup2, p, i, conjunctions):
return not (remove_invalid_splitters(parse_tree,
find_punctuations(
parse_tree,
all_words_in_between
(
parse_tree,
sorted([tup1, tup2]),
i
),
i
),
p,
i,
conjunctions
)
)
def no_conj_in_between(parse_tree, tup1, tup2, p, i, conjunctions):
def get_conjunctions(parse_tree, li, i, conjunctions):
#assumes list is sorted
indices = range(li[0][0], li[-1][0])
return ([tup for tup in parse_tree[i] if
word(tup) in conjunctions
and index(tup) in indices])
return not (remove_linking_splitters(parse_tree,
get_conjunctions(
parse_tree,
all_words_in_between
(
parse_tree,
sorted([tup1, tup2]),
i
),
i,
conjunctions
),
p,
i,
conjunctions
)
)
def near_word_exists(parse_tree, tup, i, k):
return (index(tup) + k >= 0 and index(tup) + k < len(parse_tree[i]))
def next_word_exists(parse_tree, tup, i):
return near_word_exists(parse_tree, tup, i, 1)
def prev_word_exists(parse_tree, tup, i):
return near_word_exists(parse_tree, tup, i, -1)
def near_word(parse_tree, tup, i, k):
return parse_tree[i][index(tup) + k]
def next_word(parse_tree, tup, i):
return parse_tree[i][index(tup) + 1]
def prev_word(parse_tree, tup, i):
return parse_tree[i][index(tup) - 1]
def words_between_adj_and_parent(parse_tree, a, i):
if index(a) < parent_index(a):
indices = range(index(a) + 1, parent_index(a) + 1)
else:
indices = range(parent_index(a), index(a))
tups = [tup for tup in parse_tree[i] if index(tup) in indices]
return sorted(tups)
def punct_split(parse_tree, li, p, a, i, conjunctions):
bigl = []
phrase = []
last = 0
puncts = remove_invalid_splitters(parse_tree,
[word for word in li if is_dependency_equal(word, "punct")],
p, i,
conjunctions)
for punct in puncts:
if True:
bigl.append(li[last:li.index(punct)])
last = li.index(punct) + 1
bigl.append(li[last:])
for sublist in bigl:
if (a in sublist or
sublist_follows_p(parse_tree, p, i, sublist) or
is_first_word_person(sublist, p)
):
phrase += sublist
return phrase
def is_first_word_person(li, p):
if li :
return char_id(li[0]) == char_id(p)
return False
def sublist_follows_p(parse_tree, p, i, sublist):
if sublist and p and next_word_exists(parse_tree, p, i):
return next_word(parse_tree, p, i) == sublist[0]
return False
def sibling_states_conditions(parse_tree, p, a, i):
return (
not (is_dependency_of(a, "dep")) and
not (is_dependency_of(a, "nmod") and
not is_adjective(parent(parse_tree, a, i))
) and
not ([tup for tup in parse_tree[i] if
is_dependency_of(tup, "nsubj") and
is_parent(tup, a)])
)
def find_conj_and_nmod_words(parse_tree, a, i):
def get_nmod_children(parse_tree, a, i):
return (
[tup for tup in parse_tree[i] if
is_dependency_of(tup, "nmod:") and
(
is_parent(tup, a) or
has_common_parent(tup, a) or
tup == a or
tup == parent(parse_tree, a, i))]
)
def get_conj_children(parse_tree, a, i):
return (
[tup for tup in parse_tree[i] if
is_dependency_of(tup, "conj:") and
not (
is_pos_equal(tup, "VBD") or
is_pos_equal(tup, "VB")
) and
(
is_parent(tup, a) or
has_common_parent(tup, a) or
tup == a or
tup == parent(parse_tree, a, i))]
)
nmod_list = []
conj_list = []
def get_words(parse_tree, li, a, string, i):
tuples = []
for tup in li:
word = dep(tup).split(string)[1].lower()
if '_' in word:
split_word = word.split("_")
first_mods = get_nmod_tup(parse_tree, split_word[0], a, li, i)
tuples += first_mods
tuples += get_nmod_tup(parse_tree, split_word[1], a, first_mods,
i)
else:
nmod_tup = get_nmod_tup(parse_tree, word, a, li, i)
if nmod_tup:
tuples += nmod_tup
else:
li.remove(tup)
return tuples
nmod_children = get_nmod_children(parse_tree, a, i)
nmod_list += (get_words(parse_tree, nmod_children, a, "nmod:", i) +
nmod_children)
conj_children = get_conj_children(parse_tree, a, i)
conj_list += (get_words(parse_tree, conj_children, a, "conj:", i) +
conj_children)
return nmod_list + conj_list
##############################################################################
##############################################################################
##############################################################################
#################### ADJ_LIST METHODS ########################################
##############################################################################
##############################################################################
##############################################################################
##############################################################################
def find_latter_preps_and_coord_conjs(parse_tree, adj_list, p, a, i):
def is_present_before(parse_tree, tup, li, i):
return parse_tree[i].index(tup) < li[0][0]
def ins(parse_tree, p, a, i):
return ([tup for tup in parse_tree[i] if
is_pos_equal(tup, "IN")
and not is_dependency_equal(tup, "mark") and
not is_present_before(parse_tree, tup, sorted([p, a]), i) and
(has_common_parent(tup, a) or
is_parent(tup, a))])
def ccs(parse_tree, adj_list, p, a, i):
return ([tup for tup in parse_tree[i] if
is_pos_equal(tup, "CC") and
not is_present_before(parse_tree, tup, sorted([p, a]), i) and
(any(is_parent(tup, x) for x in adj_list) or
is_parent(tup, a) or has_common_parent(tup, a))])
return ins(parse_tree, p, a, i) + ccs(parse_tree, adj_list, p, a, i)
def nsubj_children(parse_tree, p, a, i, conjunctions):
return (
[
tup for tup in parse_tree[i] if
is_dependency_of(tup, "nsubj") and
(is_parent(tup, a) or
(
is_pos_of(parent(parse_tree, a, i), "VB") and
is_related(tup, parent(parse_tree, a, i))
and not (no_punct_in_between(parse_tree, p, tup, p, i, conjunctions)
and parse_tree[i].index(tup) > parse_tree[i].index(p))
)
) and
char_id(tup) != -1 and
(char_id(tup) != char_id(p) and
not (
(is_dependency_of(tup, "conj") and is_parent(tup, p)) or
(is_dependency_of(p, "conj") and is_parent(p, tup))
)
)
]
)
def nsubj_parents(parse_tree, p, a, i):
return ([tup for tup in parse_tree[i] if
is_dependency_of(tup, "nsubj") and
is_parent(p, tup) and
char_id(tup) != char_id(p)])
def tup_is_before(parse_tree, li, tup, i):
return parse_tree[i].index(tup) < parse_tree[i].index(sorted(li)[0])
def recursively_add_all_dep_and_pos_children(parse_tree, dep_strings,
pos_strings, adj_list, i):
def recursively_add_all_dep_and_pos_children(parse_tree, dep_strings, pos_strings, li, filled_li, i):
def get_the_dep_and_pos_children(parse_tree, dep_strings, pos_strings, li, filled_li, i, deps_and_pos):
def add_all_dep_and_pos_children(parse_tree, dep_strings, pos_strings, li, filled_li, i):
return ([tup for tup in parse_tree[i] if
(is_dependency_of_any(tup, dep_strings) or
is_pos_of_any(tup, pos_strings)) and
is_parent_in(tup, filled_li) and
tup not in li])
if li:
children = add_all_dep_and_pos_children(parse_tree, dep_strings, pos_strings, li, filled_li, i)
deps_and_pos += children
return get_the_dep_and_pos_children(parse_tree,
dep_strings,
pos_strings,
children,
children,
i,
deps_and_pos)
return deps_and_pos
return get_the_dep_and_pos_children(parse_tree, dep_strings, pos_strings, li, filled_li, i, deps_and_pos = [])
descendants = ["do while loop emulator"]
while descendants:
descendants = []
descendants += (recursively_add_all_dep_and_pos_children
(
parse_tree,
dep_strings,
pos_strings,
adj_list,
all_words_in_between(parse_tree,
filter_duplicates(adj_list),
i),
i
))
adj_list += descendants
return adj_list
def in_in_between_or_root_a(parse_tree, p, a, i, conjunctions):
return (check_for_in(parse_tree, p, a, i, conjunctions) or
is_root(a))
def ancestor_states_conditions(parse_tree, p, a, i, conjunctions, bes, haves):
def parent_of_be_or_have(parse_tree, a, i, bes, haves):
return ([tup for tup in parse_tree[i] if
is_a_be_or_have(tup, bes, haves) and
is_parent(tup, a)])
return (not parent_of_be_or_have(parse_tree, a, i, bes, haves) or
find_nearby_vbs(parse_tree, a, i, ["VBG", "VBD"]) or
in_in_between_or_root_a(parse_tree, p, a, i, conjunctions))
def add_nt_words(parse_tree, negs, i):
for neg in negs:
if is_word_equal(neg, "n't"):
negs.append(prev_word(parse_tree, neg, i))
return negs
def add_to_set(word_list, p, a, i, char_id, description, container, facts, states, feelings, invalid, all_phrases):
if char_id not in container:
container[char_id] = {}
if i not in container[char_id]:
container[char_id][i] = []
container[char_id][i].append(description)
store_description(word_list, p, a, i, description, container,
facts, states, feelings, invalid,
all_phrases)
else:
added = False
for j in range(len(container[char_id][i])):
if (is_subset(container[char_id][i][j], description) or
is_subset(description, container[char_id][i][j])):
container[char_id][i][j] = superset(container[char_id][i][j], description)
added = True
if not added:
container[char_id][i].append(description)
subset_phrases = [phrase for phrase in all_phrases if
phrase[0] == i and 0 not in phrase[5] and
(is_substring(phrase[4], get_adj_from_list(description)) or
is_substring(get_adj_from_list(description), phrase[4]))]
if subset_phrases:
for ph in subset_phrases:
all_phrases.remove(ph)
all_phrases.add((ph[0], ph[1], ph[2], ph[3],
superset(ph[4], get_adj_from_list(description)), ph[5]))
store_description(word_list, p, a, i, description, container,
facts, states, feelings, invalid,
all_phrases)
def find_non_person_nsubjs(parse_tree, a, i):
return [tup for tup in parse_tree[i] if
is_dependency_equal(tup, "nsubj") and
is_parent(tup, a) and char_id(tup) == -1 and
not is_pos_of_any(tup, ["PRP", "NNP", "WP", "DT"])]
def find_child_sibling_and_between_negs(parse_tree, p, a, i):
def find_child_sibling_negs(parse_tree, a, i):
def child_or_sibling_negs(parse_tree, a, i):
return ([tup for tup in parse_tree[i] if
is_dependency_equal(tup, "neg") and
(is_parent(tup, a) or
has_common_parent(tup, a))])
return sorted(add_nt_words(parse_tree,
add_prevs_of_negs(parse_tree,
child_or_sibling_negs(parse_tree,
a,
i),
i),
i))
def find_between_negs(parse_tree, p, a, i):
return ([tup for tup in parse_tree[i] if
is_dependency_of(tup, "neg") and
index(p) < index(tup) and
index(tup) < index(a)])
return (find_child_sibling_negs(parse_tree, a, i) +
find_between_negs(parse_tree, p, a, i))
def add_dependents(parse_tree, adj_list, people, p, a, i, conjunctions, bes, haves):
if is_dependency_of(a, "xcomp"):
adj_list += words_between_adj_and_parent(parse_tree, a, i)
adj_list += find_child_sibling_and_between_negs(parse_tree, p, a, i)
adj_list += find_RBs(parse_tree, a, i)
adj_list += find_non_person_nsubjs(parse_tree, a, i)
adj_list += find_nearby_vbs(parse_tree, a, i, ["VBG", "VBD", "VBZ"])
adj_list += find_nearby_vbs(parse_tree, p, i, ["VBG", "VBD", "VBZ"])
adj_list += find_adj_advcls(parse_tree, a, i)
adj_list += find_adj_acl_relcls(parse_tree, p , i)
adj_list += find_preps(parse_tree, a, i)
adj_list += find_advmods_and_dets(parse_tree, a, i, bes, haves)
adj_list += find_conj_and_nmod_words(parse_tree, a, i)
adj_list += find_valid_person_parents(parse_tree, p, i, conjunctions)
adj_list += find_valid_adj_parents(parse_tree, p, a, i, conjunctions)
adj_list += find_latter_preps_and_coord_conjs(parse_tree, adj_list, p, a, i)
adj_list += find_punctuations(parse_tree, adj_list, i)
adj_list += find_as(parse_tree, filter_duplicates(adj_list), i)
adj_list += find_parent_and_prev_negs(parse_tree, adj_list, p, a, i, people, conjunctions)
adj_list = recursively_add_all_dep_and_pos_children(parse_tree,
["xcomp", "ccomp",
"nmod", "conj",
"dobj", "cc", "aux"],
["VB", "RB", "WRB"],
adj_list,
i)
return adj_list
def refine(parse_tree, adj_list, p, a, i, stopwords, conjunctions):
adj_list = all_words_in_between(parse_tree, filter_duplicates(adj_list), i)
adj_list = punct_split(parse_tree, all_words_in_between(parse_tree,
adj_list,
i),
p, a, i, conjunctions)
adj_list = all_words_in_between(parse_tree, filter_duplicates(adj_list), i)
adj_list = remove_stop_words(adj_list, stopwords)
return filter_duplicates(adj_list)
def find_valid_person_parents(parse_tree, p, i, conjunctions):
def add_parent_of_person_conds(parse_tree, p, i):
return (is_pos_of(parent(parse_tree, p, i), "VB"))
parent_tup = parent(parse_tree, p, i)
if (add_parent_of_person_conds(parse_tree, p, i) and
no_punct_in_between(parse_tree, p, parent_tup, p, i, conjunctions)):
return [parent_tup]
return []
def get_description(parse_tree, people, p, a, i, stopwords, conjunctions, bes, haves):
adj_list = add_dependents(parse_tree, [a], people, p, a, i, conjunctions, bes, haves)
adj_list = refine(parse_tree, filter_duplicates(adj_list), p, a, i,
stopwords, conjunctions)
return adj_list
##############################################################################
##############################################################################
##############################################################################
#################### PRINT METHODS ###########################################
##############################################################################
##############################################################################
##############################################################################
##############################################################################
def print_sentence_and_tuples(parse_tree, word_list, (p, a, i, facts)):
print i, '--\n', sentence(word_list, i), '\n', parse_tree[i],\
'----------------------\n'
print p
print a
print parent(parse_tree, a, i)
print parent(parse_tree, p, i)
print '-----------------------------------------------------------\
-----------------\n'
def print_in_string(dictionary):
stringed_dict = {}
for key in dictionary:
stringed_dict[key] = get_adj_from_list(dictionary[key])
return stringed_dict
def store_description(word_list, p, a, i, description, dictionary, facts, states, feelings, invalid, all_phrases):
desc_num = []
if dictionary == invalid:
desc_num.append(0)
if dictionary == facts:
desc_num.append(1)
elif dictionary == states:
desc_num.append(2)
elif dictionary == feelings:
desc_num.append(3)
same_phrases = [phrase for phrase in all_phrases if phrase[0] == i and
phrase[4] == get_adj_from_list(description) and
desc_num[0] != 0 and 0 not in phrase[5] and desc_num[0] not in phrase[5]]
if same_phrases:
for tag in [phrase[5] for phrase in same_phrases]:
for val in tag:
if val not in desc_num:
desc_num.append(val)
for phrase in same_phrases:
all_phrases.add((phrase[0], phrase[1], phrase[2], phrase[3], phrase[4], tuple(desc_num)))
all_phrases.remove(phrase)
all_phrases.add((i, highlighted_sentence(word_list, i, index(p), index(a)),
(index(p), word(p)), (index(a), word(a)),
get_adj_from_list(description), tuple(desc_num)))
def print_undifferentiated_personas(descriptions, character):
describers = descriptions[0]
facts = describers[0]
states = describers[1]
feelings = describers[2]
#invalid = describers[3]
for id in sorted(character):
if id in facts or id in feelings or id in states:
print'____________________________________________________________'
print id, sorted(character[id])
print '----------------'
print 'FACTS'
print '----------------'
if id in facts:
for sentID in sorted(facts[id]):
for li in facts[id][sentID]:
#print sentID, get_adj_from_list(li)
print get_adj_from_list(li)
print '----------------'
print 'STATES'
print '----------------'
if id in states:
for sentID in sorted(states[id]):
for li in states[id][sentID]:
#print sentID, get_adj_from_list(li)
print get_adj_from_list(li)
print '----------------'
print 'FEELINGS'
print '----------------'
if id in feelings:
for sentID in sorted(feelings[id]):
for li in feelings[id][sentID]:
#print sentID, get_adj_from_list(li)
print get_adj_from_list(li)
####
#print '----------------'
#print 'INVALID'
#print '----------------'
#if id in invalid:
# for sentID in sorted(invalid[id]):
# print sentID, get_adj_from_list(invalid[id][sentID])
####
print'____________________________________________________________'
def print_personas(descriptions, character, differentiated):
describers = descriptions[0]
opinion_describers = descriptions[1]
#print "diff", differentiated
if not differentiated:
print_undifferentiated_personas(descriptions, character)
return
facts = describers[0]
states = describers[1]
feelings = describers[2]
opinion_facts = opinion_describers[0]
opinion_states = opinion_describers[1]
opinion_feelings = opinion_describers[2]
#invalid = describers[3]
for id in sorted(character):
if id in facts or id in feelings or id in states:
print'____________________________________________________________'
print id, sorted(character[id])
print '----------------'
print 'FACTS'
print '----------------'
if id in facts:
for sentID in sorted(facts[id]):
for li in facts[id][sentID]:
#print sentID, get_adj_from_list(li)
print get_adj_from_list(li)
print '----------------'
print 'STATES'
print '----------------'
if id in states:
for sentID in sorted(states[id]):
for li in states[id][sentID]:
#print sentID, get_adj_from_list(li)
print get_adj_from_list(li)
print '----------------'
print 'FEELINGS'
print '----------------'
if id in feelings:
for sentID in sorted(feelings[id]):
for li in feelings[id][sentID]:
#print sentID, get_adj_from_list(li)
print get_adj_from_list(li)
####
#print '----------------'
#print 'INVALID'
#print '----------------'
#if id in invalid:
# for sentID in sorted(invalid[id]):
# print sentID, get_adj_from_list(invalid[id][sentID])
####
if id in opinion_facts or id in opinion_feelings or id in opinion_states:
print'____________________________________________________________'
print id, sorted(character[id])
print '----------------'
print 'OPINION FACTS'
print '----------------'
if id in opinion_facts:
for sentID in sorted(opinion_facts[id]):
for li in opinion_facts[id][sentID]:
#print sentID, get_adj_from_list(li)
print get_adj_from_list(li)
print '----------------'