-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependency_tree.py
157 lines (105 loc) · 4.54 KB
/
dependency_tree.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
# -*- coding: utf-8 -*-
from nltk.corpus import wordnet as wn
class Relation:
def __init__(self, mention, index):
self.mention = mention
self.index = index
class Node:
def __init__(self, word, index):
self.word = word
self.kids = []
self.index = index
self.finished = False
class DTree:
def __init__(self, toks, parents, labels,vocab,rel_vocab):
self.nodes = []
for tok in toks:
self.nodes.append(Node(tok, vocab[tok]))
self.dependencies = []
for i, (govIdx, rel) in enumerate(zip(parents, labels)):
depIdx = i + 1
govIdx = int(govIdx)
if govIdx > 0:
self.nodes[govIdx-1].kids.append((depIdx, Relation(rel,rel_vocab[rel])))
self.dependencies.append((govIdx, depIdx))
def resetFinished(self):
for node in self.nodes:
node.finished = False
def mergeWith(self, dtree):
for govIdx_d, depIdx_d in dtree.dependencies:
if govIdx_d == 0:
continue
govTok_d = dtree.nodes[govIdx_d-1].word
depTok_d = dtree.nodes[depIdx_d-1].word
rel_d = None
for idx, rel in dtree.nodes[govIdx_d-1].kids:
if idx == depIdx_d:
rel_d = rel
assert rel_d != None
add = False
for govIdx, depIdx in self.dependencies:
if govIdx == 0:
continue
govTok = self.nodes[govIdx-1].word
depTok = self.nodes[depIdx-1].word
govMatch = isMatch(govTok, govTok_d, synonym = True, entail = True,
antonym = True, hypernym = True, hyponym = True)
depMatch = isMatch(depTok, depTok_d, synonym = True, entail = True,
antonym = True, hypernym = True, hyponym = True)
if govMatch and not depMatch:
add = True
matchedTextGovToken = govTok
matchedTextGovIdx = govIdx
if govMatch and depMatch:
add = False
break
if add:
depGloIdx_d = dtree.nodes[depIdx_d-1].index
cNode = Node(depTok_d,depGloIdx_d)
self.nodes.append(cNode)
newDepIdx = len(self.nodes)
self.dependencies.append((matchedTextGovIdx, newDepIdx))
self.nodes[matchedTextGovIdx].kids.append((newDepIdx, rel_d))
def isMatch(T, H, synonym = False, entail = False, antonym = False, hypernym = False, hyponym = False):
is_exact_match = False
if T == H:
is_exact_match = True
synsets_T = wn.synsets(T)
synsets_H = wn.synsets(H)
is_synonym = False
is_entail = False
is_antonmy = False
is_hypernym = False
is_hyponym = False
if synonym :
lemmas_T = [str(lemma.name()) for ss in synsets_T for lemma in ss.lemmas()]
lemmas_H = [str(lemma.name()) for ss in synsets_H for lemma in ss.lemmas()]
c = list(set(lemmas_T).intersection(set(lemmas_H)))
if len(c) > 0 or H in lemmas_T:
is_synonym = True
else:
is_synonym = False
if entail :
for s_T in synsets_T:
for s_H in synsets_H:
if s_H in s_T.entailments():
is_entail = True
if antonym :
nega_T = [str(nega.name()) for ss in synsets_T for lemma in ss.lemmas() for nega in lemma.antonyms()]
if H in nega_T:
is_antonmy = True
if hypernym :
for s_T in synsets_T:
for s_H in synsets_H:
if s_H in s_T.hyponyms():
is_hypernym = True
if s_T in [synset for path in s_H.hypernym_paths() for synset in path]:
is_hypernym = True
if hyponym :
for s_T in synsets_T:
for s_H in synsets_H:
if s_T in s_H.hyponyms():
is_hyponym = True
if s_H in [synset for path in s_T.hypernym_paths() for synset in path]:
is_hyponym = True
return is_exact_match or is_synonym or is_entail or is_antonmy or is_hypernym or is_hyponym