-
Notifications
You must be signed in to change notification settings - Fork 9
/
dataamr.py
486 lines (424 loc) · 17.5 KB
/
dataamr.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
from stog.data.dataset_readers.amr_parsing.io import AMRIO
from extra.utils import LongTensor
from extra.settings import PAD_IDX, PAD, OOV, OOV_IDX, BOS, BOS_IDX, \
EOS, EOS_IDX
from tqdm import tqdm
import logging
logger = logging.getLogger(__file__)
def batch_data(amr_data, batch_size=20):
data_train = []
src_batch = []
trg_batch = []
src_batch_len = 0
trg_batch_len = 0
for src, trg in zip(amr_data.X_train_ints, amr_data.Y_train_ints):
if len(src) > src_batch_len:
src_batch_len = len(src)
if len(trg) > trg_batch_len:
trg_batch_len = len(trg)
src_batch.append(src)
trg_batch.append(trg)
if len(src_batch) == batch_size:
for seq in src_batch:
seq.extend([PAD_IDX] * (src_batch_len - len(seq)))
for seq in trg_batch:
seq.extend([PAD_IDX] * (trg_batch_len - len(seq)))
data_train.append((LongTensor(src_batch), LongTensor(trg_batch)))
src_batch = []
trg_batch = []
src_batch_len = 0
trg_batch_len = 0
data_dev = []
for src, trg in zip(amr_data.X_dev_ints, amr_data.Y_dev_ints):
if len(src) > src_batch_len:
src_batch_len = len(src)
if len(trg) > trg_batch_len:
trg_batch_len = len(trg)
src_batch.append(src)
trg_batch.append(trg)
if len(src_batch) == batch_size:
for seq in src_batch:
seq.extend([PAD_IDX] * (src_batch_len - len(seq)))
for seq in trg_batch:
seq.extend([PAD_IDX] * (trg_batch_len - len(seq)))
data_dev.append((LongTensor(src_batch), LongTensor(trg_batch)))
src_batch = []
trg_batch = []
src_batch_len = 0
trg_batch_len = 0
data_test = []
for src, trg in zip(amr_data.X_test_ints, amr_data.Y_test_ints):
if len(src) > src_batch_len:
src_batch_len = len(src)
if len(trg) > trg_batch_len:
trg_batch_len = len(trg)
src_batch.append(src)
trg_batch.append(trg)
if len(src_batch) == batch_size:
for seq in src_batch:
seq.extend([PAD_IDX] * (src_batch_len - len(seq)))
for seq in trg_batch:
seq.extend([PAD_IDX] * (trg_batch_len - len(seq)))
data_test.append((LongTensor(src_batch), LongTensor(trg_batch)))
src_batch = []
trg_batch = []
src_batch_len = 0
trg_batch_len = 0
print("Training data size: %d" % (len(data_train) * batch_size))
print("Training batch size: %d" % batch_size)
print("Dev data size: %d" % (len(data_dev) * batch_size))
print("Dev batch size: %d" % batch_size)
print("Test data size: %d" % (len(data_test) * batch_size))
print("Test batch size: %d" % batch_size)
return data_train, data_dev, data_test
class AMRData():
def __init__(self, train_file, dev_file, test_file, silver,
input_format="raw", use_silver_data=False, small=False):
# Include atributes of each node to the linearized version of the graph
self.use_silver_data = use_silver_data
self.input_format = input_format
self.small = small
self.train_file = train_file
self.X_train = list()
self.Y_train = list()
self.Y_train_tok = list()
self.X_train_simple = list()
self.X_train_simple_attributes = list()
self.X_train_simple_only_nodes = list()
self.X_train_concepts = list()
self.X_train_ints = list()
self.X_train_raw = list()
self.Y_train_ints = list()
self.amr_train = None
self.silver_train_file = silver
self.X_silver_train = list()
self.Y_silver_train = list()
self.Y_silver_train_tok = list()
self.X_silver_train_simple = list()
self.X_silver_train_simple_attributes = list()
self.X_silver_train_simple_only_nodes = list()
self.X_silver_train_concepts = list()
self.X_silver_train_ints = list()
self.X_silver_train_raw = list()
self.Y_silver_train_ints = list()
self.amr_silver_train = None
self.dev_file = dev_file
self.X_dev = list()
self.Y_dev = list()
self.Y_dev_tok = list()
self.X_dev_simple = list()
self.X_dev_simple_attributes = list()
self.X_dev_simple_only_nodes = list()
self.X_dev_concepts = list()
self.X_dev_ints = list()
self.X_dev_raw = list()
self.Y_dev_ints = list()
self.test_file = test_file
self.X_test = list()
self.Y_test = list()
self.Y_test_tok = list()
self.X_test_simple = list()
self.X_test_simple_attributes = list()
self.X_test_simple_only_nodes = list()
self.X_test_ints = list()
self.X_test_raw = list()
self.Y_test_ints = list()
self.edges = list()
self.edges_w_attributes = list()
self.lin_to_int = {
PAD: PAD_IDX,
BOS: BOS_IDX,
EOS: EOS_IDX,
OOV: OOV_IDX}
self.int_to_lin = {
PAD_IDX: PAD,
BOS_IDX: BOS,
EOS_IDX: EOS,
OOV_IDX: OOV}
self.word_to_int = {
PAD: PAD_IDX,
BOS: BOS_IDX,
EOS: EOS_IDX,
OOV: OOV_IDX}
self.int_to_word = {
PAD_IDX: PAD,
BOS_IDX: BOS,
EOS_IDX: EOS,
OOV_IDX: OOV}
def get_list(self, amr):
if self.input_format == "linearized_simple":
with_attributes = False
else:
with_attributes = True
dfs_list = amr.graph.get_list_node()
out_list = list()
for n1, t, n2 in dfs_list:
try:
out_list += [":"+t, n1.__repr__()]
except BaseException:
return None
# If the nodes has attributes, itter through it and add it to the
# list
if with_attributes:
if len(n1.attributes) > 1:
for attr in n1.attributes[1:]:
if type(attr[1]) != str():
attr_tmp = str(attr[1])
else:
attr_tmp = attr[1]
# Attach to final list
out_list += [":"+attr[0], attr_tmp]
return out_list
# Remove not needed symbols
def simplify(self, step):
if step.startswith(":"):
return step, True
step = step.replace(" ", "")
step = step.replace('"', "")
step = step.replace("_", " ")
if "/" in step:
step = step.split("/")[1]
if step != '-':
step = step.split("-")[0]
return step, False
# Main loading method
def load_data(self):
logger.info("Parsing and linearizing the AMR dataset")
train_amr = AMRIO.read(self.train_file)
for i, amr in tqdm(enumerate(train_amr), desc='Train AMR'):
# Raw version
if self.small and i > 50:
break
raw_amr = []
for amr_line in str(amr.graph).splitlines():
striped_amr = amr_line.strip()
raw_amr.append(striped_amr)
self.X_train_raw.append(" ".join(raw_amr))
linearized_amr = self.get_list(amr)
self.X_train.append(linearized_amr[1:])
self.Y_train.append(amr.sentence)
self.Y_train_tok.append(amr.tokens)
# Vocabulary Create dictionaries and simplify list
simpl = list()
simpl_only_nodes = list()
for step in linearized_amr:
if step not in self.lin_to_int.keys():
self.lin_to_int[step] = len(self.lin_to_int)
self.int_to_lin[len(self.int_to_lin)] = step
# simplyfied AMR version
step, edge = self.simplify(step)
simpl.append(step)
if not step.startswith(":"):
simpl_only_nodes.append(step)
# Identify edges and save them
if edge and step not in self.edges:
self.edges.append(step)
self.X_train_simple.append(simpl)
self.X_train_simple_only_nodes.append(simpl_only_nodes)
sent = amr.sentence.split()
for word in sent:
if word not in self.word_to_int.keys():
self.word_to_int[word] = len(self.word_to_int)
self.int_to_word[len(self.int_to_word)] = word
if self.use_silver_data:
print("Processing silver data from", self.silver_train_file)
ii = 0
silver_train_amr = AMRIO.read(self.silver_train_file)
for i, amr in enumerate(silver_train_amr):
if self.small and i > 50:
break
# Raw version
raw_amr = []
ii += 1
linearized_amr = self.get_list(amr)
if linearized_amr is None:
continue
for amr_line in str(amr.graph).splitlines():
striped_amr = amr_line.strip()
raw_amr.append(striped_amr)
self.X_silver_train_raw.append(" ".join(raw_amr))
self.X_silver_train.append(linearized_amr[1:])
self.Y_silver_train.append(amr.sentence)
self.Y_silver_train_tok.append(amr.tokens)
# Vocabulary Create dictionaries and simplify list
simpl = list()
simpl_only_nodes = list()
for step in linearized_amr:
if step not in self.lin_to_int.keys():
self.lin_to_int[step] = len(self.lin_to_int)
self.int_to_lin[len(self.int_to_lin)] = step
# simplyfied AMR version
step, edge = self.simplify(step)
simpl.append(step)
if not step.startswith(":"):
simpl_only_nodes.append(step)
# Identify edges and save them
if edge and step not in self.edges:
self.edges.append(step)
self.X_silver_train_simple.append(simpl)
self.X_silver_train_simple_only_nodes.append(simpl_only_nodes)
sent = amr.sentence.split()
for word in sent:
if word not in self.word_to_int.keys():
self.word_to_int[word] = len(self.word_to_int)
self.int_to_word[len(self.int_to_word)] = word
print("Silver data with size:", len(self.X_silver_train_raw))
else:
print("No silver data performed")
dev_amr = AMRIO.read(self.dev_file)
for i, amr in tqdm(enumerate(dev_amr), desc='Dev AMR'):
if self.small and i > 50:
break
# Raw input
raw_amr = []
for amr_line in str(amr.graph).splitlines():
striped_amr = amr_line.strip()
raw_amr.append(striped_amr)
self.X_dev_raw.append(" ".join(raw_amr))
linearized_amr = self.get_list(amr)
self.X_dev.append(linearized_amr[1:])
self.Y_dev.append(amr.sentence)
self.Y_dev_tok.append(amr.tokens)
# simplyfied AMR version
simpl = list()
simpl_only_nodes = list()
for step in linearized_amr:
step, edge = self.simplify(step)
simpl.append(step)
if not step.startswith(":"):
simpl_only_nodes.append(step)
if edge and step not in self.edges:
self.edges.append(step)
self.X_dev_simple.append(simpl)
self.X_dev_simple_only_nodes.append(simpl_only_nodes)
test_amr = AMRIO.read(self.test_file)
self.amr_test = test_amr
for i, amr in tqdm(enumerate(test_amr), desc='Test AMR'):
if self.small and i > 50:
break
# Raw version
raw_amr = []
for amr_line in str(amr.graph).splitlines():
striped_amr = amr_line.strip()
raw_amr.append(striped_amr)
self.X_test_raw.append(" ".join(raw_amr))
linearized_amr = self.get_list(amr)
self.X_test.append(linearized_amr[1:])
self.Y_test.append(amr.sentence)
self.Y_test_tok.append(amr.tokens)
# simplyfied AMR version
simpl = list()
simpl_only_nodes = list()
for step in linearized_amr:
step, edge = self.simplify(step)
simpl.append(step)
if not step.startswith(":"):
simpl_only_nodes.append(step)
if edge and step not in self.edges:
self.edges.append(step)
self.X_test_simple.append(simpl)
self.X_test_simple_only_nodes.append(simpl_only_nodes)
def output_data(self, output_src_file, output_trg_file):
print("Write linearized AMRs to file")
F_train_src = open(output_src_file+".train", "w")
F_train_raw_src = open(output_src_file+".amr.train", "w")
F_train_trg = open(output_trg_file+".train", "w")
F_train_tok_trg = open(output_trg_file+".tok.train", "w")
F_dev_src = open(output_src_file+".dev", "w")
F_dev_raw_src = open(output_src_file+".amr.dev", "w")
F_dev_trg = open(output_trg_file+".dev", "w")
F_dev_tok_trg = open(output_trg_file+".tok.dev", "w")
F_test_src = open(output_src_file+".test", "w")
F_test_raw_src = open(output_src_file+".amr.test", "w")
F_test_trg = open(output_trg_file+".test", "w")
F_test_tok_trg = open(output_trg_file+".tok.test", "w")
print(
"TRAIN: src lin:", len(
self.X_train), "src amr", len(
self.X_train_raw), "trg text", len(
self.Y_train_tok), "trg tok", len(
self.Y_train_tok))
for x, x_raw, y, y_tok in zip(
self.X_train, self.X_train_raw, self.Y_train, self.Y_train_tok):
print(" ".join(x), file=F_train_src)
print(y_tok, file=F_train_trg)
print(x_raw, file=F_train_raw_src)
print(y_tok, file=F_train_tok_trg)
print(
"dev: src lin:", len(
self.X_dev), "src amr", len(
self.X_dev_raw), "trg text", len(
self.Y_dev), "trg tok", len(
self.Y_dev_tok))
for x, x_raw, y, y_tok in zip(
self.X_dev, self.X_dev_raw, self.Y_dev, self.Y_dev_tok):
print(" ".join(x), file=F_dev_src)
print(y_tok, file=F_dev_trg)
print(x_raw, file=F_dev_raw_src)
print(y_tok, file=F_dev_tok_trg)
print(
"test: src lin:", len(
self.X_test), "src amr", len(
self.X_test_raw), "trg text", len(
self.Y_test), "trg tok", len(
self.Y_test_tok))
for x, x_raw, y, y_tok in zip(
self.X_test, self.X_test_raw, self.Y_test, self.Y_test_tok):
print(" ".join(x), file=F_test_src)
print(y_tok, file=F_test_trg)
print(x_raw, file=F_test_raw_src)
print(y_tok, file=F_test_tok_trg)
F_train_src.close()
F_train_trg.close()
F_train_raw_src.close()
F_train_tok_trg.close()
F_dev_src.close()
F_dev_trg.close()
F_dev_raw_src.close()
F_dev_tok_trg.close()
F_test_src.close()
F_test_trg.close()
F_test_raw_src.close()
F_test_tok_trg.close()
def to_ints(self):
print("Transform to ints")
pbar = tqdm(total=len(self.X_train)+len(self.X_dev)+len(self.X_test))
for x, y in zip(self.X_train, self.Y_train):
self.X_train_ints.append([self.lin_to_int[x_i]
for x_i in x] + [EOS_IDX])
self.Y_train_ints.append([self.word_to_int[y_i]
for y_i in y.split()] + [EOS_IDX])
pbar.update(1)
for x, y in zip(self.X_dev, self.Y_dev):
x_in = list()
y_in = list()
for x_i in x:
if x_i not in self.lin_to_int.keys():
x_in.append(self.lin_to_int[OOV])
else:
x_in.append(self.lin_to_int[x_i])
for y_i in y:
if y_i not in self.word_to_int.keys():
y_in.append(self.word_to_int[OOV])
else:
y_in.append(self.word_to_int[y_i])
self.Y_dev_ints.append(y_in + [EOS_IDX])
self.X_dev_ints.append(x_in + [EOS_IDX])
pbar.update(1)
for x, y in zip(self.X_test, self.Y_test):
x_in = list()
y_in = list()
for x_i in x:
if x_i not in self.lin_to_int.keys():
x_in.append(self.lin_to_int[OOV])
else:
x_in.append(self.lin_to_int[x_i])
for y_i in y:
if y_i not in self.word_to_int.keys():
y_in.append(self.word_to_int[OOV])
else:
y_in.append(self.word_to_int[y_i])
self.Y_test_ints.append(y_in + [EOS_IDX])
self.X_test_ints.append(x_in + [EOS_IDX])
pbar.update(1)
pbar.close()