-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataset_constructor.py
340 lines (281 loc) · 9.33 KB
/
dataset_constructor.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
import csv
from dataclasses import dataclass
from enum import Enum
from typing import List, Optional, Tuple
from transformers import PreTrainedTokenizer
from torch.utils.data import Dataset
import torch
from utils import file_handler as f_handler
"""
Parent Classes
- GeneralInputExample
- GeneralProcessor
- GeneralDataset(Dataset)
"""
@dataclass(frozen=True)
class GeneralInputExample:
text: str
text_pair: Optional[str]
label: Optional[str]
class GeneralProcessor:
"""Parent dataset processor"""
def get_train_instances(self):
"""Process Train Set"""
return NotImplementedError()
def get_valid_instances(self):
"""Process Valid Set"""
return NotImplementedError()
def get_test_instances(self):
"""Process Test Set"""
return NotImplementedError()
def get_labels(self):
"""See base class."""
return ["True", "False"]
def _read_csv(self, input_file):
df = f_handler.load_ndjson_and_return_pandas(input_file)
return df.to_dict('records')
def _create_instances(self, lines: List[dict]):
"""Creates instances for the training and dev sets."""
instances = []
for line_dict in lines:
instances.append(
GeneralInputExample(
text = line_dict['text'],
text_pair = line_dict['text_pair'],
label = line_dict['difficult_text']
)
)
return instances
class DatasetForSeq2Seq(Dataset):
def __init__(self, tokenizer, split, max_seq_length):
self.split = split
self.max_seq_length = max_seq_length
self.tokenizer = tokenizer
self.proc = GeneralProcessor()
self._build()
def __getitem__(self, item):
return {key: self.inputs[key][item] for key in self.inputs.keys()}
def __len__(self):
return len(self.texts)
def _build(self):
if self.split == 'train':
self.instances = self.proc.get_train_instances()
elif self.split == "valid":
self.instances = self.proc.get_valid_instances()
elif self.split == "test":
self.instances = self.proc.get_test_instances()
else:
raise Exception("wrong split")
self.texts, self.labels = self._iterate_and_prepare_instances()
self._make_inputs()
def _truncate_text(self, text: str) -> str:
max_seq_length = int(self.max_seq_length/2 - 10)
tokenized = self.tokenizer(
text,
max_length = max_seq_length,
truncation=True,
pad_to_max_length = False,
return_tensors = "pt"
)
text = self.tokenizer.decode(
*tokenized['input_ids'].tolist(),
skip_special_tokens = True,
)
return text
def _map_label(self, label) -> str:
"""label_mapper = {
'text': 'Text 1',
'text_pair': 'Text 2'
}# train"""
label_mapper = {
'text': 'Text 2',
'text_pair': 'Text 1'
}# test"""
return label_mapper[label]
def _iterate_and_prepare_instances(self) -> Tuple[List[str], List[str]]:
texts = []
labels = []
for instance in self.instances:
text = self._truncate_text(instance.text)
text_pair = self._truncate_text(instance.text_pair)
label = self._map_label(instance.label)
"""texts.append(f"Which Text is more difficult? Text 1: {text} Text 2: {text_pair}") # train"""
texts.append(f"Text 1: {text} Text 2: {text_pair} Easier:") # test"""
labels.append(label)
return texts, labels
def _make_inputs(self):
self.inputs = self.tokenizer(
self.texts,
max_length=self.max_seq_length,
padding='max_length',
truncation=True,
return_tensors='pt'
)
self.labels = self.tokenizer(
self.labels,
max_length = 5,
padding='max_length',
return_tensors = "pt"
)
self.inputs.update({
'labels':self.labels['input_ids']
})
"""
OneStopEnglish
- OneStopEnglishProcessor(GeneralProcessor)
- OneStopEnglishDataset(DatasetForSeq2Seq)
"""
class OneStopEnglishProcessor(GeneralProcessor):
"""Processor for the OneStopEnglish."""
def get_train_instances(self):
"""Process Train Set"""
return self._create_instances(
self._read_csv(
"datasets/final_OSEN_train.json"
)
)
def get_valid_instances(self):
"""Process Valid Set"""
return self._create_instances(
self._read_csv(
"datasets/final_OSEN_dev.json"
)
)
def get_test_instances(self):
"""Process Test Set"""
return self._create_instances(
self._read_csv(
"datasets/final_OSEN_test.json"
)
)
class OneStopEnglishDataset(DatasetForSeq2Seq):
"""Dataset for the OneStopEnglish."""
def __init__(self, tokenizer, split, max_seq_length):
self.split = split
self.max_seq_length = max_seq_length
self.tokenizer = tokenizer
self.proc = OneStopEnglishProcessor()
self._build()
"""
Newsela
- NewselaProcessor(GeneralProcessor)
- NewselaDataset(DatasetForSeq2Seq)
"""
class NewselaProcessor(GeneralProcessor):
"""Processor for the Newsela."""
def get_train_instances(self):
"""Process Train Set"""
return self._create_instances(
self._read_csv(
"datasets/final_NEWS_train.json"
)
)
def get_valid_instances(self):
"""Process Valid Set"""
return self._create_instances(
self._read_csv(
"datasets/final_NEWS_dev.json"
)
)
def get_test_instances(self):
"""Process Test Set"""
return self._create_instances(
self._read_csv(
"datasets/final_NEWS_test.json"
)
)
class NewselaDataset(DatasetForSeq2Seq):
"""Dataset for the Newsela."""
def __init__(self, tokenizer, split, max_seq_length):
self.split = split
self.max_seq_length = max_seq_length
self.tokenizer = tokenizer
self.proc = NewselaProcessor()
self._build()
"""
CambridgeEnglishReadability
- CambridgeEnglishReadabilityProcessor(GeneralProcessor)
- CambridgeEnglishReadabilityDataset(DatasetForSeq2Seq)
"""
class CambridgeEnglishReadabilityProcessor(GeneralProcessor):
"""Processor for the CambridgeEnglishReadability."""
def get_train_instances(self):
"""Process Train Set"""
return NotImplementedError()
def get_valid_instances(self):
"""Process Valid Set"""
return NotImplementedError()
def get_test_instances(self):
"""Process Test Set"""
return self._create_instances(
self._read_csv(
"datasets/final_CAMB.json"
)
)
class CambridgeEnglishReadabilityDataset(DatasetForSeq2Seq):
"""Dataset for the CambridgeEnglishReadability."""
def __init__(self, tokenizer, split, max_seq_length):
self.split = split
self.max_seq_length = max_seq_length
self.tokenizer = tokenizer
self.proc = CambridgeEnglishReadabilityProcessor()
self._build()
"""
CommonCoreStandards
- CommonCoreStandardsProcessor(GeneralProcessor)
- CommonCoreStandardsDataset(DatasetForSeq2Seq)
"""
class CommonCoreStandardsProcessor(GeneralProcessor):
"""Processor for the CommonCoreStandards."""
def get_train_instances(self):
"""Process Train Set"""
return NotImplementedError()
def get_valid_instances(self):
"""Process Valid Set"""
return NotImplementedError()
def get_test_instances(self):
"""Process Test Set"""
return self._create_instances(
self._read_csv(
"datasets/final_CCSB_0_2.json"
)
)
class CommonCoreStandardsDataset(DatasetForSeq2Seq):
"""Dataset for the CommonCoreStandards."""
def __init__(self, tokenizer, split, max_seq_length):
self.split = split
self.max_seq_length = max_seq_length
self.tokenizer = tokenizer
self.proc = CommonCoreStandardsProcessor()
self._build()
if __name__ == "__main__":
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path="t5-base")
test = OneStopEnglishDataset(
tokenizer = tokenizer,
split = 'train',
max_seq_length = 512,
)
print(len(test))
print(test[0])
test = NewselaDataset(
tokenizer = tokenizer,
split = 'train',
max_seq_length = 512,
)
print(len(test))
print(test[0])
test = CambridgeEnglishReadabilityDataset(
tokenizer = tokenizer,
split = 'train',
max_seq_length = 512,
)
print(len(test))
print(test[0])
test = CommonCoreStandardsDataset(
tokenizer = tokenizer,
split = 'train',
max_seq_length = 512,
)
print(len(test))
print(test[0])