-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpredict_multitask_lm_model.py
619 lines (547 loc) · 25.7 KB
/
predict_multitask_lm_model.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author : WenRichard
# @Email : [email protected]
# @File : predict_bert_crf.py
# @Software: PyCharm
"""
由于 BIO 词表得到了缩减,CRF 运行时间以及消耗内存迅速减少,cascade_bert_ner训练速度得到提高
"""
import pandas as pd
import tensorflow as tf
import numpy as np
import codecs
import pickle
import os
from datetime import time, timedelta, datetime
import copy
from run_multitask_bert_crf import create_model, InputFeatures, InputExample
from bert import tokenization
from bert import modeling_bert
from public_tools.ner_utils import get_entity, get_entity_without_labelid
from public_tools.tag_evaluating import Metrics
from public_tools.entity_evaluating import entity_metrics, entity_metrics_without_lableid
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
flags = tf.flags
FLAGS = flags.FLAGS
flags.DEFINE_bool(
"do_predict_outline", False,
"Whether to do predict outline."
)
flags.DEFINE_bool(
"do_predict_online", True,
"Whether to do predict online."
)
gpu_config = tf.ConfigProto()
gpu_config.gpu_options.allow_growth = True
class Args():
def __init__(self):
self.kflod = 2
self.is_training =False
self.use_one_hot_embeddings = False
self.batch_size = 1
self.dev_file = './data/clue_ner/dev.txt'
self.test_file = './data/clue_ner/test.txt'
self.bert_config_file = 'D:/Expriment/pretrain_model_tf/bert/bert_config.json'
self.output_dir = 'D:/Expriment/model_output/ner_tool/bert_crf/multi_task/clue_ner/runs/checkpoints'
self.vocab_file = 'D:/Expriment/pretrain_model_tf/bert/vocab.txt'
args = Args()
# 加载label->id的词典
with open(os.path.join(FLAGS.output_dir, 'bmeo_label2id.pkl'), 'rb') as f:
bmeo_label2id = pickle.load(f)
bmeo_id2label = {value: key for key, value in bmeo_label2id.items()}
with open(os.path.join(FLAGS.output_dir, 'attr_label2id.pkl'), 'rb') as f:
attr_label2id = pickle.load(f)
attr_id2label = {value: key for key, value in attr_label2id.items()}
num_bmeo_labels = len(bmeo_label2id)
num_attr_labels = len(attr_label2id)
global graph
graph = tf.get_default_graph()
sess = tf.Session(config=gpu_config)
def parse_file(input_file):
with open(input_file, 'r', encoding='utf-8') as f:
lines = []
words = []
bmeo_labels = []
attr_labels = []
for line in f:
contends = line.strip()
tokens = contends.split('\t')
if len(tokens) == 2:
word = line.strip().split('\t')[0]
label = line.strip().split('\t')[-1]
else:
if len(contends) == 0:
# L: 'B-ORG M-ORG M-ORG M-ORG'
# W: '中 共 中 央'
bmeo_s = ' '.join([label for label in bmeo_labels if len(label) > 0])
attr_s = ' '.join([label for label in attr_labels if len(label) > 0])
w = ' '.join([word for word in words if len(word) > 0])
lines.append([w, bmeo_s, attr_s])
words = []
bmeo_labels = []
attr_labels = []
continue
words.append(word)
bmeo = label.split('-')[0]
# 将s的attr置为'O'
if len(label.split('-')) > 1:
attr = label.split('-')[1]
else:
attr = 'O'
bmeo_labels.append(bmeo)
attr_labels.append(attr)
return lines
def trans_label(bmeo_labels_id, attr_labels_id):
"""
答案拼接2种方式:
例子:
bmeo:[B, M, E]
attr: [LOC, LOC, ORG]
1. 实体词 bmeo 与 attr 的属性全都对应起来
result: [B_LOC, M_LOC, E_ORG]
2. 实体词 bmeo 的e所对应的attr作为该实体的attr
result: [B_ORG, M_ORG, E_ORG]
目前采用第一种
"""
std_labels = []
for index, bmeo_line in enumerate(bmeo_labels_id):
bmeo_attr_label = []
attr_line = attr_labels_id[index]
for item in list(zip(bmeo_line, attr_line)):
bmeo_id = item[0]
attr_id = item[1]
if bmeo_id2label[bmeo_id] == "O":
bmeo_attr = "O"
else:
bmeo_attr = bmeo_id2label[bmeo_id] + "_" + attr_id2label[attr_id]
bmeo_attr_label.append(bmeo_attr)
std_labels.append(bmeo_attr_label)
# print("std labels")
# print(std_labels)
return std_labels
def dev_offline(file):
"""
do online prediction. each time make prediction for one instance.
you can change to a batch if you want.
:param line: a list. element is: [dummy_label,text_a,text_b]
:return:
"""
def convert(line, bmeo_label, attr_label):
feature = convert_single_example_dev(2, line, bmeo_label, attr_label, bmeo_label2id, attr_label2id, FLAGS.max_seq_length, tokenizer)
input_ids = np.reshape([feature.input_ids], (1, FLAGS.max_seq_length))
input_mask = np.reshape([feature.input_mask], (1, FLAGS.max_seq_length))
segment_ids = np.reshape([feature.segment_ids], (1, FLAGS.max_seq_length))
bmeo_label_ids = np.reshape([feature.bmeo_label_ids], (1, FLAGS.max_seq_length))
attr_label_ids = np.reshape([feature.attr_label_ids], (1, FLAGS.max_seq_length))
return input_ids, input_mask, segment_ids, bmeo_label_ids, attr_label_ids
global graph
with graph.as_default():
# sess.run(tf.global_variables_initializer())
input_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="input_ids")
input_mask_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="input_mask")
bmeo_label_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="bmeo_label_ids")
attr_label_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="attr_label_ids")
segment_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="segment_ids")
bert_config = modeling_bert.BertConfig.from_json_file(args.bert_config_file)
(total_loss, bmeo_pred_ids, attr_pred_ids) = create_model(
bert_config, args.is_training, input_ids_p, input_mask_p, segment_ids_p, bmeo_label2id, attr_label2id,
bmeo_label_ids_p, attr_label_ids_p, num_bmeo_labels, num_attr_labels, args.use_one_hot_embeddings)
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint(args.output_dir))
tokenizer = tokenization.FullTokenizer(
vocab_file=args.vocab_file, do_lower_case=FLAGS.do_lower_case)
# 获取id2char字典
id2char = tokenizer.inv_vocab
dev_texts, dev_bmeo_labels, dev_attr_labels = zip(*parse_file(file))
start = datetime.now()
bmeo_pred_labels_all = []
bmeo_true_labels_all = []
attr_pred_labels_all = []
attr_true_labels_all = []
x_all = []
for index, text in enumerate(dev_texts):
sentence = str(text)
input_ids, input_mask, segment_ids, bmeo_label_ids, attr_label_ids = convert(sentence, dev_bmeo_labels[index], dev_attr_labels[index])
feed_dict = {input_ids_p: input_ids,
input_mask_p: input_mask,
segment_ids_p: segment_ids,
bmeo_label_ids_p: bmeo_label_ids,
attr_label_ids_p: attr_label_ids}
# run session get current feed_dict result
bmeo_y_pred, attr_y_pred = sess.run([bmeo_pred_ids, attr_pred_ids], feed_dict)
# print(bmeo_y_pred)
# print(list(bmeo_y_pred[0]))
# print(list(attr_y_pred[0]))
# print(len(list(y_pred[0][0])))
bmeo_sent_tag = []
attr_sent_tag = []
bmeo_y_pred_clean = []
attr_y_pred_clean = []
input_ids_clean = []
bmeo_y_true_clean = []
attr_y_true_clean = []
# 去除 [CLS] 和 [SEP]获取正确的tag范围
for index_b, id in enumerate(list(np.reshape(input_ids, -1))):
char = id2char[id]
bmeo_tag = bmeo_id2label[list(bmeo_y_pred[0])[index_b]]
attr_tag = attr_id2label[list(attr_y_pred[0])[index_b]]
if char == "[CLS]":
continue
if char == "[SEP]":
break
input_ids_clean.append(id)
bmeo_sent_tag.append(bmeo_tag)
attr_sent_tag.append(attr_tag)
bmeo_y_pred_clean.append(list(bmeo_y_pred[0])[index_b])
attr_y_pred_clean.append(list(attr_y_pred[0])[index_b])
bmeo_y_true_clean.append(bmeo_label_ids[0][index_b])
attr_y_true_clean.append(attr_label_ids[0][index_b])
bmeo_pred_labels_all.append(bmeo_y_pred_clean)
bmeo_true_labels_all.append(bmeo_y_true_clean)
attr_pred_labels_all.append(attr_y_pred_clean)
attr_true_labels_all.append(attr_y_true_clean)
x_all.append(input_ids_clean)
true_labels_all = trans_label(bmeo_true_labels_all, attr_true_labels_all)
pred_labels_all = trans_label(bmeo_pred_labels_all, attr_pred_labels_all)
print('预测标签与真实标签评价结果......')
print(true_labels_all)
print(len(true_labels_all))
print(pred_labels_all)
print(len(pred_labels_all))
bmeo_attr_metrics = Metrics(true_labels_all, pred_labels_all, bmeo_id2label, remove_O=True, use_id2tag=False)
bmeo_attr_metrics.report_scores()
# attr_metrics = Metrics(attr_true_labels_all, attr_pred_labels_all, attr_id2label, remove_O=True)
# attr_metrics.report_scores()
# metrics.report_confusion_matrix()
print('预测实体与真实实体评价结果......')
bmeo_attr_precision, bmeo_attr_recall, bmeo_attr_f1 = entity_metrics_without_lableid(x_all, true_labels_all, pred_labels_all,
id2char)
print("BMEO_ATTR Dev P/R/F1: {} / {} / {}".format(round(bmeo_attr_precision, 2), round(bmeo_attr_recall, 2), round(bmeo_attr_f1, 2)))
print('Time used: {} sec'.format((datetime.now() - start).seconds))
def predict_online():
"""
do online prediction. each time make prediction for one instance.
you can change to a batch if you want.
"""
def convert(line):
feature = convert_single_example(line, bmeo_label2id, attr_label2id, FLAGS.max_seq_length, tokenizer)
input_ids = np.reshape([feature.input_ids], (args.batch_size, FLAGS.max_seq_length))
input_mask = np.reshape([feature.input_mask], (args.batch_size, FLAGS.max_seq_length))
segment_ids = np.reshape([feature.segment_ids], (args.batch_size, FLAGS.max_seq_length))
bmeo_label_ids =np.reshape([feature.bmeo_label_ids], (args.batch_size, FLAGS.max_seq_length))
attr_label_ids = np.reshape([feature.attr_label_ids], (args.batch_size, FLAGS.max_seq_length))
return input_ids, input_mask, segment_ids, bmeo_label_ids, attr_label_ids
global graph
with graph.as_default():
print("going to restore checkpoint")
# sess.run(tf.global_variables_initializer())
input_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="input_ids")
input_mask_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="input_mask")
bmeo_label_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="bmeo_label_ids")
attr_label_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="attr_label_ids")
segment_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="segment_ids")
bert_config = modeling_bert.BertConfig.from_json_file(args.bert_config_file)
(total_loss, bmeo_pred_ids, attr_pred_ids) = create_model(
bert_config, args.is_training, input_ids_p, input_mask_p, segment_ids_p, bmeo_label2id, attr_label2id,
bmeo_label_ids_p, attr_label_ids_p, num_bmeo_labels, num_attr_labels, args.use_one_hot_embeddings)
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint(args.output_dir))
tokenizer = tokenization.FullTokenizer(
vocab_file=args.vocab_file, do_lower_case=FLAGS.do_lower_case)
# 获取id2char字典
id2char = tokenizer.inv_vocab
while True:
print('input the test sentence:')
sentence = str(input())
start = datetime.now()
if len(sentence) < 2:
print(sentence)
continue
# print('your input is:{}'.format(sentence))
input_ids, input_mask, segment_ids, bmeo_label_ids, attr_label_ids = convert(sentence)
feed_dict = {input_ids_p: input_ids,
input_mask_p: input_mask,
segment_ids_p: segment_ids,
bmeo_label_ids_p: bmeo_label_ids,
attr_label_ids_p: attr_label_ids}
# run session get current feed_dict result
bmeo_y_pred, attr_y_pred = sess.run([bmeo_pred_ids, attr_pred_ids], feed_dict)
bmeo_sent_tag = []
attr_sent_tag = []
bmeo_y_pred_clean = []
attr_y_pred_clean = []
input_ids_clean = []
# 去除 [CLS] 和 [SEP]获取正确的tag范围
for index_b, id in enumerate(list(np.reshape(input_ids, -1))):
char = id2char[id]
bmeo_tag = bmeo_id2label[list(bmeo_y_pred[0])[index_b]]
attr_tag = attr_id2label[list(attr_y_pred[0])[index_b]]
if char == "[CLS]":
continue
if char == "[SEP]":
break
input_ids_clean.append(id)
bmeo_sent_tag.append(bmeo_tag)
attr_sent_tag.append(attr_tag)
bmeo_y_pred_clean.append(list(bmeo_y_pred[0])[index_b])
attr_y_pred_clean.append(list(attr_y_pred[0])[index_b])
pred_sent_label = trans_label([bmeo_y_pred_clean], [attr_y_pred_clean])
sent_tag = ' '.join(pred_sent_label[0])
print(sentence + '\n' + sent_tag)
entity = get_entity_without_labelid([sentence], pred_sent_label)
print('predict_result:')
print(entity)
print('Time used: {} sec'.format((datetime.now() - start).seconds))
def predict_outline():
"""
do offline prediction. each time make prediction for one instance.
you can change to a batch if you want.
"""
# TODO 以文件形式预测结果,暂未开发,目前保持和 predict_online 一致
def convert(line):
feature = convert_single_example(line, bmeo_label2id, attr_label2id, FLAGS.max_seq_length, tokenizer)
input_ids = np.reshape([feature.input_ids], (args.batch_size, FLAGS.max_seq_length))
input_mask = np.reshape([feature.input_mask], (args.batch_size, FLAGS.max_seq_length))
segment_ids = np.reshape([feature.segment_ids], (args.batch_size, FLAGS.max_seq_length))
bmeo_label_ids = np.reshape([feature.bmeo_label_ids], (args.batch_size, FLAGS.max_seq_length))
attr_label_ids = np.reshape([feature.attr_label_ids], (args.batch_size, FLAGS.max_seq_length))
return input_ids, input_mask, segment_ids, bmeo_label_ids, attr_label_ids
global graph
with graph.as_default():
print("going to restore checkpoint")
# sess.run(tf.global_variables_initializer())
input_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="input_ids")
input_mask_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="input_mask")
bmeo_label_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="bmeo_label_ids")
attr_label_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="attr_label_ids")
segment_ids_p = tf.placeholder(tf.int32, [1, FLAGS.max_seq_length], name="segment_ids")
bert_config = modeling_bert.BertConfig.from_json_file(args.bert_config_file)
(total_loss, bmeo_pred_ids, attr_pred_ids) = create_model(
bert_config, args.is_training, input_ids_p, input_mask_p, segment_ids_p, bmeo_label2id, attr_label2id,
bmeo_label_ids_p, attr_label_ids_p, num_bmeo_labels, num_attr_labels, args.use_one_hot_embeddings)
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint(args.output_dir))
tokenizer = tokenization.FullTokenizer(
vocab_file=args.vocab_file, do_lower_case=FLAGS.do_lower_case)
# 获取id2char字典
id2char = tokenizer.inv_vocab
while True:
print('input the test sentence:')
sentence = str(input())
start = datetime.now()
if len(sentence) < 2:
print(sentence)
continue
# print('your input is:{}'.format(sentence))
input_ids, input_mask, segment_ids, bmeo_label_ids, attr_label_ids = convert(sentence)
feed_dict = {input_ids_p: input_ids,
input_mask_p: input_mask,
segment_ids_p: segment_ids,
bmeo_label_ids_p: bmeo_label_ids,
attr_label_ids_p: attr_label_ids}
# run session get current feed_dict result
bmeo_y_pred, attr_y_pred = sess.run([bmeo_pred_ids, attr_pred_ids], feed_dict)
bmeo_sent_tag = []
attr_sent_tag = []
bmeo_y_pred_clean = []
attr_y_pred_clean = []
input_ids_clean = []
# 去除 [CLS] 和 [SEP]获取正确的tag范围
for index_b, id in enumerate(list(np.reshape(input_ids, -1))):
char = id2char[id]
bmeo_tag = bmeo_id2label[list(bmeo_y_pred[0])[index_b]]
attr_tag = attr_id2label[list(attr_y_pred[0])[index_b]]
if char == "[CLS]":
continue
if char == "[SEP]":
break
input_ids_clean.append(id)
bmeo_sent_tag.append(bmeo_tag)
attr_sent_tag.append(attr_tag)
bmeo_y_pred_clean.append(list(bmeo_y_pred[0])[index_b])
attr_y_pred_clean.append(list(attr_y_pred[0])[index_b])
pred_sent_label = trans_label([bmeo_y_pred_clean], [attr_y_pred_clean])
sent_tag = ' '.join(pred_sent_label[0])
print(sentence + '\n' + sent_tag)
entity = get_entity_without_labelid([sentence], pred_sent_label)
print('predict_result:')
print(entity)
print('Time used: {} sec'.format((datetime.now() - start).seconds))
def convert_single_example_dev(ex_index, text, bmeo_label, attr_label, bmeo_label2id, attr_label2id, max_seq_length,
tokenizer):
"""
将一个样本进行分析,然后将字转化为id, 标签转化为id,然后结构化到InputFeatures对象中
:param ex_index: index
:param example: 一个样本
:param label_list: 标签列表
:param max_seq_length:
:param tokenizer:
:param mode:
:return:
"""
bmeo_label_map = bmeo_label2id
attr_label_map = attr_label2id
bmeo_O_index = bmeo_label_map["O"]
attr_O_index = attr_label_map["O"]
# bmeo_L: ['B', 'M', 'M', 'E']
# attr_L: ['ORG', 'ORG', 'ORG', 'ORG']
# W: ['黑', '龙', '江', '省']
textlist = text.split(' ')
bmeo_labellist = bmeo_label.split(' ')
attr_labellist = attr_label.split(' ')
tokens = []
bmeo_labels = []
attr_labels = []
for i, word in enumerate(textlist):
# 对每个字进行tokenize,返回list
token = tokenizer.tokenize(word)
tokens.extend(token)
bmeo_label_1 = bmeo_labellist[i]
attr_label_1 = attr_labellist[i]
for m in range(len(token)):
if m == 0:
bmeo_labels.append(bmeo_label_1)
attr_labels.append(attr_label_1)
else: # 一般不会出现else
bmeo_labels.append("X")
attr_labels.append("X")
# 序列截断
if len(tokens) >= max_seq_length - 1:
tokens = tokens[0:(max_seq_length - 2)] # -2 的原因是因为序列需要加一个句首和句尾标志
bmeo_labels = bmeo_labels[0:(max_seq_length - 2)]
attr_labels = attr_labels[0:(max_seq_length - 2)]
ntokens = []
segment_ids = []
bmeo_label_ids = []
attr_label_ids = []
ntokens.append("[CLS]") # 句子开始设置CLS 标志
segment_ids.append(0)
bmeo_label_ids.append(bmeo_label_map["[CLS]"]) #
attr_label_ids.append(attr_label_map["[CLS]"])
for i, token in enumerate(tokens):
ntokens.append(token)
segment_ids.append(0)
bmeo_label_ids.append(bmeo_label_map[bmeo_labels[i]])
attr_label_ids.append(attr_label_map[attr_labels[i]])
ntokens.append("[SEP]") # 句尾添加[SEP] 标志
segment_ids.append(0)
bmeo_label_ids.append(bmeo_label_map["[SEP]"])
attr_label_ids.append(attr_label_map["[SEP]"])
input_ids = tokenizer.convert_tokens_to_ids(ntokens) # 将序列中的字(ntokens)转化为ID形式
input_mask = [1] * len(input_ids)
while len(input_ids) < max_seq_length:
input_ids.append(0)
input_mask.append(0)
segment_ids.append(0)
# bmeo_label用BMEO中的O去padding,可以忽略此时的padding
bmeo_label_ids.append(bmeo_O_index)
# attr_label用attr中的O去padding,可以忽略此时的padding
attr_label_ids.append(attr_O_index)
ntokens.append("[PAD]")
# label_mask.append(0)
# print(len(input_ids))
assert len(input_ids) == max_seq_length
assert len(input_mask) == max_seq_length
assert len(segment_ids) == max_seq_length
assert len(bmeo_label_ids) == max_seq_length
assert len(attr_label_ids) == max_seq_length
# assert len(label_mask) == max_seq_length
# 打印部分样本数据信息
if ex_index < 5:
tf.logging.info("*** Example ***")
tf.logging.info("tokens: %s" % " ".join(
[tokenization.printable_text(x) for x in tokens]))
tf.logging.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
tf.logging.info("input_mask: %s" % " ".join([str(x) for x in input_mask]))
tf.logging.info("segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
tf.logging.info("bmeo_label_ids: %s" % " ".join([str(x) for x in bmeo_label_ids]))
tf.logging.info("attr_label_ids: %s" % " ".join([str(x) for x in attr_label_ids]))
# tf.logging.info("label_mask: %s" % " ".join([str(x) for x in label_mask]))
# 结构化为一个类
feature = InputFeatures(
input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids,
bmeo_label_ids=bmeo_label_ids,
attr_label_ids=attr_label_ids,
# label_mask = label_mask
)
return feature
def convert_single_example(example, bmeo_label2id, attr_label2id, max_seq_length, tokenizer):
"""
将一个样本进行分析,然后将字转化为id, 标签转化为id,然后结构化到InputFeatures对象中
:param ex_index: index
:param example: 一个样本
:param label_list: 标签列表
:param max_seq_length:
:param tokenizer:
:param mode:
:return:
"""
bmeo_label_map = bmeo_label2id
attr_label_map = attr_label2id
bmeo_O_index = bmeo_label_map["O"]
attr_O_index = attr_label_map["O"]
tokens = tokenizer.tokenize(example)
# tokens = tokenizer.tokenize(example.text)
# 序列截断
if len(tokens) >= max_seq_length - 1:
tokens = tokens[0:(max_seq_length - 2)] # -2 的原因是因为序列需要加一个句首和句尾标志
ntokens = []
segment_ids = []
bmeo_label_ids = []
attr_label_ids = []
ntokens.append("[CLS]") # 句子开始设置CLS 标志
segment_ids.append(0)
# append("O") or append("[CLS]") not sure!
bmeo_label_ids.append(bmeo_label_map["[CLS]"]) #
attr_label_ids.append(attr_label_map["[CLS]"])
for i, token in enumerate(tokens):
ntokens.append(token)
segment_ids.append(0)
bmeo_label_ids.append(0)
attr_label_ids.append(0)
ntokens.append("[SEP]") # 句尾添加[SEP] 标志
segment_ids.append(0)
bmeo_label_ids.append(bmeo_label_map["[SEP]"])
attr_label_ids.append(attr_label_map["[SEP]"])
input_ids = tokenizer.convert_tokens_to_ids(ntokens) # 将序列中的字(ntokens)转化为ID形式
input_mask = [1] * len(input_ids)
# padding, 使用
while len(input_ids) < max_seq_length:
input_ids.append(0)
input_mask.append(0)
segment_ids.append(0)
# bmeo_label用BMEO中的O去padding,可以忽略此时的padding
bmeo_label_ids.append(bmeo_O_index)
# attr_label用attr中的O去padding,可以忽略此时的padding
attr_label_ids.append(attr_O_index)
# label_mask.append(0)
# print(len(input_ids))
assert len(input_ids) == max_seq_length
assert len(input_mask) == max_seq_length
assert len(segment_ids) == max_seq_length
assert len(bmeo_label_ids) == max_seq_length
assert len(attr_label_ids) == max_seq_length
# assert len(label_mask) == max_seq_length
# 结构化为一个类
feature = InputFeatures(
input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids,
bmeo_label_ids=bmeo_label_ids,
attr_label_ids=attr_label_ids,
# label_mask = label_mask
)
return feature
if __name__ == "__main__":
dev_texts, dev_bmeo_labels, dev_attr_labels = zip(*parse_file(args.dev_file))
print('dev_texts')
print(dev_texts)
dev_offline(args.dev_file)
# dev_offline(args.test_file)
# if FLAGS.do_predict_outline:
# predict_outline()
# if FLAGS.do_predict_online:
# predict_online()