forked from MurtyShikhar/structural-grokking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_q_and_tense.py
461 lines (394 loc) · 16.4 KB
/
parse_q_and_tense.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
# Code borrowed from McCoy et al: Does syntax need to grow on trees? Sources of hierarchical inductive bias in sequence-to-sequence networks
# Various functions to enable parsing of sentences
# from our artificial grammars
import os
# Load part-of-speech labels
posDict = {}
DIR = os.path.abspath(os.path.dirname(__file__))
fi = open("{}/data_utils/tense_inflection_data/pos.txt".format(DIR), "r") # MIGHT NEED TO CHANGE BACK
for line in fi:
parts = line.split("\t")
posDict[parts[0].strip()] = parts[1].strip()
posDict2 = {}
fi = open("{}/data_utils/tense_inflection_data/pos2.txt".format(DIR), "r")
for line in fi:
parts = line.split("\t")
posDict2[parts[0].strip()] = parts[1].strip()
# Conert a sentence to part-of-speech tags
def sent_to_pos(sent):
words = sent.split()
pos_tags = []
for word in words:
if word in posDict:
pos_tags.append(posDict[word])
else:
pos_tags.append(posDictTense[word])
return pos_tags
# Convert a sentence to part-of-speech tags
# from the second part-of-speech file
def sent_to_posb(sent):
words = sent.split()
pos_tags = []
for word in words:
pos_tags.append(posDict2[word])
return pos_tags
# Convert a sequence of part-of-speech tags into
# a parse. Works by successively grouping together
# neighboring pairs.
def pos_to_parse(pos_seq):
full_parse = []
current_parse = []
current_nodes = pos_seq
new_nodes = []
skip_next = 0
while len(current_nodes) > 1:
for index, node in enumerate(current_nodes):
if skip_next:
skip_next = 0
continue
if node == "D" and current_nodes[index + 1] == "N":
new_nodes.append("NP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "P" and current_nodes[index + 1] == "NP":
new_nodes.append("PP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "P" and current_nodes[index + 1] == "NP_f":
new_nodes.append("PP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "NP" and current_nodes[index + 1] == "PP":
new_nodes.append("NP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "NP" and current_nodes[index + 1] == "RC":
new_nodes.append("NP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "V" and current_nodes[index + 1] == "NP_f":
new_nodes.append("VP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "V" and current_nodes[index + 1] == "T":
new_nodes.append("VP")
current_parse.append([index])
elif node == "V" and current_nodes[index + 1] == "A":
new_nodes.append("VP")
current_parse.append([index])
elif node == "V" and current_nodes[index + 1] == "VP":
new_nodes.append("VP")
current_parse.append([index])
elif node == "VP" and current_nodes[index + 1] == "VP_f":
new_nodes.append("VP_f")
current_parse.append([index])
elif node == "NP" and current_nodes[index + 1] == "T":
new_nodes.append("NP_f")
current_parse.append([index])
elif node == "NP" and current_nodes[index + 1] == "VP_f":
new_nodes.append("S")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "S" and current_nodes[index + 1] == "T":
new_nodes.append("ROOT")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "A" and current_nodes[index + 1] == "S_bar":
new_nodes.append("A_S_bar")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "A" and current_nodes[index + 1] == "S":
new_nodes.append("A_S_bar")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "K" and current_nodes[index + 1] == "ROOT":
new_nodes.append("ROOT")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "K" and current_nodes[index + 1] == "ROOT":
new_nodes.append("ROOT")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "A" and current_nodes[index + 1] == "VP":
new_nodes.append("VP_f")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "C" and current_nodes[index + 1] == "VP":
new_nodes.append("VP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "R" and current_nodes[index + 1] == "VP_f":
new_nodes.append("RC")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "R" and current_nodes[index + 1] == "S":
new_nodes.append("RC")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "R" and current_nodes[index + 1] == "S_bar":
new_nodes.append("RC")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "NP" and current_nodes[index + 1] == "A":
new_nodes.append("NP_f")
current_parse.append([index])
elif node == "NP_f" and current_nodes[index + 1] == "VP_f":
new_nodes.append("S")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "V" and current_nodes[index + 1] == "NP_bar":
new_nodes.append("VP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "NP_bar" and current_nodes[index + 1] == "VP":
new_nodes.append("S_bar")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "NP" and current_nodes[index + 1] == "VP":
new_nodes.append("NP_bar")
current_parse.append([index])
elif node == "A_S_bar" and current_nodes[index + 1] == "T":
new_nodes.append("ROOT") # CHANGE
current_parse.append([index, index + 1])
skip_next = 1
elif node == "ROOT" and current_nodes[index + 1] == "G":
new_nodes.append("ROOT")
current_parse.append([index, index + 1])
skip_next = 1
else:
new_nodes.append(node)
current_parse.append([index])
current_nodes = new_nodes
new_nodes = []
skip_next = 0
full_parse.append(current_parse)
current_parse = []
full_parse.append([[0]])
return full_parse
# Parse a sentence from the question formation dataset
def parse_question(sent):
return pos_to_parse(sent_to_posb(sent))
# Create a part-of-speech dictionary for tense reinflection sentences
posDictTense = {}
fi = open("{}/data_utils/tense_inflection_data/pos_tense.txt".format(DIR), "r")
for line in fi:
parts = line.split("\t")
posDictTense[parts[0].strip()] = parts[1].strip()
# Convert a tense reinflection sentence into
# a sequence of part-of-speech tags
def sent_to_pos_tense(sent):
words = sent.split()
pos_tags = []
for word in words:
pos_tags.append(posDictTense[word])
return pos_tags
# Convert a sequence of part-of-speech tags into a parse.
# Works by successively grouping together
# neighboring pairs.
def pos_to_parse_tense(pos_seq):
full_parse = []
current_parse = []
current_nodes = pos_seq
new_nodes = []
skip_next = 0
while len(current_nodes) > 1:
for index, node in enumerate(current_nodes):
if skip_next:
skip_next = 0
continue
if node == "D" and current_nodes[index + 1] == "N":
new_nodes.append("NP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "P" and current_nodes[index + 1] == "NP":
new_nodes.append("PP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "P" and current_nodes[index + 1] == "NP_f":
new_nodes.append("PP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "NP" and current_nodes[index + 1] == "PP":
new_nodes.append("NP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "NP" and current_nodes[index + 1] == "RC":
new_nodes.append("NP")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "V" and current_nodes[index + 1] == "NP_f":
new_nodes.append("VP_f")
current_parse.append([index, index + 1])
skip_next = 1
elif (
node == "V"
and current_nodes[index + 1] == "NP"
and current_nodes[index + 2] == "VP_f"
):
new_nodes.append("VP_f")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "V" and current_nodes[index + 1] == "T":
new_nodes.append("VP_f")
current_parse.append([index])
elif node == "V" and current_nodes[index + 1] == "VP_f":
new_nodes.append("VP_f")
current_parse.append([index])
elif node == "NP" and current_nodes[index + 1] == "T":
new_nodes.append("NP_f")
current_parse.append([index])
elif node == "NP" and current_nodes[index + 1] == "VP_f":
new_nodes.append("S")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "S" and current_nodes[index + 1] == "T":
new_nodes.append("ROOT")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "K" and current_nodes[index + 1] == "ROOT":
new_nodes.append("ROOT")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "K" and current_nodes[index + 1] == "ROOT":
new_nodes.append("ROOT")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "R" and current_nodes[index + 1] == "VP_f":
new_nodes.append("RC")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "R" and current_nodes[index + 1] == "S":
new_nodes.append("RC")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "NP" and current_nodes[index + 1] == "A":
new_nodes.append("NP_f")
current_parse.append([index])
elif node == "NP_f" and current_nodes[index + 1] == "VP_f":
new_nodes.append("S")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "V" and current_nodes[index + 1] == "NP_bar":
new_nodes.append("VP_f")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "NP_bar" and current_nodes[index + 1] == "VP_f":
new_nodes.append("S_bar")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "NP" and current_nodes[index + 1] == "VP_f":
new_nodes.append("NP_bar")
current_parse.append([index])
elif node == "A_S_bar" and current_nodes[index + 1] == "T":
new_nodes.append("ROOT")
current_parse.append([index, index + 1])
skip_next = 1
elif node == "ROOT" and current_nodes[index + 1] == "G":
new_nodes.append("ROOT")
current_parse.append([index, index + 1])
skip_next = 1
else:
new_nodes.append(node)
current_parse.append([index])
current_nodes = new_nodes
new_nodes = []
skip_next = 0
full_parse.append(current_parse)
current_parse = []
full_parse.append([[0]])
return full_parse
# Parse a sentence from the tense reinflection dataset
def parse_tense(sent):
return pos_to_parse_tense(sent_to_pos_tense(sent))
# Returns a uniformly right-branching parse
def parse_right_branching(sent):
len_sent = len(sent.split())
full_parse = []
for i in range(len_sent):
if i == 0:
new_part = [[0]]
elif i == 1:
new_part = [[0, 1]]
else:
new_part = [[j] for j in range(i - 1)] + [[i - 1, i]]
full_parse = [new_part] + full_parse
return full_parse
def convert_parse_to_list(sent, parse):
words = sent.split(" ")
def helper():
return
def convert_to_parse(sent, parse_list, do_postprocess=True):
words = sent.split(" ")
def parse_helper(l_idx):
if l_idx == 0:
out_prev = words
else:
out_prev = parse_helper(l_idx - 1)
elems = parse_list[l_idx] ### index into the previous
out = []
for elem in elems:
curr = [out_prev[idx] for idx in elem]
if len(curr) > 1:
out.append(tuple(curr))
else:
out.append(curr[0])
return tuple(out)
parsed_inp = parse_helper(len(parse_list) - 1)
if len(parsed_inp) == 1:
parsed_inp = parsed_inp[0]
if do_postprocess:
if parsed_inp[-1] in ["quest", "decl", "past", "present"]:
parsed_inp = parsed_inp[0]
if parsed_inp[-1] == ".":
parsed_inp = parsed_inp[0]
return parsed_inp
def convert_into_layerwise_format(sent, parse):
def refine_layer(frontier, layer, g_obj):
so_far = {}
next_nodes = {}
parents = g_obj.parent
next_layer = []
for idx, _ in enumerate(layer):
parent = parents[frontier[idx]]
if parent in so_far:
index = so_far[parent]
next_layer[index].append(idx)
else:
so_far[parent] = len(so_far)
next_nodes[len(next_layer)] = (parent, frontier[idx])
next_layer.append([idx])
new_frontier = {}
for key, node in next_nodes.items():
if len(next_layer[key]) == 1:
new_frontier[key] = node[1]
else:
new_frontier[key] = node[0]
return new_frontier, next_layer
from graph_node import Graph
g_obj = Graph(parse)
words = sent.split(" ")
curr_layer = [[idx] for idx in range(len(words))]
nodes = {idx[0]: g_obj.idx_dict[idx[0]] for idx in curr_layer}
next_nodes = nodes
next_layer = curr_layer
layered_solution = [curr_layer]
while True:
next_nodes, next_layer = refine_layer(next_nodes, next_layer, g_obj)
layered_solution.append(next_layer)
if len(next_layer) == 1:
break
return layered_solution
def parse_tense_patched(sent):
parse = convert_to_parse(sent, parse_tense(sent), do_postprocess=False)
return convert_into_layerwise_format(sent, parse)
if __name__ == "__main__":
from data_utils.lm_dataset_helpers import read_lm_data
from data_utils.tense_inflection_helpers import read_ti_data
in_sentences, _ = read_ti_data(["train"], do_process=False)
import random
for idx in random.sample([idx for idx in range(10000)], k=1000):
sent = in_sentences[idx].strip().split("\t")[0].strip().lower()
parse = convert_to_parse(sent, parse_tense(sent), do_postprocess=False)
assert parse == convert_to_parse(
sent, convert_into_layerwise_format(sent, parse), do_postprocess=False
)