-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_additional_data.py
326 lines (281 loc) · 14.7 KB
/
process_additional_data.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
''' use this scripts to process the five additional datasets for mt-dnn, that is MELD,D-MELD and three SL task '''
''' when running these five tasks, we need to add argument '--test_with_label', that directly generate test score rather than pred label '''
# due to the setting of our experiments, we only use bert-base-cased
import sys
import os
import json
import argparse
import tqdm
from transformers import AutoTokenizer, BertTokenizer
def parse_args():
parser = argparse.ArgumentParser(
description='Preprocessing the additional five datasets.')
parser.add_argument('--model', type=str, default='bert-base-cased',
help='can be any hugging-face bert model, but bert-base-cased here by default.')
parser.add_argument('--do_lower_case', type=bool, default=False)
parser.add_argument('--do_padding', action='store_true')
parser.add_argument('--root_dir', type=str, default='data')
# parser.add_argument('--task_def', type=str, default="experiments/glue/glue_task_def.yml")
parser.add_argument("--data_name", type=str, default="SC")
parser.add_argument("--bpe_pad", type=bool, default=True,
help="for SL task, use this arg to keep additional bpe token")
# all data name:
# meld, d-meld, pos, ner, sc
args = parser.parse_args()
return args
def read_meld(data_dir, tokenizer: BertTokenizer):
# return a tokenized dict list
train_file = open(os.path.join(data_dir, "train_format.json"), "r", encoding="utf-8")
dev_file = open(os.path.join(data_dir, "dev_format.json"), "r", encoding="utf-8")
test_file = open(os.path.join(data_dir, "test_format.json"), "r", encoding="utf-8")
train_data = json.load(train_file)
dev_data = json.load(dev_file)
test_data = json.load(test_file)
train_feature = []
for i, item in enumerate(train_data):
token_list = tokenizer.tokenize(item['seq1'])
token_id_list = tokenizer.convert_tokens_to_ids(token_list)
token_id_list = tokenizer.build_inputs_with_special_tokens(token_ids_0=token_id_list)
token_type_list = tokenizer.create_token_type_ids_from_sequences(token_id_list)
attention_list = [1] * len(token_type_list) # no pad
new_item = {"uid": i, "label": item["label"], "token_id": token_id_list, "type_id": token_type_list,
"attention_mask": attention_list}
train_feature.append(new_item)
dev_feature = []
for i, item in enumerate(dev_data):
token_list = tokenizer.tokenize(item['seq1'])
token_id_list = tokenizer.convert_tokens_to_ids(token_list)
token_id_list = tokenizer.build_inputs_with_special_tokens(token_ids_0=token_id_list)
token_type_list = tokenizer.create_token_type_ids_from_sequences(token_id_list)
attention_list = [1] * len(token_type_list) # no pad
new_item = {"uid": i, "label": item["label"], "token_id": token_id_list, "type_id": token_type_list,
"attention_mask": attention_list}
dev_feature.append(new_item)
test_feature = []
for i, item in enumerate(test_data):
token_list = tokenizer.tokenize(item['seq1'])
token_id_list = tokenizer.convert_tokens_to_ids(token_list)
token_id_list = tokenizer.build_inputs_with_special_tokens(token_ids_0=token_id_list)
token_type_list = tokenizer.create_token_type_ids_from_sequences(token_id_list)
attention_list = [1] * len(token_type_list) # no pad
new_item = {"uid": i, "label": item["label"], "token_id": token_id_list, "type_id": token_type_list,
"attention_mask": attention_list}
test_feature.append(new_item)
train_file.close()
dev_file.close()
test_file.close()
return train_feature, dev_feature, test_feature
def read_sl(data_dir, tokenizer: BertTokenizer):
# return a tokenized dict list
train_file = open(os.path.join(data_dir, "train_format.json"), "r", encoding="utf-8")
dev_file = open(os.path.join(data_dir, "dev_format.json"), "r", encoding="utf-8")
test_file = open(os.path.join(data_dir, "test_format.json"), "r", encoding="utf-8")
data_info_file = open(os.path.join(data_dir, "data_info.json"), "r", encoding="utf-8")
train_data = json.load(train_file)
dev_data = json.load(dev_file)
test_data = json.load(test_file)
data_info = json.load(data_info_file)
label_num = len(data_info["labels"].items())
train_feature = []
for i, item in enumerate(train_data):
seq = item['seq1']
lbs = item['label']
assert len(seq) == len(lbs)
token_list = []
label_list = []
for idx, tk in enumerate(seq):
lb = lbs[idx]
tked_list = tokenizer.tokenize(tk)
tked = tked_list[0] ## assert taking the first tk to represent the ori token
token_list.append(tked)
label_list.append(lb)
token_id_list = tokenizer.convert_tokens_to_ids(token_list)
token_id_list = tokenizer.build_inputs_with_special_tokens(token_ids_0=token_id_list)
label_list = [label_num] + label_list + [label_num + 1]
assert len(label_list) == len(token_id_list)
token_type_list = tokenizer.create_token_type_ids_from_sequences(token_id_list)
# attention_list = [1] * len(token_type_list) # no pad
new_item = {"uid": i, "label": label_list, "token_id": token_id_list, "type_id": token_type_list}
train_feature.append(new_item)
dev_feature = []
for i, item in enumerate(dev_data):
seq = item['seq1']
lbs = item['label']
assert len(seq) == len(lbs)
token_list = []
label_list = []
for idx, tk in enumerate(seq):
lb = lbs[idx]
tked_list = tokenizer.tokenize(tk)
tked = tked_list[0] ## assert taking the first tk to represent the ori token
token_list.append(tked)
label_list.append(lb)
token_id_list = tokenizer.convert_tokens_to_ids(token_list)
token_id_list = tokenizer.build_inputs_with_special_tokens(token_ids_0=token_id_list)
label_list = [label_num] + label_list + [label_num + 1]
assert len(label_list) == len(token_id_list)
token_type_list = tokenizer.create_token_type_ids_from_sequences(token_id_list)
# attention_list = [1] * len(token_type_list) # no pad
new_item = {"uid": i, "label": label_list, "token_id": token_id_list, "type_id": token_type_list}
dev_feature.append(new_item)
test_feature = []
for i, item in enumerate(test_data):
seq = item['seq1']
lbs = item['label']
assert len(seq) == len(lbs)
token_list = []
label_list = []
for idx, tk in enumerate(seq):
lb = lbs[idx]
tked_list = tokenizer.tokenize(tk)
tked = tked_list[0] ## assert taking the first tk to represent the ori token
token_list.append(tked)
label_list.append(lb)
token_id_list = tokenizer.convert_tokens_to_ids(token_list)
token_id_list = tokenizer.build_inputs_with_special_tokens(token_ids_0=token_id_list)
label_list = [label_num] + label_list + [label_num + 1]
assert len(label_list) == len(token_id_list)
token_type_list = tokenizer.create_token_type_ids_from_sequences(token_id_list)
# attention_list = [1] * len(token_type_list) # no pad
new_item = {"uid": i, "label": label_list, "token_id": token_id_list, "type_id": token_type_list}
test_feature.append(new_item)
train_file.close()
dev_file.close()
test_file.close()
data_info_file.close()
return train_feature, dev_feature, test_feature
def read_sl_new(data_dir, tokenizer: BertTokenizer):
''' the only difference of this procedure is that we don't ignore the additional token generated by BPE '''
# return a tokenized dict list
train_file = open(os.path.join(data_dir, "train_format.json"), "r", encoding="utf-8")
dev_file = open(os.path.join(data_dir, "dev_format.json"), "r", encoding="utf-8")
test_file = open(os.path.join(data_dir, "test_format.json"), "r", encoding="utf-8")
data_info_file = open(os.path.join(data_dir, "data_info.json"), "r", encoding="utf-8")
train_data = json.load(train_file)
dev_data = json.load(dev_file)
test_data = json.load(test_file)
data_info = json.load(data_info_file)
label_num = len(data_info["labels"].items())
train_feature = []
for i, item in enumerate(train_data):
seq = item['seq1']
lbs = item['label']
assert len(seq) == len(lbs)
token_list = []
label_list = []
for idx, tk in enumerate(seq):
tked_list = tokenizer.tokenize(tk)
tked = tked_list # take all the BPE token
token_list += tked
lb = [lbs[idx]] + [-1] * (len(tked) - 1) # append the additional tokens' label with -1
label_list += lb
assert len(token_list) == len(label_list)
token_id_list = tokenizer.convert_tokens_to_ids(token_list)
token_id_list = tokenizer.build_inputs_with_special_tokens(token_ids_0=token_id_list)
label_list = [label_num] + label_list + [label_num + 1]
assert len(label_list) == len(token_id_list)
token_type_list = tokenizer.create_token_type_ids_from_sequences(token_id_list)
# attention_list = [1] * len(token_type_list) # no pad
new_item = {"uid": i, "label": label_list, "token_id": token_id_list, "type_id": token_type_list}
train_feature.append(new_item)
dev_feature = []
for i, item in enumerate(dev_data):
seq = item['seq1']
lbs = item['label']
assert len(seq) == len(lbs)
token_list = []
label_list = []
for idx, tk in enumerate(seq):
tked_list = tokenizer.tokenize(tk)
tked = tked_list # take all the BPE token
token_list += tked
lb = [lbs[idx]] + [-1] * (len(tked) - 1) # append the additional tokens' label with -1
label_list += lb
assert len(token_list) == len(label_list)
token_id_list = tokenizer.convert_tokens_to_ids(token_list)
token_id_list = tokenizer.build_inputs_with_special_tokens(token_ids_0=token_id_list)
label_list = [label_num] + label_list + [label_num + 1]
assert len(label_list) == len(token_id_list)
token_type_list = tokenizer.create_token_type_ids_from_sequences(token_id_list)
# attention_list = [1] * len(token_type_list) # no pad
new_item = {"uid": i, "label": label_list, "token_id": token_id_list, "type_id": token_type_list}
dev_feature.append(new_item)
test_feature = []
for i, item in enumerate(test_data):
seq = item['seq1']
lbs = item['label']
assert len(seq) == len(lbs)
token_list = []
label_list = []
for idx, tk in enumerate(seq):
tked_list = tokenizer.tokenize(tk)
tked = tked_list # take all the BPE token
token_list += tked
lb = [lbs[idx]] + [-1] * (len(tked) - 1) # append the additional tokens' label with -1
label_list += lb
assert len(token_list) == len(label_list)
token_id_list = tokenizer.convert_tokens_to_ids(token_list)
token_id_list = tokenizer.build_inputs_with_special_tokens(token_ids_0=token_id_list)
label_list = [label_num] + label_list + [label_num + 1]
assert len(label_list) == len(token_id_list)
token_type_list = tokenizer.create_token_type_ids_from_sequences(token_id_list)
# attention_list = [1] * len(token_type_list) # no pad
new_item = {"uid": i, "label": label_list, "token_id": token_id_list, "type_id": token_type_list}
test_feature.append(new_item)
train_file.close()
dev_file.close()
test_file.close()
data_info_file.close()
return train_feature, dev_feature, test_feature
def main(args):
# hyper param
root = os.path.join(args.root_dir, args.data_name)
assert os.path.exists(root), "there is no such dir of {}".format(root)
tokenizer = AutoTokenizer.from_pretrained(args.model)
canonical_data_root = os.path.join(args.root_dir, "canonical_data")
mt_dnn_root = os.path.join(canonical_data_root, args.model)
if not os.path.isdir(mt_dnn_root):
os.makedirs(mt_dnn_root)
data_dir = os.path.join("data", args.data_name)
# task_defs = TaskDefs(args.task_def)
#
# for task in task_defs.get_task_names():
# task_def = task_defs.get_task_def(task)
# logger.info("Task %s" % task)
# for split_name in task_def.split_names:
# file_path = os.path.join(root, "%s_%s.tsv" % (task, split_name))
# if not os.path.exists(file_path):
# logger.warning("File %s doesnot exit")
# sys.exit(1)
# rows = load_data(file_path, task_def)
# dump_path = os.path.join(mt_dnn_root, "%s_%s.json" % (task, split_name))
# logger.info(dump_path)
# build_data(
# rows,
# dump_path,
# tokenizer,
# task_def.data_type,
# lab_dict=task_def.label_vocab)
train_feature, dev_feature, test_feature = None, None, None
if args.data_name in ["MELD", "D-MELD"]:
train_feature, dev_feature, test_feature = read_meld(data_dir, tokenizer)
else:
train_feature, dev_feature, test_feature = read_sl(data_dir, tokenizer) if not args.bpe_pad else read_sl_new(
data_dir, tokenizer)
task_name = args.data_name.replace("-", "").lower()
with open(os.path.join(mt_dnn_root, "{}_train.json".format(task_name)), "w", encoding="utf-8") as writer:
for i, feature in enumerate(train_feature):
writer.write('{}\n'.format(json.dumps(feature)))
print("{} train data dumped successfully".format(i))
with open(os.path.join(mt_dnn_root, "{}_dev.json".format(task_name)), "w", encoding="utf-8") as writer:
for i, feature in enumerate(dev_feature):
writer.write('{}\n'.format(json.dumps(feature)))
print("{} dev data dumped successfully".format(i))
with open(os.path.join(mt_dnn_root, "{}_test.json".format(task_name)), "w", encoding="utf-8") as writer:
for i, feature in enumerate(test_feature):
writer.write('{}\n'.format(json.dumps(feature)))
print("{} test data dumped successfully".format(i))
print("{} data process successfully!".format(args.data_name))
if __name__ == '__main__':
args = parse_args()
main(args)