-
Notifications
You must be signed in to change notification settings - Fork 7
/
phrase_tree.py
263 lines (208 loc) · 6.05 KB
/
phrase_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
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
class PhraseNode:
def __init__(self, idx, iparent=None, token=None):
self._idx = idx
self._iparent = iparent if iparent is not None and iparent >= 0 else None
self._token = token
self._left = None
self._right = None
self._parent = None
self._sentiment = None
self._phrase_id = None
self._word_vec = None
def __repr__(self):
if self.token is not None:
return '{}:{}'.format(self.idx, self.token)
elif self.left is not None:
lstr = self.left.idx
else:
lstr = '*'
if self.right is not None:
rstr = self.right.idx
else:
rstr = '*'
return '{}:[{},{}]'.format(self._idx, lstr, rstr)
def fix_pointers(self):
if self._token is not None:
return
if self.left.lowest_leaf_index > self.right.lowest_leaf_index:
self._left, self._right = self._right, self._left
self._left.fix_pointers()
self._right.fix_pointers()
@property
def is_root(self):
return self.iparent is None or self.iparent < 0
@property
def is_leaf(self):
return self.token is not None
@property
def idx(self):
return self._idx
@property
def iparent(self):
return self._iparent
@iparent.setter
def iparent(self, val):
self._iparent = val
@property
def parent(self):
return self._parent
@parent.setter
def parent(self, par):
self._parent = par
@property
def token(self):
return self._token
@token.setter
def token(self, s):
self._token = s
@property
def left(self):
return self._left
@left.setter
def left(self, node):
self._left = node
@property
def right(self):
return self._right
@right.setter
def right(self, node):
self._right = node
@property
def phrase(self):
ll = sorted(self.leaves, key=lambda x: x.idx)
return ' '.join([x.token for x in ll])
@property
def phrase_id(self):
return self._phrase_id
@phrase_id.setter
def phrase_id(self, val):
self._phrase_id = val
@property
def word_vec(self):
return self._word_vec
@word_vec.setter
def word_vec(self, val):
self._word_vec = val
@property
def sentiment(self):
return self._sentiment
@sentiment.setter
def sentiment(self, val):
self._sentiment = val
@property
def leaves(self):
if self.is_leaf:
return [self]
else:
return self.left.leaves + self.right.leaves
@property
def lowest_leaf_index(self):
if self.is_leaf:
return self.idx
else:
return min(self.left.lowest_leaf_index, self.right.lowest_leaf_index)
@property
def n_chunks(self):
if self.is_leaf:
return 1
else:
return self._left.n_chunks + 1 + self._right.n_chunks
class SentenceTree:
def __init__(self, idx, tokens, ppointers, sentence):
self._idx = idx
self._iter_index = 0
self._sentence = sentence
self._nodes = [PhraseNode(i, ppointers[i]-1) for i in range(len(ppointers))]
for i in range(len(ppointers)):
node = self._nodes[i]
if i < len(tokens):
node._token = tokens[i]
pp = ppointers[i]
if pp != 0:
node._parent = self._nodes[pp-1]
if node._parent.left is None:
node._parent.left = node
else:
node._parent.right = node
else:
self._root = node
def __repr__(self):
return '{}:{}'.format(self._idx, self._root.phrase)
@property
def root(self):
return self._root
@property
def idx(self):
return self._idx
@property
def nodes(self):
return self._nodes
@property
def leaves(self):
return [_node for _node in self._nodes if _node.is_leaf]
@property
def sentence(self):
return self._sentence
@property
def n_chunks(self):
return self._root.n_chunks
def fix_pointers(self):
self._root.fix_pointers()
def __getitem__(self, idx):
return self._nodes[idx]
def __len__(self):
return len(self._nodes)
def __iter__(self):
self._iter_index = 0
return self
def __next__(self):
self._iter_index += 1
if self._iter_index > self.__len__():
raise StopIteration
else:
return self[self._iter_index - 1]
#@property # breaks in Python 2
def next(self):
return self.__next__()
@classmethod
def load_trees(cls, tree_data, sentences):
trees = [cls(i, tokens, tree, sentences[i])
for (i, (tokens, tree)) in enumerate(tree_data)]
return trees
class SentenceSet:
def __init__(self, trees=None):
self._iter_index = 0
self._idx_dict = {}
if trees:
for tree in trees:
self.add(tree)
def add(self, tree):
self._idx_dict[tree.idx] = tree
def __getitem__(self, idx):
return self._idx_dict[idx]
def __len__(self):
return len(self._idx_dict)
def __iter__(self):
self._iter_index = 0
return self
def __next__(self):
self._iter_index += 1
if self._iter_index > len(self):
raise StopIteration
else:
return self._idx_dict[self._iter_index - 1]
def fix_pointers(self):
for tree in self:
tree.fix_pointers()
#@property #only works in Python 3
def next(self):
return self.__next__()
@property
def n_chunks(self):
return sum(sentence.n_chunks for sentence in self)
@property
def phrases(self):
return [tree.phrase for tree in
[item for sublist in
[sentence.nodes for sentence in self]
for item in sublist]]