-
Notifications
You must be signed in to change notification settings - Fork 82
/
preprocess.py
364 lines (292 loc) · 14.7 KB
/
preprocess.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
import os
import json
import logging
from transformers import BertTokenizer
from utils import cutSentences, commonUtils
import config
logger = logging.getLogger(__name__)
class InputExample:
def __init__(self, set_type, text, labels=None):
self.set_type = set_type
self.text = text
self.labels = labels
class BaseFeature:
def __init__(self, token_ids, attention_masks, token_type_ids):
# BERT 输入
self.token_ids = token_ids
self.attention_masks = attention_masks
self.token_type_ids = token_type_ids
class BertFeature(BaseFeature):
def __init__(self, token_ids, attention_masks, token_type_ids, labels=None):
super(BertFeature, self).__init__(
token_ids=token_ids,
attention_masks=attention_masks,
token_type_ids=token_type_ids)
# labels
self.labels = labels
class NerProcessor:
def __init__(self, cut_sent=True, cut_sent_len=256):
self.cut_sent = cut_sent
self.cut_sent_len = cut_sent_len
@staticmethod
def read_json(file_path):
with open(file_path, encoding='utf-8') as f:
raw_examples = json.load(f)
return raw_examples
def get_examples(self, raw_examples, set_type):
examples = []
# 这里是从json数据中的字典中获取
for i, item in enumerate(raw_examples):
# print(i,item)
text = item['text']
if self.cut_sent:
sentences = cutSentences.cut_sent_for_bert(text, self.cut_sent_len)
start_index = 0
for sent in sentences:
labels = cutSentences.refactor_labels(sent, item['labels'], start_index)
start_index += len(sent)
examples.append(InputExample(set_type=set_type,
text=sent,
labels=labels))
else:
labels = item['labels']
if len(labels) != 0:
labels = [(label[1],label[4],label[2]) for label in labels]
examples.append(InputExample(set_type=set_type,
text=text,
labels=labels))
return examples
def convert_bert_example(ex_idx, example: InputExample, tokenizer: BertTokenizer,
max_seq_len, ent2id, labels):
set_type = example.set_type
raw_text = example.text
entities = example.labels
# 文本元组
callback_info = (raw_text,)
# 标签字典
callback_labels = {x: [] for x in labels}
# _label:实体类别 实体名 实体起始位置
for _label in entities:
# print(_label)
callback_labels[_label[0]].append((_label[1], _label[2]))
callback_info += (callback_labels,)
# 序列标注任务 BERT 分词器可能会导致标注偏
tokens = commonUtils.fine_grade_tokenize(raw_text, tokenizer)
assert len(tokens) == len(raw_text)
label_ids = None
# information for dev callback
# ========================
label_ids = [0] * len(tokens)
# tag labels ent ex. (T1, DRUG_DOSAGE, 447, 450, 小蜜丸)
for ent in entities:
# ent: ('PER', '陈元', 0)
ent_type = ent[0] # 类别
ent_start = ent[-1] # 起始位置
ent_end = ent_start + len(ent[1]) - 1
if ent_start == ent_end:
label_ids[ent_start] = ent2id['S-' + ent_type]
else:
label_ids[ent_start] = ent2id['B-' + ent_type]
label_ids[ent_end] = ent2id['E-' + ent_type]
for i in range(ent_start + 1, ent_end):
label_ids[i] = ent2id['I-' + ent_type]
if len(label_ids) > max_seq_len - 2:
label_ids = label_ids[:max_seq_len - 2]
label_ids = [0] + label_ids + [0]
# pad
if len(label_ids) < max_seq_len:
pad_length = max_seq_len - len(label_ids)
label_ids = label_ids + [0] * pad_length # CLS SEP PAD label都为O
assert len(label_ids) == max_seq_len, f'{len(label_ids)}'
# ========================
encode_dict = tokenizer.encode_plus(text=tokens,
max_length=max_seq_len,
padding="max_length",
truncation='longest_first',
return_token_type_ids=True,
return_attention_mask=True)
tokens = ['[CLS]'] + tokens + ['[SEP]']
token_ids = encode_dict['input_ids']
attention_masks = encode_dict['attention_mask']
token_type_ids = encode_dict['token_type_ids']
if ex_idx < 3:
logger.info(f"*** {set_type}_example-{ex_idx} ***")
print(tokenizer.decode(token_ids[:len(raw_text)+2]))
logger.info(f'text: {str(" ".join(tokens))}')
logger.info(f"token_ids: {token_ids}")
logger.info(f"attention_masks: {attention_masks}")
logger.info(f"token_type_ids: {token_type_ids}")
logger.info(f"labels: {label_ids}")
logger.info('length: ' + str(len(token_ids)))
# for word, token, attn, label in zip(tokens, token_ids, attention_masks, label_ids):
# print(word + ' ' + str(token) + ' ' + str(attn) + ' ' + str(label))
feature = BertFeature(
# bert inputs
token_ids=token_ids,
attention_masks=attention_masks,
token_type_ids=token_type_ids,
labels=label_ids,
)
return feature, callback_info
def convert_examples_to_features(examples, max_seq_len, bert_dir, ent2id, labels):
tokenizer = BertTokenizer(os.path.join(bert_dir, 'vocab.txt'))
features = []
callback_info = []
logger.info(f'Convert {len(examples)} examples to features')
for i, example in enumerate(examples):
# 有可能text为空,过滤掉
if not example.text:
continue
feature, tmp_callback = convert_bert_example(
ex_idx=i,
example=example,
max_seq_len=max_seq_len,
ent2id=ent2id,
tokenizer=tokenizer,
labels = labels,
)
if feature is None:
continue
features.append(feature)
callback_info.append(tmp_callback)
logger.info(f'Build {len(features)} features')
out = (features,)
if not len(callback_info):
return out
out += (callback_info,)
return out
def get_data(processor, raw_data_path, json_file, mode, ent2id, labels, args):
raw_examples = processor.read_json(os.path.join(raw_data_path, json_file))
examples = processor.get_examples(raw_examples, mode)
data = convert_examples_to_features(examples, args.max_seq_len, args.bert_dir, ent2id, labels)
save_path = os.path.join(args.data_dir, 'final_data')
if not os.path.exists(save_path):
os.makedirs(save_path)
commonUtils.save_pkl(save_path, data, mode)
return data
def save_file(filename, data ,id2ent):
features, callback_info = data
file = open(filename,'w',encoding='utf-8')
for feature,tmp_callback in zip(features, callback_info):
text, gt_entities = tmp_callback
for word, label in zip(text, feature.labels[1:len(text)+1]):
file.write(word + ' ' + id2ent[label] + '\n')
file.write('\n')
file.close()
if __name__ == '__main__':
dataset = "cner"
args = config.Args().get_parser()
args.bert_dir = '../model_hub/chinese-bert-wwm-ext/'
commonUtils.set_logger(os.path.join(args.log_dir, 'preprocess.log'))
use_aug = False
if dataset == "cner":
args.data_dir = './data/cner'
args.max_seq_len = 150
labels_path = os.path.join(args.data_dir, 'mid_data', 'labels.json')
with open(labels_path, 'r') as fp:
labels = json.load(fp)
ent2id_path = os.path.join(args.data_dir, 'mid_data')
with open(os.path.join(ent2id_path, 'nor_ent2id.json'), encoding='utf-8') as f:
ent2id = json.load(f)
id2ent = {v: k for k, v in ent2id.items()}
mid_data_path = os.path.join(args.data_dir, 'mid_data')
processor = NerProcessor(cut_sent=True, cut_sent_len=args.max_seq_len)
if use_aug:
train_data = get_data(processor, mid_data_path, "train_aug.json", "train", ent2id, labels, args)
else:
train_data = get_data(processor, mid_data_path, "train.json", "train", ent2id, labels, args)
save_file(os.path.join(mid_data_path,"cner_{}_cut.txt".format(args.max_seq_len)), train_data, id2ent)
dev_data = get_data(processor, mid_data_path, "dev.json", "dev", ent2id, labels, args)
test_data = get_data(processor, mid_data_path, "test.json", "test", ent2id, labels, args)
elif dataset == "chip":
args.data_dir = './data/CHIP2020'
args.max_seq_len = 150
labels_path = os.path.join(args.data_dir, 'mid_data', 'labels.json')
with open(labels_path, 'r') as fp:
labels = json.load(fp)
ent2id_path = os.path.join(args.data_dir, 'mid_data')
with open(os.path.join(ent2id_path, 'nor_ent2id.json'), encoding='utf-8') as f:
ent2id = json.load(f)
id2ent = {v: k for k, v in ent2id.items()}
mid_data_path = os.path.join(args.data_dir, 'mid_data')
processor = NerProcessor(cut_sent=True, cut_sent_len=args.max_seq_len)
if use_aug:
train_data = get_data(processor, mid_data_path, "train_aug.json", "train", ent2id, labels, args)
else:
train_data = get_data(processor, mid_data_path, "train.json", "train", ent2id, labels, args)
save_file(os.path.join(mid_data_path,"chip_{}_cut.txt".format(args.max_seq_len)), train_data, id2ent)
dev_data = get_data(processor, mid_data_path, "dev.json", "dev", ent2id, labels, args)
# test_data = get_data(processor, mid_data_path, "test.json", "test", ent2id, labels, args)
elif dataset == "clue":
args.data_dir = './data/CLUE'
args.max_seq_len = 150
labels_path = os.path.join(args.data_dir, 'mid_data', 'labels.json')
with open(labels_path, 'r') as fp:
labels = json.load(fp)
ent2id_path = os.path.join(args.data_dir, 'mid_data')
with open(os.path.join(ent2id_path, 'nor_ent2id.json'), encoding='utf-8') as f:
ent2id = json.load(f)
id2ent = {v: k for k, v in ent2id.items()}
mid_data_path = os.path.join(args.data_dir, 'mid_data')
processor = NerProcessor(cut_sent=True, cut_sent_len=args.max_seq_len)
if use_aug:
train_data = get_data(processor, mid_data_path, "train_aug.json", "train", ent2id, labels, args)
else:
train_data = get_data(processor, mid_data_path, "train.json", "train", ent2id, labels, args)
save_file(os.path.join(mid_data_path,"clue_{}_cut.txt".format(args.max_seq_len)), train_data, id2ent)
dev_data = get_data(processor, mid_data_path, "dev.json", "dev", ent2id, labels, args)
# test_data = get_data(processor, mid_data_path, "test.json", "test", ent2id, labels, args)
elif dataset == "addr":
args.data_dir = './data/addr'
args.max_seq_len = 64
labels_path = os.path.join(args.data_dir, 'mid_data', 'labels.json')
with open(labels_path, 'r') as fp:
labels = json.load(fp)
ent2id_path = os.path.join(args.data_dir, 'mid_data')
with open(os.path.join(ent2id_path, 'nor_ent2id.json'), encoding='utf-8') as f:
ent2id = json.load(f)
id2ent = {v: k for k, v in ent2id.items()}
mid_data_path = os.path.join(args.data_dir, 'mid_data')
processor = NerProcessor(cut_sent=True, cut_sent_len=args.max_seq_len)
if use_aug:
train_data = get_data(processor, mid_data_path, "train_aug.json", "train", ent2id, labels, args)
else:
train_data = get_data(processor, mid_data_path, "train.json", "train", ent2id, labels, args)
save_file(os.path.join(mid_data_path,"clue_{}_cut.txt".format(args.max_seq_len)), train_data, id2ent)
dev_data = get_data(processor, mid_data_path, "dev.json", "dev", ent2id, labels, args)
# test_data = get_data(processor, mid_data_path, "test.json", "test", ent2id, labels, args)
elif dataset == "attr":
args.data_dir = './data/attr'
args.max_seq_len = 128
labels_path = os.path.join(args.data_dir, 'mid_data', 'labels.json')
with open(labels_path, 'r') as fp:
labels = json.load(fp)
ent2id_path = os.path.join(args.data_dir, 'mid_data')
with open(os.path.join(ent2id_path, 'nor_ent2id.json'), encoding='utf-8') as f:
ent2id = json.load(f)
id2ent = {v: k for k, v in ent2id.items()}
mid_data_path = os.path.join(args.data_dir, 'mid_data')
processor = NerProcessor(cut_sent=True, cut_sent_len=args.max_seq_len)
if use_aug:
train_data = get_data(processor, mid_data_path, "train_aug.json", "train", ent2id, labels, args)
else:
train_data = get_data(processor, mid_data_path, "train.json", "train", ent2id, labels, args)
save_file(os.path.join(mid_data_path,"clue_{}_cut.txt".format(args.max_seq_len)), train_data, id2ent)
dev_data = get_data(processor, mid_data_path, "dev.json", "dev", ent2id, labels, args)
# test_data = get_data(processor, mid_data_path, "test.json", "test", ent2id, labels, args)
elif dataset == "sighan2005":
args.data_dir = './data/sighan2005'
args.max_seq_len = 512
labels_path = os.path.join(args.data_dir, 'mid_data', 'labels.json')
with open(labels_path, 'r') as fp:
labels = json.load(fp)
ent2id_path = os.path.join(args.data_dir, 'mid_data')
with open(os.path.join(ent2id_path, 'nor_ent2id.json'), encoding='utf-8') as f:
ent2id = json.load(f)
id2ent = {v: k for k, v in ent2id.items()}
mid_data_path = os.path.join(args.data_dir, 'mid_data')
processor = NerProcessor(cut_sent=True, cut_sent_len=args.max_seq_len)
train_data = get_data(processor, mid_data_path, "train.json", "train", ent2id, labels, args)
save_file(os.path.join(mid_data_path, "clue_{}_cut.txt".format(args.max_seq_len)), train_data, id2ent)
dev_data = get_data(processor, mid_data_path, "test.json", "dev", ent2id, labels, args)
# test_data = get_data(processor, mid_data_path, "test.json", "test", ent2id, labels, args)