-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata_utils.py
More file actions
679 lines (506 loc) · 23 KB
/
data_utils.py
File metadata and controls
679 lines (506 loc) · 23 KB
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
import os
import torch
from torch.utils import data
import numpy as np
from tabulate import tabulate
import random
import torch.nn.functional as F
class InputFeatures(object):
"""A single set of features of data.
Result of convert_examples_to_features(ReviewExample)
"""
def __init__(self, input_ids, attention_mask, tag_ids, label_id, has_rationale, start_labels, end_labels, span_labels):
self.input_ids = input_ids
self.attention_mask = attention_mask
self.tag_ids = tag_ids
self.label_id = label_id
self.has_rationale = has_rationale
self.start_labels = start_labels
self.end_labels = end_labels
self.span_labels = span_labels
class DataProcessor(object):
"""Base class for data converters for rationale identification data sets."""
def get_train_examples(self, data_dir):
"""Gets a collection of examples for the train set."""
raise NotImplementedError()
def get_dev_examples(self, data_dir):
"""Gets a collection of examples for the dev set."""
raise NotImplementedError()
def get_labels(self):
"""Gets the list of labels for this data set."""
raise NotImplementedError()
@classmethod
def _read_data(cls, input_file):
""" Reads the data """
lines = open(input_file).readlines()
return lines
class MovieReviewsProcessor(DataProcessor):
def __init__(self):
self._tags = ['START', 'END', '0', '1']
self._tag_map = {tag: i for i, tag in enumerate(self._tags)}
self.fraction_rationales = 1.0
def set_fraction_rationales(self, fraction_rationales):
self.fraction_rationales = fraction_rationales
def get_train_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "train.txt")))
def get_dev_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "dev.txt")))
def get_test_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "test.txt")))
def get_labels(self):
return ["0", "1"]
def get_tags(self):
return self._tags
def get_num_labels(self):
return len(self.get_labels())
def get_num_tags(self):
return len(self._tags)
def get_tag_map(self):
return self._tag_map
def get_start_tag_id(self):
return self._tag_map['START']
def get_stop_tag_id(self):
return self._tag_map['END']
def _create_examples(self, examples):
# BOGUS method
return examples
def _read_data_with_block(self, filename):
content_lines = open(filename).readlines()
tag_lines = open(filename + ".block").readlines()
for idx in range(len(content_lines)):
content_lines[idx] = content_lines[idx].replace("</POS>", "")
content_lines[idx] = content_lines[idx].replace("<POS>", "")
content_lines[idx] = content_lines[idx].replace("</NEG>", "")
content_lines[idx] = content_lines[idx].replace("<NEG>", "")
# remove certain rationales from the train set if needed
if "train" in filename and self.fraction_rationales != 1.0:
# print ("debug mode --- filtering out sentences")
for idx in range(len(tag_lines)):
# allow the input if it does *NOT* contain any rationale
if "-1" in tag_lines[idx]:
continue
# it contains a rationale
if random.random() > self.fraction_rationales:
# remove the rationale
tag_lines[idx] = " ".join(["-1" for _ in \
range(len(tag_lines[idx].strip().split()))])
return [*zip(content_lines, tag_lines)]
class EsnliProcessor(DataProcessor):
def __init__(self):
self._tags = ['START', 'END', '0', '1', '2']
self._tag_map = {tag: i for i, tag in enumerate(self._tags)}
self.fraction_rationales = 1.0
def set_fraction_rationales(self, fraction_rationales):
self.fraction_rationales = fraction_rationales
def get_train_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "train.txt")))
def get_dev_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "dev.txt")))
def get_test_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "test.txt")))
def get_labels(self):
return ["0", "1", "2"]
def get_tags(self):
return self._tags
def get_num_labels(self):
return len(self.get_labels())
def get_num_tags(self):
return len(self._tags)
def get_tag_map(self):
return self._tag_map
def get_start_tag_id(self):
return self._tag_map['START']
def get_stop_tag_id(self):
return self._tag_map['END']
def _create_examples(self, examples):
# BOGUS method
return examples
def _read_data_with_block(self, filename):
content_lines = open(filename).readlines()
tag_lines = open(filename + ".block").readlines()
for idx in range(len(content_lines)):
content_lines[idx] = content_lines[idx].replace("</POS>", "")
content_lines[idx] = content_lines[idx].replace("<POS>", "")
content_lines[idx] = content_lines[idx].replace("</NEG>", "")
content_lines[idx] = content_lines[idx].replace("<NEG>", "")
# remove certain rationales from the train set if needed
if "train" in filename and self.fraction_rationales != 1.0:
# print ("debug mode --- filtering out sentences")
for idx in range(len(tag_lines)):
# allow the input if it does *NOT* contain any rationale
if "-1" in tag_lines[idx]:
continue
# it contains a rationale
if random.random() > self.fraction_rationales:
# remove the rationale
tag_lines[idx] = " ".join(["-1" for _ in \
range(len(tag_lines[idx].strip().split()))])
return [*zip(content_lines, tag_lines)]
class EvinfProcessor(DataProcessor):
def __init__(self):
self._tags = ['START', 'END', '0', '1', '2']
self._tag_map = {tag: i for i, tag in enumerate(self._tags)}
self.fraction_rationales = 1.0
def set_fraction_rationales(self, fraction_rationales):
self.fraction_rationales = fraction_rationales
def get_train_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "train.txt")))
def get_dev_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "dev.txt")))
def get_test_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "test.txt")))
def get_labels(self):
return ["0", "1", "2"]
def get_tags(self):
return self._tags
def get_num_labels(self):
return len(self.get_labels())
def get_num_tags(self):
return len(self._tags)
def get_tag_map(self):
return self._tag_map
def get_start_tag_id(self):
return self._tag_map['START']
def get_stop_tag_id(self):
return self._tag_map['END']
def _create_examples(self, examples):
# BOGUS method
return examples
def _read_data_with_block(self, filename):
content_lines = open(filename).readlines()
tag_lines = open(filename + ".block").readlines()
for idx in range(len(content_lines)):
content_lines[idx] = content_lines[idx].replace("</POS>", "")
content_lines[idx] = content_lines[idx].replace("<POS>", "")
content_lines[idx] = content_lines[idx].replace("</NEG>", "")
content_lines[idx] = content_lines[idx].replace("<NEG>", "")
# remove certain rationales from the train set if needed
if "train" in filename and self.fraction_rationales != 1.0:
# print ("debug mode --- filtering out sentences")
for idx in range(len(tag_lines)):
# allow the input if it does *NOT* contain any rationale
if "-1" in tag_lines[idx]:
continue
# it contains a rationale
if random.random() > self.fraction_rationales:
# remove the rationale
tag_lines[idx] = " ".join(["-1" for _ in \
range(len(tag_lines[idx].strip().split()))])
return [*zip(content_lines, tag_lines)]
class MultircProcessor(DataProcessor):
def __init__(self):
self._tags = ['START', 'END', '0', '1']
self._tag_map = {tag: i for i, tag in enumerate(self._tags)}
self.fraction_rationales = 1.0
def set_fraction_rationales(self, fraction_rationales):
self.fraction_rationales = fraction_rationales
def get_train_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "train.txt")))
def get_dev_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "dev.txt")))
def get_test_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "test.txt")))
def get_labels(self):
return ["0", "1"]
def get_tags(self):
return self._tags
def get_num_labels(self):
return len(self.get_labels())
def get_num_tags(self):
return len(self._tags)
def get_tag_map(self):
return self._tag_map
def get_start_tag_id(self):
return self._tag_map['START']
def get_stop_tag_id(self):
return self._tag_map['END']
def _create_examples(self, examples):
# BOGUS method
return examples
def _read_data_with_block(self, filename):
content_lines = open(filename).readlines()
tag_lines = open(filename + ".block").readlines()
for idx in range(len(content_lines)):
content_lines[idx] = content_lines[idx].replace("</POS>", "")
content_lines[idx] = content_lines[idx].replace("<POS>", "")
content_lines[idx] = content_lines[idx].replace("</NEG>", "")
content_lines[idx] = content_lines[idx].replace("<NEG>", "")
# remove certain rationales from the train set if needed
if "train" in filename and self.fraction_rationales != 1.0:
# print ("debug mode --- filtering out sentences")
for idx in range(len(tag_lines)):
# allow the input if it does *NOT* contain any rationale
if "-1" in tag_lines[idx]:
continue
# it contains a rationale
if random.random() > self.fraction_rationales:
# remove the rationale
tag_lines[idx] = " ".join(["-1" for _ in \
range(len(tag_lines[idx].strip().split()))])
return [*zip(content_lines, tag_lines)]
class PropagandaProcessor(DataProcessor):
def __init__(self):
self._tags = ['START', 'END', '0', '1']
self._tag_map = {tag: i for i, tag in enumerate(self._tags)}
self.fraction_rationales = 1.0
def set_fraction_rationales(self, fraction_rationales):
self.fraction_rationales = fraction_rationales
def get_train_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "train.txt")))
def get_dev_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "dev.txt")))
def get_test_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "test.txt")))
def get_labels(self):
return ["0", "1"]
def get_tags(self):
return self._tags
def get_num_labels(self):
return len(self.get_labels())
def get_num_tags(self):
return len(self._tags)
def get_tag_map(self):
return self._tag_map
def get_start_tag_id(self):
return self._tag_map['START']
def get_stop_tag_id(self):
return self._tag_map['END']
def _create_examples(self, examples):
# BOGUS method
return examples
def _read_data_with_block(self, filename):
content_lines = open(filename).readlines()
tag_lines = open(filename + ".block").readlines()
# remove certain rationales from the train set if needed
if "train" in filename and self.fraction_rationales != 1.0:
# print ("debug mode --- filtering out sentences")
for idx in range(len(tag_lines)):
# allow the input if it does *NOT* contain any rationale
if "-1" in tag_lines[idx]:
continue
# it contains a rationale
if random.random() > self.fraction_rationales:
# remove the rationale
tag_lines[idx] = " ".join(["-1" for _ in \
range(len(tag_lines[idx].strip().split()))])
return [*zip(content_lines, tag_lines)]
class MultiRCProcessor(DataProcessor):
def __init__(self):
self._tags = ['START', 'END', '0', '1']
# different tag for question, middle sep, and answer?
self._tag_map = {tag: i for i, tag in enumerate(self._tags)}
self.fraction_rationales = 1.0
def set_fraction_rationales(self, fraction_rationales):
self.fraction_rationales = fraction_rationales
def get_train_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "train.txt")))
def get_dev_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "dev.txt")))
def get_test_examples(self, data_dir):
return self._create_examples(
self._read_data_with_block(os.path.join(data_dir, "test.txt")))
def get_labels(self):
return ["0", "1"]
def get_tags(self):
return self._tags
def get_num_labels(self):
return len(self.get_labels())
def get_num_tags(self):
return len(self._tags)
def get_tag_map(self):
return self._tag_map
def get_start_tag_id(self):
return self._tag_map['START']
def get_stop_tag_id(self):
return self._tag_map['END']
def _create_examples(self, examples):
# BOGUS method
return examples
def _read_data_with_block(self, filename):
content_lines = open(filename).readlines()
tag_lines = open(filename + ".block").readlines()
# remove certain rationales from the train set if needed
if "train" in filename and self.fraction_rationales != 1.0:
# print ("debug mode --- filtering out sentences")
for idx in range(len(tag_lines)):
# allow the input if it does *NOT* contain any rationale
if "-1" in tag_lines[idx]:
continue
# it contains a rationale
if random.random() > self.fraction_rationales:
# remove the rationale
tag_lines[idx] = " ".join(["-1" for _ in \
range(len(tag_lines[idx].strip().split()))])
return [*zip(content_lines, tag_lines)]
def tags_to_positions(tags, has_rationale, max_len):
seq_length = max_len # len(tags)
start_labels, end_labels, span_labels = [0] * seq_length, [0] * seq_length, [[0]*seq_length]*seq_length
span_labels = np.array(span_labels)
if has_rationale:
tlen = len(tags)
start_positions = []
end_positions = []
for i in range(tlen):
if (i-1 >= 0) and (tags[i] == 3 and tags[i-1] != 3):
start_labels[i] = 1
start_positions.append(i)
if (i+1 <= tlen-1) and (tags[i+1] != 3 and tags[i] == 3):
end_labels[i] = 1
end_positions.append(i)
# for i in range(seq_length):
# for j in range(seq_length):
# if start_labels[i] == 1 and end_labels[j] == 1:
# span_labels[i,j] = 1
for start, end in zip(start_positions, end_positions):
if start >= tlen or end >= tlen:
continue
span_labels[start, end] = 1
#############################################################
# idx = 0
# while idx < tlen:
# row_id = -1
# col_id = -1
# if tags[idx] == 3:
# row_id = idx
# while (idx < tlen) and tags[idx] == 3:
# idx = idx + 1
# if idx < tlen:
# col_id = idx - 1
# if row_id != -1 and col_id != -1:
# span_labels[row_id][col_id] = 1
# idx = idx + 1
# # print(tags)
##############################################################
sstart_sum = sum(start_labels)
# print()
# print(sum(end_labels))
# print(sum(sum(span_labels)))
# print()
# if sstart_sum == 0:
# has_rationale = 0
# print(start_labels)
# print(end_labels)
# print(span_labels)
# for ind in range(tlen):
# if start_labels[ind] == 1:
# assert tags[ind] == 3
# if end_labels[ind] == 1:
# assert tags[ind] == 3
# for ind_j in range(tlen):
# if span_labels[ind][ind_j] == 1:
# assert tags[ind] == 3
# assert tags[ind_j] == 3
return start_labels, end_labels, span_labels, has_rationale
def input_to_features(example, tokenizer, tag_map, max_seq_len):
# news example is a tuple of content, tag
content, tags_input = example
label = content.split("\t")[0]
content = content.split("\t")[1]
tokens = ['[CLS]']
tags = [tag_map['START']]
has_rationale = not ("-1" in tags_input)
assert len(content.strip().split()) == len(tags_input.strip().split())
for i, (word, tag) in enumerate(zip(content.strip().split(), tags_input.strip().split())):
if word == "sep_token":
tokens.append("[SEP]")
tags.append(tag_map['0'])
continue
sub_words = tokenizer.tokenize(word)
if not sub_words or len(sub_words) == 0:
# can this even happen?? YES.... it does happen!!
# print ("Ignoring the weird word: ", word)
continue
tokens.extend(sub_words)
if tag != "-1":
tags.extend([tag_map[tag] for _ in range(len(sub_words))])
else:
tags.extend([-1 for _ in range(len(sub_words))])
tokens = tokens[:max_seq_len-1]
tags = tags[:max_seq_len-1]
tokens.append("[SEP]")
tags.append(tag_map['END'])
# print ('label = ', label)
# print ('content = ', content)
# print (tabulate(zip(tokens, tags)))
input_ids = tokenizer.convert_tokens_to_ids(tokens)
attention_mask = [1] * len(tokens)
# if has_rationale:
# print(tags_input)
start_labels, end_labels, span_labels, has_rationale = tags_to_positions(tags, has_rationale, max_seq_len)
return InputFeatures(input_ids, attention_mask, tags, int(label), int(has_rationale), start_labels, end_labels, span_labels)
class DatasetWithRationales(data.Dataset):
def __init__(self, examples, tokenizer, tag_map, max_seq_len, dataset="movie_reviews"):
super(DatasetWithRationales, self).__init__()
self.examples = examples
self.tokenizer = tokenizer
self.tag_map = tag_map
self.max_seq_len = max_seq_len
self.dataset = dataset
def __len__(self):
return len(self.examples)
def __getitem__(self, idx):
# if self.dataset == "movie_reviews": # modified delete
if self.dataset in ['movie_reviews', 'esnli', 'evinf', 'multirc']: # modified add
features = input_to_features(self.examples[idx], self.tokenizer,
self.tag_map, self.max_seq_len)
elif self.dataset == "propaganda":
features = input_to_features(self.examples[idx], self.tokenizer,
self.tag_map, self.max_seq_len)
elif self.dataset == "multi_rc":
features = input_to_features(self.examples[idx], self.tokenizer,
self.tag_map, self.max_seq_len)
else:
raise Exception("No dataset selected....")
return features.input_ids, features.attention_mask, \
features.label_id, features.tag_ids, features.has_rationale, features.start_labels, features.end_labels, features.span_labels
@classmethod
def pad(cls, batch):
float_type = torch.FloatTensor
long_type = torch.LongTensor
bool_type = torch.bool
is_cuda = torch.cuda.is_available()
if is_cuda:
float_type = torch.cuda.FloatTensor
long_type = torch.cuda.LongTensor
seqlen_list = [len(sample[0]) for sample in batch]
maxlen = np.array(seqlen_list).max()
f = lambda x, seqlen: [sample[x] + [0] * (seqlen - len(sample[x])) for sample in batch]
f_single = lambda x: [sample[x] for sample in batch]
f_two_dims = lambda x, seqlen: [np.array(sample[x])[:seqlen, :seqlen] for sample in batch]
f_one_dims = lambda x, seqlen: [np.array(sample[x])[:seqlen] for sample in batch]
# 0: X for padding
# input_ids, attention_mask, label_ids, tag_ids, has_rationale = batch
input_ids_list = torch.Tensor(f(0, maxlen)).type(long_type)
attention_mask_list = torch.Tensor(f(1, maxlen)).type(long_type)
label_ids_list = torch.Tensor(f_single(2)).type(long_type)
tag_ids_list = torch.Tensor(f(3, maxlen)).type(long_type)
rationale_list = torch.Tensor(f_single(4)).type(bool_type)
start_labels_list = torch.Tensor(f_one_dims(5, maxlen)).type(long_type)
end_labels_list = torch.Tensor(f_one_dims(6, maxlen)).type(long_type)
span_labels_list = torch.Tensor(f_two_dims(7, maxlen)).type(long_type)
return input_ids_list, attention_mask_list, label_ids_list, tag_ids_list, rationale_list, start_labels_list, end_labels_list, span_labels_list
if __name__ == "__main__":
start_labels, end_labels, span_labels, _ = tags_to_positions(tags=[0, 2, 3, 2, 3, 2, 3, 1], has_rationale=1, max_len=10)
print(start_labels)
print()
print(end_labels)
print()
print(span_labels)
print()