-
Notifications
You must be signed in to change notification settings - Fork 37
/
utils.py
331 lines (312 loc) · 8.15 KB
/
utils.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
#Norm!/usr/bin/env python3
import numpy as np
import itertools
import random
import torch
import nltk
def all_binary_trees(n):
#get all binary trees of length n
def is_tree(tree, n):
# shift = 0, reduce = 1
if sum(tree) != n-1:
return False
stack = 0
for a in tree:
if a == 0:
stack += 1
else:
if stack < 2:
return False
stack -= 1
if stack < 0:
return False
return True
valid_tree = []
num_shift = 0
num_reduce = 0
num_actions = 2*n - 1
trees = map(list, itertools.product([0,1], repeat = num_actions-3))
start = [0, 0] #first two actions are always shift
end = [1] # last action is always reduce
for tree in trees:
tree = start + tree + end
if is_tree(tree, n):
valid_tree.append(tree[::])
return valid_tree
def get_actions(tree, SHIFT = 0, REDUCE = 1, OPEN='(', CLOSE=')'):
#input tree in bracket form: ((A B) (C D))
#output action sequence: S S R S S R R
actions = []
tree = tree.strip()
i = 0
num_shift = 0
num_reduce = 0
left = 0
right = 0
while i < len(tree):
if tree[i] != ' ' and tree[i] != OPEN and tree[i] != CLOSE: #terminal
if tree[i-1] == OPEN or tree[i-1] == ' ':
actions.append(SHIFT)
num_shift += 1
elif tree[i] == CLOSE:
actions.append(REDUCE)
num_reduce += 1
right += 1
elif tree[i] == OPEN:
left += 1
i += 1
assert(num_shift == num_reduce + 1)
return actions
def get_tree(actions, sent = None, SHIFT = 0, REDUCE = 1):
#input action and sent (lists), e.g. S S R S S R R, A B C D
#output tree ((A B) (C D))
stack = []
pointer = 0
if sent is None:
sent = list(map(str, range((len(actions)+1) // 2)))
# assert(len(actions) == 2*len(sent) - 1)
for action in actions:
if action == SHIFT:
word = sent[pointer]
stack.append(word)
pointer += 1
elif action == REDUCE:
right = stack.pop()
left = stack.pop()
stack.append('(' + left + ' ' + right + ')')
assert(len(stack) == 1)
return stack[-1]
def get_depth(tree, SHIFT = 0, REDUCE = 1):
stack = []
depth = 0
max = 0
curr_max = 0
for c in tree:
if c == '(':
curr_max += 1
if curr_max > max:
max = curr_max
elif c == ')':
curr_max -= 1
assert(curr_max == 0)
return max
def get_spans(actions, SHIFT = 0, REDUCE = 1):
sent = list(range((len(actions)+1) // 2))
spans = []
pointer = 0
stack = []
for action in actions:
if action == SHIFT:
word = sent[pointer]
stack.append(word)
pointer += 1
elif action == REDUCE:
right = stack.pop()
left = stack.pop()
if isinstance(left, int):
left = (left, None)
if isinstance(right, int):
right = (None, right)
new_span = (left[0], right[1])
spans.append(new_span)
stack.append(new_span)
return spans
def get_stats(span1, span2):
tp = 0
fp = 0
fn = 0
for span in span1:
if span in span2:
tp += 1
else:
fp += 1
for span in span2:
if span not in span1:
fn += 1
return tp, fp, fn
def update_stats(pred_span, gold_spans, stats):
for gold_span, stat in zip(gold_spans, stats):
tp, fp, fn = get_stats(pred_span, gold_span)
stat[0] += tp
stat[1] += fp
stat[2] += fn
def get_f1(stats):
f1s = []
for stat in stats:
prec = stat[0] / (stat[0] + stat[1]) if stat[0] + stat[1] > 0 else 0.
recall = stat[0] / (stat[0] + stat[2]) if stat[0] + stat[2] > 0 else 0.
f1 = 2*prec*recall / (prec + recall)*100 if prec+recall > 0 else 0.
f1s.append(f1)
return f1s
def get_random_tree(length, SHIFT = 0, REDUCE = 1):
tree = [SHIFT, SHIFT]
stack = ['', '']
num_shift = 2
while len(tree) < 2*length - 1:
if len(stack) < 2:
tree.append(SHIFT)
stack.append('')
num_shift += 1
elif num_shift >= length:
tree.append(REDUCE)
stack.pop()
else:
if random.random() < 0.5:
tree.append(SHIFT)
stack.append('')
num_shift += 1
else:
tree.append(REDUCE)
stack.pop()
return tree
def span_str(start = None, end = None):
assert(start is not None or end is not None)
if start is None:
return ' ' + str(end) + ')'
elif end is None:
return '(' + str(start) + ' '
else:
return ' (' + str(start) + ' ' + str(end) + ') '
def get_tree_from_binary_matrix(matrix, length):
sent = list(map(str, range(length)))
n = len(sent)
tree = {}
for i in range(n):
tree[i] = sent[i]
for k in np.arange(1, n):
for s in np.arange(n):
t = s + k
if t > n-1:
break
if matrix[s][t].item() == 1:
span = '(' + tree[s] + ' ' + tree[t] + ')'
tree[s] = span
tree[t] = span
return tree[0]
def get_nonbinary_spans(actions, SHIFT = 0, REDUCE = 1):
spans = []
stack = []
pointer = 0
binary_actions = []
nonbinary_actions = []
num_shift = 0
num_reduce = 0
for action in actions:
# print(action, stack)
if action == "SHIFT":
nonbinary_actions.append(SHIFT)
stack.append((pointer, pointer))
pointer += 1
binary_actions.append(SHIFT)
num_shift += 1
elif action[:3] == 'NT(':
stack.append('(')
elif action == "REDUCE":
nonbinary_actions.append(REDUCE)
right = stack.pop()
left = right
n = 1
while stack[-1] is not '(':
left = stack.pop()
n += 1
span = (left[0], right[1])
if left[0] != right[1]:
spans.append(span)
stack.pop()
stack.append(span)
while n > 1:
n -= 1
binary_actions.append(REDUCE)
num_reduce += 1
else:
assert False
assert(len(stack) == 1)
assert(num_shift == num_reduce + 1)
return spans, binary_actions, nonbinary_actions
def get_nonbinary_tree(sent, tags, actions):
pointer = 0
tree = []
for action in actions:
if action[:2] == "NT":
node_label = action[:-1].split("NT")[1]
node_label = node_label.split("-")[0]
tree.append(node_label)
elif action == "REDUCE":
tree.append(")")
elif action == "SHIFT":
leaf = "(" + tags[pointer] + " " + sent[pointer] + ")"
pointer += 1
tree.append(leaf)
else:
assert(False)
assert(pointer == len(sent))
return " ".join(tree).replace(" )", ")")
def build_tree(depth, sen):
assert len(depth) == len(sen)
if len(depth) == 1:
parse_tree = sen[0]
else:
idx_max = np.argmax(depth)
parse_tree = []
if len(sen[:idx_max]) > 0:
tree0 = build_tree(depth[:idx_max], sen[:idx_max])
parse_tree.append(tree0)
tree1 = sen[idx_max]
if len(sen[idx_max + 1:]) > 0:
tree2 = build_tree(depth[idx_max + 1:], sen[idx_max + 1:])
tree1 = [tree1, tree2]
if parse_tree == []:
parse_tree = tree1
else:
parse_tree.append(tree1)
return parse_tree
def get_brackets(tree, idx=0):
brackets = set()
if isinstance(tree, list) or isinstance(tree, nltk.Tree):
for node in tree:
node_brac, next_idx = get_brackets(node, idx)
if next_idx - idx > 1:
brackets.add((idx, next_idx))
brackets.update(node_brac)
idx = next_idx
return brackets, idx
else:
return brackets, idx + 1
def get_nonbinary_spans_label(actions, SHIFT = 0, REDUCE = 1):
spans = []
stack = []
pointer = 0
binary_actions = []
num_shift = 0
num_reduce = 0
for action in actions:
# print(action, stack)
if action == "SHIFT":
stack.append((pointer, pointer))
pointer += 1
binary_actions.append(SHIFT)
num_shift += 1
elif action[:3] == 'NT(':
label = "(" + action.split("(")[1][:-1]
stack.append(label)
elif action == "REDUCE":
right = stack.pop()
left = right
n = 1
while stack[-1][0] is not '(':
left = stack.pop()
n += 1
span = (left[0], right[1], stack[-1][1:])
if left[0] != right[1]:
spans.append(span)
stack.pop()
stack.append(span)
while n > 1:
n -= 1
binary_actions.append(REDUCE)
num_reduce += 1
else:
assert False
assert(len(stack) == 1)
assert(num_shift == num_reduce + 1)
return spans, binary_actions