forked from Sotwi-zz/NL-Augmenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
449 lines (380 loc) Β· 16.3 KB
/
dataset.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
from __future__ import annotations
from typing import Iterable, List
from tqdm import tqdm
from interfaces import Operation
from interfaces.QuestionAnswerOperation import QuestionAnswerOperation
from interfaces.SentenceOperation import (
SentenceAndTargetOperation,
SentenceAndTargetsOperation,
SentenceOperation,
)
from tasks.TaskTypes import TaskType
class BaseDataset(Iterable):
def __init__(self, data: Iterable):
self.data = data
def apply_filter(self, condition: Operation):
raise NotImplementedError(
"BaseDataset does not implement this function."
)
def apply_transformation(self, transformation: Operation):
raise NotImplementedError(
"BaseDataset does not implement this function."
)
def __iter__(self):
raise NotImplementedError(
"BaseDataset does not implement this function."
)
def __len__(self):
raise NotImplementedError(
"BaseDataset does not implement this function."
)
def __or__(self, other):
raise NotImplementedError(
"BaseDataset does not implement this function."
)
def __and__(self, other):
raise NotImplementedError(
"BaseDataset does not implement this function."
)
def __sub__(self, other):
raise NotImplementedError(
"BaseDataset does not implement this function"
)
"""
Dataset for data in plain texts where each line is a datapoint
"""
class TextLineDataset(BaseDataset):
tasks = [TaskType.TEXT_CLASSIFICATION]
def __init__(self, data: List[str], labels: List):
super(TextLineDataset, self).__init__(data)
assert len(data) == len(
labels
), "The number of datapoint should be the same as the number of labels"
self.labels = labels
self.mapping = {
datapoint: label
for datapoint, label in zip(self.data, self.labels)
}
@classmethod
def from_huggingface(cls, dataset, task_type, fields, max_size=None):
data = []
labels = []
max_size = max_size or len(dataset)
for example in list(dataset)[:max_size]:
data.append(example[fields[0]])
labels.append(example[fields[1]])
return cls(data, labels)
def apply_filter(self, filter: SentenceOperation) -> TextLineDataset:
filtered_data = []
filtered_labels = []
print("Applying filtering:")
for datapoint, label in tqdm(
zip(self.data, self.labels), total=len(self.data)
):
if filter.filter(datapoint):
filtered_data.append(datapoint)
filtered_labels.append(label)
return TextLineDataset(filtered_data, filtered_labels)
def apply_transformation(
self, transformation: SentenceOperation
) -> TextLineDataset:
transformed_data = []
print("Applying transformation:")
# calculating ratio of transformed example to unchanged example
successful_num = 0
failed_num = 0
for line in tqdm(self.data):
pt_examples = transformation.generate(line)
successful_pt, failed_pt = transformation.compare(
line, pt_examples
)
successful_num += successful_pt
failed_num += failed_pt
transformed_data.extend(pt_examples)
total_num = successful_num + failed_num
print(
"Finished transformation! {} examples generated from {} original examples, with {} successfully transformed and {} unchanged ({} perturb rate)".format(
total_num,
len(self.data),
successful_num,
failed_num,
successful_num / total_num if total_num > 0 else 0,
)
)
if total_num == 0: return None
return TextLineDataset(transformed_data, self.labels)
def __iter__(self):
for text, label in zip(self.data, self.labels):
yield (text, label)
def __len__(self):
return len(self.data)
def __or__(self, other: TextLineDataset) -> TextLineDataset:
data = list(set(self.data).union(set(other.data)))
mapping = {**self.mapping, **other.mapping}
labels = [mapping[datapoint] for datapoint in data]
return TextLineDataset(data, labels)
def __and__(self, other: TextLineDataset) -> TextLineDataset:
data = list(set(self.data).intersection(set(other.data)))
labels = [self.mapping[datapoint] for datapoint in data]
return TextLineDataset(data, labels)
def __sub__(self, other: TextLineDataset) -> TextLineDataset:
data = list(set(self.data).difference(set(other.data)))
labels = [self.mapping[datapoint] for datapoint in data]
return TextLineDataset(data, labels)
"""
Dataset for data in format of key-value pairs, e.g. data read from jsonl file
"""
class KeyValueDataset(BaseDataset):
tasks = [
TaskType.TEXT_TO_TEXT_GENERATION,
TaskType.QUESTION_ANSWERING,
TaskType.QUESTION_GENERATION,
TaskType.TEXT_CLASSIFICATION, # for >1 field classification
]
# data: input data samples read from jsonl file
# task_type: task type specified
# fields: list of relevant keys (e.g. to your sentence/target, context/question/answer, etc.)
# The number of keys should be aligned with the transform/filter operation.
def __init__(
self,
data: List[dict],
task_type=TaskType.TEXT_TO_TEXT_GENERATION,
fields: List[str] = None,
):
super(KeyValueDataset, self).__init__(data)
self.task_type = task_type
self.fields = fields
self.operation_type = None
@classmethod
def from_huggingface(cls, dataset, task_type, fields, max_size=None):
data = []
max_size = max_size or len(dataset)
if task_type not in [
TaskType.QUESTION_ANSWERING,
TaskType.QUESTION_GENERATION,
]:
for example in list(dataset)[:max_size]:
data.append({key: example[key] for key in fields})
else:
# this is an ugly implementation, which hard-codes the squad data format
# TODO might need a more elegant way to deal with the fields with hierarchy, e.g. the answers field in squad data (exampl['answers']['text'])
for example in list(dataset)[:max_size]:
data.append(
{
fields[0]: example[fields[0]],
fields[1]: example[fields[1]],
fields[2]: example[fields[2]]["text"],
}
)
return cls(data, task_type, fields)
def _analyze(self, subfields: List[str]):
if subfields is None:
subfields = self.fields
assert set(subfields) <= set(
self.fields
), "Your can only choose from fields within {}".format(self.fields)
if self.task_type == TaskType.TEXT_TO_TEXT_GENERATION:
if len(subfields) == 1:
self.operation_type = "sentence"
elif len(subfields) == 2:
self.operation_type = "sentence_and_target"
elif len(subfields) > 2:
self.operation_type = "sentence_and_targets"
elif self.task_type in [
TaskType.QUESTION_ANSWERING,
TaskType.QUESTION_GENERATION,
]:
# this is in case that one would like to use SentenceOperation (e.g. butter finger) on specific fields (e.g. only the question)
if len(subfields) == 1:
self.operation_type = "sentence"
elif len(subfields) == 2:
self.operation_type = "sentence_and_target"
else:
self.operation_type = "question_answer"
elif self.task_type in [TaskType.TEXT_CLASSIFICATION]:
self.operation_type = "sentence"
filter_func = self.__getattribute__(
"_apply_" + self.operation_type + "_filter"
)
transformation_func = self.__getattribute__(
"_apply_" + self.operation_type + "_transformation"
)
return filter_func, transformation_func
# this function is an adapter and will call the corresponding filter function for the task
# subfields: the fields to apply filter, it is a subset of self.fields
def apply_filter(
self, filter: Operation, subfields: List[str] = None
) -> KeyValueDataset:
filter_func, _ = self._analyze(subfields)
filtered_data = []
print("Applying filtering:")
for datapoint in tqdm(self.data):
if filter_func(datapoint, filter):
filtered_data.append(datapoint)
return KeyValueDataset(filtered_data, self.task_type, self.fields)
def _apply_sentence_filter(
self, datapoint: dict, filter: SentenceOperation
):
sentence = datapoint[self.fields[0]]
return filter.filter(sentence)
def _apply_sentence_and_target_filter(
self, datapoint: dict, filter: SentenceAndTargetOperation
):
sentence = datapoint[self.fields[0]]
target = datapoint[self.fields[1]]
return filter.filter(sentence, target)
def _apply_sentence_and_targets_filter(
self, datapoint: dict, filter: SentenceAndTargetsOperation
):
sentence = datapoint[self.fields[0]]
targets = [datapoint[target_key] for target_key in self.fields[1:]]
return filter.filter(sentence, targets)
def _apply_question_answer_filter(
self, datapoint: dict, filter: QuestionAnswerOperation
):
context = datapoint[self.fields[0]]
question = datapoint[self.fields[1]]
answers = [datapoint[answer_key] for answer_key in self.fields[2:]]
return filter.filter(context, question, answers)
# this function is an adapter and will call the corresponding transform function for the task
# subfields: the fields to apply transformation, it is a subset of self.fields
def apply_transformation(
self, transformation: Operation, subfields: List[str] = None
) -> KeyValueDataset:
_, transformation_func = self._analyze(subfields)
transformed_data = []
print("Applying transformation:")
# calculating ratio of transformed example to unchanged example
successful_num = 0
failed_num = 0
for datapoint in tqdm(self.data):
pt_examples = transformation_func(datapoint.copy(), transformation)
successful_pt, failed_pt = transformation.compare(
datapoint, pt_examples
)
successful_num += successful_pt
failed_num += failed_pt
transformed_data.extend(pt_examples) # don't want self.data to be changed
total_num = successful_num + failed_num
print(
"Finished transformation! {} examples generated from {} original examples, with {} successfully transformed and {} unchanged ({} perturb rate)".format(
total_num,
len(self.data),
successful_num,
failed_num,
successful_num / total_num if total_num > 0 else 0,
)
)
if total_num == 0: return None
return KeyValueDataset(transformed_data, self.task_type, self.fields)
def _apply_sentence_transformation(
self, datapoint: dict, transformation: SentenceOperation
):
sentence = datapoint[self.fields[0]]
transformed_sentence = transformation.generate(sentence)
datapoint[self.fields[0]] = transformed_sentence
return [datapoint]
def _apply_sentence_and_target_transformation(
self, datapoint: dict, transformation: SentenceAndTargetOperation
):
sentence = datapoint[self.fields[0]]
target = datapoint[self.fields[1]]
transformed_sentence, transformed_target = transformation.generate(
sentence, target
)
datapoint[self.fields[0]] = transformed_sentence
datapoint[self.fields[1]] = transformed_target
return [datapoint]
def _apply_sentence_and_targets_transformation(
self, datapoint: dict, transformation: SentenceAndTargetsOperation
):
sentence = datapoint[self.fields[0]]
targets = [datapoint[target_key] for target_key in self.fields[1:]]
transformed = transformation.generate(sentence, targets)
datapoints = []
for to in transformed:
datapoint_n = dict()
datapoint_n[self.fields[0]] = to[0]
for i, target_key in enumerate(self.fields[1:]):
datapoint[target_key] = to[1][
1 + i
] # targets starting from pos 1
datapoints.append(datapoint_n)
return datapoints
def _apply_question_answer_transformation(
self, datapoint: dict, transformation: QuestionAnswerOperation
):
context = datapoint[self.fields[0]]
question = datapoint[self.fields[1]]
answers = [datapoint[answer_key] for answer_key in self.fields[2:]]
transformed = transformation.generate(context, question, answers)
datapoints = []
for to in transformed:
datapoint_n = dict()
datapoint_n[self.fields[0]] = to[0]
datapoint_n[self.fields[1]] = to[1]
for i, answers_key in enumerate(self.fields[2:]):
datapoint_n[answers_key] = to[
2 + i
] # answers starting from pos 2
datapoints.append(datapoint_n)
return datapoints
def __iter__(self):
for datapoint in self.data:
yield (datapoint[field] for field in self.fields)
def __len__(self):
return len(self.data)
def _sanity_check(self, other: KeyValueDataset):
assert (
self.data[0].keys() == other.data[0].keys()
), "You cannot do dataset operation on datasets with different keys"
assert (
self.task_type == other.task_type
), "You cannot do dataset operation on datasets for different tasks"
assert len(self.fields) == len(
other.fields
), "You cannot do dataset operation on datasets with different number fields"
def _data2identifier(self, data: List[str]):
id2datapoint = {}
identifier2id = {}
identifiers = []
for idx, datapoint in enumerate(data):
id2datapoint[idx] = datapoint
# "|||" is a naive separator
identifier = "|||".join(
[datapoint[field] for field in self.fields]
)
identifiers.append(identifier)
identifier2id[identifier] = idx
identifiers = set(identifiers) # remove duplicates
return id2datapoint, identifier2id, identifiers
def _identifier2data(self, id2datapoint, identifier2id, identifiers):
result_data = []
for identifier in identifiers:
result_data.append(id2datapoint[identifier2id[identifier]])
return result_data
def __or__(self, other: KeyValueDataset) -> KeyValueDataset:
self._sanity_check(other)
id2datapoint, identifier2id, identifiers = self._data2identifier(
self.data + other.data
)
data = self._identifier2data(id2datapoint, identifier2id, identifiers)
return KeyValueDataset(data, self.task_type, self.fields)
def __and__(self, other: KeyValueDataset) -> KeyValueDataset:
self._sanity_check(other)
id2datapoint, identifier2id, identifiers = self._data2identifier(
self.data
)
_, _, identifiers2 = self._data2identifier(other.data)
identifiers = identifiers.intersection(identifiers2)
data = self._identifier2data(id2datapoint, identifier2id, identifiers)
return KeyValueDataset(data, self.task_type, self.fields)
def __sub__(self, other: KeyValueDataset) -> KeyValueDataset:
self._sanity_check(other)
id2datapoint, identifier2id, identifiers = self._data2identifier(
self.data
)
_, _, identifiers2 = self._data2identifier(other.data)
identifiers = identifiers.difference(identifiers2)
data = self._identifier2data(id2datapoint, identifier2id, identifiers)
return KeyValueDataset(data, self.task_type, self.fields)