-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_utils.py
307 lines (258 loc) · 10.5 KB
/
data_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
import numpy as np
ROOT = '<root>'
NULL = '<null>'
UNK = '<unk>'
def log_stats(sentences):
num_tokens_per_s = list(map(lambda x: len(x.tokens), sentences))
print('Token stats:')
print(f'# of sentences: {len(sentences)}')
print(f'mean: {np.mean(num_tokens_per_s)}, '
f'median: {np.median(num_tokens_per_s)}, '
f'std: {np.std(num_tokens_per_s)}')
class Token:
def __init__(self, token_id, word, pos, head, dep):
self.token_id = token_id
self.word = word
self.pos = pos
self.head = head
self.dep = dep
self.lc, self.rc = [], []
self.attached = False # Used with arc-eager system only
def get_left_most_child(self, num=1):
return self.lc[0 + num - 1] if len(self.lc) >= num else NULL_TOKEN
def get_right_most_child(self, num=1):
return self.rc[-1 - num + 1] if len(self.rc) >= num else NULL_TOKEN
def __str__(self):
return f"{self.token_id:5} | {self.word} | {self.pos} |" +\
f" {self.head} | {self.dep}"
def print_children(self):
lc = [(t.word, t.token_id) for t in self.lc]
rc = [(t.word, t.token_id) for t in self.rc]
print(f'{self.token_id} {self.word} : left {lc} | right {rc}')
def _enforce_children_order_debug(self):
try:
lc = [int(t.token_id) for t in self.lc]
for i in range(len(lc) - 1):
assert lc[i] < lc[i+1]
assert int(self.token_id) > lc[i]
if len(lc) > 1:
assert int(self.token_id) > lc[-1]
except:
print('\nproblem with lc')
breakpoint()
return False
try:
rc = [int(t.token_id) for t in self.rc]
for i in range(len(rc) - 1):
assert rc[i] < rc[i+1]
assert int(self.token_id) < rc[i]
if len(rc) > 1:
assert int(self.token_id) < rc[-1]
except:
print('\nproblem with rc')
breakpoint()
return False
return True
# ROOT_TOKEN = Token(token_id="0", word=ROOT, pos=ROOT, head="-1", dep=ROOT)
NULL_TOKEN = Token(token_id="-1", word=NULL, pos=NULL, head="-1", dep=NULL)
UNK_TOKEN = Token(token_id="-1", word=UNK, pos=UNK, head="-1", dep=UNK)
class Sentence:
def __init__(self, tokens=[], transition_system='std'):
self.root = Token(token_id="0", word=ROOT, pos=ROOT, head="-1", dep=ROOT)
self.tokens = tokens.copy()
self.stack = [self.root]
self.buffer = tokens.copy()
self.arcs = []
self.setup_trans_sys(transition_system)
def setup_trans_sys(self, trans_system):
if trans_system == 'std':
# arc-standard
self.update_state = self.update_state_std
self._get_trans = self._get_trans_std
self.supported_operations =\
['left_arc', 'right_arc', 'shift']
else:
# arc-eager
self.update_state = self.update_state_eager
self._get_trans = self._get_trans_eager
self.supported_operations =\
['left_arc', 'right_arc', 'reduce', 'shift']
def __len__(self):
return len(self.tokens)
def is_exausted(self):
return len(self.stack) == 1 and len(self.buffer) == 0
def add_token(self, token):
self.tokens.append(token)
self.buffer.append(token)
def peek_stack(self, top=1):
items = list(reversed(self.stack[-top:]))
if len(self.stack) < top:
items += [NULL_TOKEN for _ in range(top - len(self.stack))]
return items
def peek_buffer(self, top=1):
items = self.buffer[:top]
if len(self.buffer) < top:
items += [NULL_TOKEN for _ in range(top - len(self.buffer))]
return items
def get_trans(self): # this function is only used for the ground truth
""" decide transition operation"""
for operation in self.supported_operations:
# Retrive transition name completely
trans = self._get_trans(operation)
if trans is not None:
# print(trans, self)
# breakpoint()
return trans
return None
def _is_dep_in_buff(self, token_id):
for t in self.buffer:
if t.head == token_id:
return True
return False
def _has_unassigned_child(self, token):
for child_t in self.buffer + self.stack:
if child_t in token.lc or child_t in token.rc:
continue
if child_t.head == token.token_id:
return True
return False
def _get_trans_eager(self, potential_trans):
""" get transition if it can legally be performed"""
# LEFT, top of buffer is parent of top of stack
def check_left_arc_sat():
if self.buffer[0].token_id != self.stack[-1].head:
return None
return f"left_arc({self.stack[-1].dep})"
# RIGHT, top of stack is parent of top in buffer
def check_right_arc_sat():
if self.stack[-1].token_id != self.buffer[0].head:
return None
return f"right_arc({self.buffer[0].dep})"
# top of the stack has an assigned parent and no unassigned children
def check_reduce():
if not self.stack[-1].attached or\
self._has_unassigned_child(self.stack[-1]):
return None
return 'reduce'
if len(self.stack) > 0 and len(self.buffer) > 0:
if potential_trans == 'left_arc':
return check_left_arc_sat()
if potential_trans == 'right_arc':
return check_right_arc_sat()
if potential_trans == 'reduce' and len(self.stack) > 0:
return check_reduce()
if potential_trans == 'shift' and len(self.buffer) > 0:
return 'shift'
return None
def update_state_eager(self, curr_trans, predicted_dep=None):
"""
updates the sentence according to the given
transition assuming dependancy satisfiability
but NOT legality
"""
if 'shift' in curr_trans:
if len(self.buffer) == 0:
return False
self.stack.append(self.buffer.pop(0))
return True
if 'reduce' in curr_trans:
if len(self.stack) == 0:
return False
self.stack.pop(-1)
return True
if len(self.stack) < 1 or len(self.buffer) < 1:
return False
# top of buffer is parent of top of stack
if 'left_arc' in curr_trans:
parent = self.buffer[0]
child = self.stack.pop(-1)
# if not child.attached:
parent.lc.insert(0, child)
child.attached = True
# DEBUG ONLY
# breakpoint()
# parent._enforce_children_order_debug()
# if not parent._enforce_children_order_debug():
# breakpoint()
# print('parent', parent.word)
# breakpoint()
if predicted_dep is not None:
child.dep = predicted_dep
child.head = parent.token_id
self.arcs.append((parent, child, child.dep, 'l'))
return True
# top of stack is parent of top in buffer
if 'right_arc' in curr_trans:
parent = self.stack[-1]
child = self.buffer[0]
# if not child.attached:
parent.rc.append(child)
child.attached = True
# DEBUG ONLY
# if self.buffer[0].word == 'prevent':
# breakpoint()
# parent._enforce_children_order_debug()
# breakpoint()
# print('parent', parent.word)
# breakpoint()
if predicted_dep is not None:
child.dep = predicted_dep
child.head = parent.token_id
self.arcs.append((parent, child, child.dep, 'r'))
return self.update_state_eager('shift-r')
def _get_trans_std(self, potential_trans):
""" get transition if it can legally be performed"""
# LEFT, top of stack is parent of second-top
def check_left_arc_sat():
if self.stack[-1].token_id != self.stack[-2].head:
return None
return f"left_arc({self.stack[-2].dep})"
# RIGHT, second-top of stack is parent of top,
# and no depends of top in buffer (buff is empty)
def check_right_arc_sat():
if self._is_dep_in_buff(self.stack[-1].token_id) or\
self.stack[-2].token_id != self.stack[-1].head:
return None
return f"right_arc({self.stack[-1].dep})"
if len(self.stack) >= 2:
if potential_trans == 'left_arc':
return check_left_arc_sat()
if potential_trans == 'right_arc':
return check_right_arc_sat()
if potential_trans == 'shift' and len(self.buffer) >= 1:
return 'shift'
return None
def update_state_std(self, curr_trans, predicted_dep=None):
"""
updates the sentence according to the given
transition assuming dependancy satisfiability
but NOT legality
"""
if 'shift' in curr_trans:
if len(self.buffer) == 0:
return False
self.stack.append(self.buffer.pop(0))
return True
if len(self.stack) < 2:
return False
if 'left_arc' in curr_trans:
parent = self.stack[-1]
child = self.stack.pop(-2)
parent.lc.insert(0, child)
if predicted_dep is not None:
child.dep = predicted_dep
child.head = parent.token_id
self.arcs.append((parent, child, child.dep, 'l'))
return True
if 'right_arc' in curr_trans:
parent = self.stack[-2]
child = self.stack.pop(-1)
parent.rc.append(child)
if predicted_dep is not None:
child.dep = predicted_dep
child.head = parent.token_id
self.arcs.append((parent, child, child.dep, 'r'))
return True
def __str__(self) -> str:
return f"Stack {[t.word for t in self.stack]} | " +\
f"Buffer {[t.word for t in self.buffer]}"