-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdata_helper.py
296 lines (236 loc) · 11.2 KB
/
data_helper.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
from torch.utils.data import Dataset
import json
import nltk
from itertools import chain
def tokenized_data(source_text, target_text, data_args, tokenizer):
padding = "max_length" if data_args.pad_to_max_length else False
model_inputs = tokenizer(source_text, max_length=data_args.max_source_length, padding=padding, truncation=True)
# Setup the tokenizer for targets
with tokenizer.as_target_tokenizer():
labels = tokenizer(target_text, max_length=data_args.max_target_length, padding=padding, truncation=True)
if data_args.ignore_pad_token_for_loss:
labels["input_ids"] = [
[(l if l != tokenizer.pad_token_id else -100) for l in label] for label in labels["input_ids"]
]
model_inputs["labels"] = labels["input_ids"]
return model_inputs
def process_normal2cloze(data, data_args, tokenizer):
all_questions = [item for item in list(chain(*[d['questions'] for d in data]))]
source_text = []
target_text = []
for q in all_questions:
# add prompt & eos token
# if data_args.answer_aware == 1:
# ans_txt = q['question']['question_choices'][q['answer']['ans_choice']]
# inp_txt = 'answer: {} normal2cloze: {}'.format(ans_txt, q['question']['normal_format'])
# else:
inp_txt = 'normal2cloze: ' + q['question']['normal_format']
out_txt = q['question']['cloze_format']
source_text.append(inp_txt)
target_text.append(out_txt)
model_inputs = tokenized_data(source_text, target_text, data_args, tokenizer)
return CustomDS(model_inputs.data)
def process_cloze2normal(data, data_args, tokenizer):
all_questions = [item for item in list(chain(*[d['questions'] for d in data]))]
source_text = []
target_text = []
for q in all_questions:
# add prompt
# if data_args.answer_aware == 1:
# ans_txt = q['question']['question_choices'][q['answer']['ans_choice']]
# inp_txt = 'answer: {} cloze2normal: {}'.format(ans_txt, q['question']['cloze_format'])
# else:
inp_txt = 'cloze2normal: ' + q['question']['cloze_format']
out_txt = q['question']['normal_format']
source_text.append(inp_txt)
target_text.append(out_txt)
model_inputs = tokenized_data(source_text, target_text, data_args, tokenizer)
return CustomDS(model_inputs.data)
def process_multi(data, data_args, tokenizer, split):
all_questions = [item for item in list(chain(*[d['questions'] for d in data]))]
source_text = []
target_text = []
for q in all_questions:
if split == 'train':
inp_txt = 'cloze2normal: ' + q['question']['cloze_format']
out_txt = q['question']['normal_format']
source_text.append(inp_txt)
target_text.append(out_txt)
inp_txt = 'normal2cloze: ' + q['question']['normal_format']
out_txt = q['question']['cloze_format']
source_text.append(inp_txt)
target_text.append(out_txt)
else:
if data_args.task == 'multi_cloze2normal':
# if data_args.answer_aware == 1:
# ans_txt = q['question']['question_choices'][q['answer']['ans_choice']]
# inp_txt = 'answer: {} cloze2normal: {}'.format(ans_txt, q['question']['cloze_format'])
# else:
inp_txt = 'cloze2normal: ' + q['question']['cloze_format']
out_txt = q['question']['normal_format']
source_text.append(inp_txt)
target_text.append(out_txt)
elif data_args.task == 'multi_normal2cloze':
# if data_args.answer_aware == 1:
# ans_txt = q['question']['question_choices'][q['answer']['ans_choice']]
# inp_txt = 'answer: {} normal2cloze: {}'.format(ans_txt, q['question']['normal_format'])
# else:
inp_txt = 'normal2cloze: ' + q['question']['normal_format']
out_txt = q['question']['cloze_format']
source_text.append(inp_txt)
target_text.append(out_txt)
else:
raise Exception("task is not found {multiway conversion}")
model_inputs = tokenized_data(source_text, target_text, data_args, tokenizer)
return CustomDS(model_inputs.data)
def process_qg_openstax(data, data_args, tokenizer):
all_questions = [item for item in list(chain(*[d['questions'] for d in data]))]
source_text = []
target_text = []
for q in all_questions:
# add prompt & eos token
ans_txt = q['question']['question_choices'][q['answer']['ans_choice']]
context = q['hl_sentences']
inp_txt = 'answer: {} context: {}'.format(ans_txt, context)
out_txt = q['question']['normal_format']
source_text.append(inp_txt)
target_text.append(out_txt)
model_inputs = tokenized_data(source_text, target_text, data_args, tokenizer)
return CustomDS(model_inputs.data)
def process_qg_agno_openstax(data, data_args, tokenizer):
all_questions = [item for item in list(chain(*[d['questions'] for d in data]))]
source_text = []
target_text = []
for q in all_questions:
# add prompt & eos token
# ans_txt = q['question']['question_choices'][q['answer']['ans_choice']]
# context = q['hl_sentences']
inp_txt = 'context {}: '.format(q['hl_context'].replace('<hl>', ''))
out_txt = q['question']['normal_format']
source_text.append(inp_txt)
target_text.append(out_txt)
model_inputs = tokenized_data(source_text, target_text, data_args, tokenizer)
return CustomDS(model_inputs.data)
def process_dutch_qg(data, data_args, tokenizer):
source_text = []
target_text = []
for qa_pair in data:
context = qa_pair['context_answer']
answer = qa_pair['answer']
question = qa_pair['question_text']
inp_txt = 'answer: {} context: {}'.format(answer, context)
source_text.append(inp_txt)
target_text.append(question)
model_inputs = tokenized_data(source_text, target_text, data_args, tokenizer)
return CustomDS(model_inputs.data)
def process_qg_squad(data, data_args, tokenizer):
source_text = []
target_text = []
for wiki_page in data:
for p in wiki_page['paragraphs']:
question_list = p['qas']
context = p['context']
# total_n_docs += 1
# avg_d_len += len(nltk.tokenize.word_tokenize(context))
sentences = nltk.tokenize.sent_tokenize(context)
for qobject in question_list:
if qobject['is_impossible']:
continue
question = qobject['question']
answer = qobject['answers'][0]['text']
q_beg_indx = qobject['answers'][0]['answer_start']
b = 0
e = 0
tmp_con = []
for s in sentences:
e += len(s)
if b <= q_beg_indx <= e:
# answer sentence
tmp_con.append("<hl> " + s + " <hl>")
else:
tmp_con.append(s)
# else:
# pass
# print('unmatch')
# non answer sentence
b += len(s)
inp_txt = 'answer: {} context: {}'.format(answer, " ".join(tmp_con))
out_txt = question
source_text.append(inp_txt)
target_text.append(out_txt)
model_inputs = tokenized_data(source_text, target_text, data_args, tokenizer)
return CustomDS(model_inputs.data)
def process_qg_agno_tqa(data, data_args, tokenizer):
source_text = []
target_text = []
for chapter in data:
for q in chapter['questions']:
inp_txt = "context: {}".format(q['ground_sentence'])
out_txt = q['question_text']
source_text.append(inp_txt)
target_text.append(out_txt)
model_inputs = tokenized_data(source_text, target_text, data_args, tokenizer)
return CustomDS(model_inputs.data)
def process_data(data, data_args, tknizer, split):
if data_args.task == 'cloze2normal':
return process_cloze2normal(data, data_args, tknizer)
elif data_args.task == 'normal2cloze':
return process_normal2cloze(data, data_args, tknizer)
elif data_args.task == 'multi_cloze2normal':
return process_multi(data, data_args, tknizer, split)
elif data_args.task == 'multi_normal2cloze':
return process_multi(data, data_args, tknizer, split)
elif data_args.task == 'qg':
return process_qg_openstax(data, data_args, tknizer)
elif data_args.task == 'qg_agno':
return process_qg_agno_openstax(data, data_args, tknizer)
elif data_args.task == 'qg_tqa':
return process_qg_agno_tqa(data, data_args, tknizer)
elif data_args.task == 'dutch_qg':
return process_dutch_qg(data, data_args, tknizer)
else:
raise Exception('task not found...')
def read_json_file(file_path):
with open(file_path) as outfile:
data = json.load(outfile)
return data
def subsample_for_debug(data_args, train_data, valid_data):
if data_args.is_debug_mode == 1:
train_data = train_data[:3]
valid_data = valid_data[:3]
return train_data, valid_data
def read_data(data_args, tokenizer):
# squad
if data_args.valid_file_path.endswith('dev-v2.0.json'):
train_data = read_json_file(data_args.train_file_path)
valid_data = read_json_file(data_args.valid_file_path)['data']
train_data, valid_data = subsample_for_debug(data_args, train_data, valid_data)
train_ds = process_data(train_data, data_args, tokenizer, 'train')
valid_ds = process_qg_squad(valid_data, data_args, tokenizer)
# dutch version of qg
elif data_args.valid_file_path.endswith('qg_dutch.json'):
train_data = read_json_file(data_args.train_file_path)['train']
valid_data = read_json_file(data_args.valid_file_path)['test']
train_data, valid_data = subsample_for_debug(data_args, train_data, valid_data)
train_ds = process_dutch_qg(train_data, data_args, tokenizer)
valid_ds = process_dutch_qg(valid_data, data_args, tokenizer)
elif data_args.valid_file_path.find('mTQA') != -1:
train_data = read_json_file(data_args.train_file_path)
valid_data = read_json_file(data_args.valid_file_path)
train_data, valid_data = subsample_for_debug(data_args, train_data, valid_data)
train_ds = process_data(train_data, data_args, tokenizer, 'train')
valid_ds = process_data(valid_data, data_args, tokenizer, 'valid')
else:
train_data = read_json_file(data_args.train_file_path)
valid_data = read_json_file(data_args.valid_file_path)
train_data, valid_data = subsample_for_debug(data_args, train_data, valid_data)
train_ds = process_data(train_data, data_args, tokenizer, 'train')
valid_ds = process_data(valid_data, data_args, tokenizer, 'valid')
return train_ds, valid_ds
class CustomDS(Dataset):
def __init__(self, data):
self.data = data
def __len__(self):
return len(self.data['input_ids'])
def __getitem__(self, idx):
return {k: v[idx] for k, v in self.data.items()}