Views
No views yet
1from transformers import AutoTokenizer, AutoModelForTokenClassification
2tokenizer = AutoTokenizer.from_pretrained("Adapting/bert-base-chinese-finetuned-NER-biomedical")
3model = AutoModelForTokenClassification.from_pretrained("Adapting/bert-base-chinese-finetuned-NER-biomedical",revision='7f63e3d18b1dc3cc23041a89e77be21860704d2e')
4
5from transformers import pipeline
6nlp = pipeline('ner',model=model,tokenizer = tokenizer)
7
8tag_set = [
9 'B_手术',
10 'I_疾病和诊断',
11 'B_症状',
12 'I_解剖部位',
13 'I_药物',
14 'B_影像检查',
15 'B_药物',
16 'B_疾病和诊断',
17 'I_影像检查',
18 'I_手术',
19 'B_解剖部位',
20 'O',
21 'B_实验室检验',
22 'I_症状',
23 'I_实验室检验'
24 ]
25
26tag2id = lambda tag: tag_set.index(tag)
27id2tag = lambda id: tag_set[id]
28
29def readable_result(result):
30
31 results_in_word = []
32 j = 0
33 while j < len(result):
34 i = result[j]
35 entity = id2tag(int(i['entity'][i['entity'].index('_')+1:]))
36 token = i['word']
37 if entity.startswith('B'):
38 entity_name = entity[entity.index('_')+1:]
39
40 word = token
41 j = j+1
42 while j<len(result):
43 next = result[j]
44 next_ent = id2tag(int(next['entity'][next['entity'].index('_')+1:]))
45 next_token = next['word']
46
47 if next_ent.startswith('I') and next_ent[next_ent.index('_')+1:] == entity_name:
48 word += next_token
49 j += 1
50
51 if j >= len(result):
52 results_in_word.append((entity_name,word))
53 else:
54 results_in_word.append((entity_name,word))
55 break
56
57 else:
58 j += 1
59
60 return results_in_word
61
62
63
64print(readable_result(nlp('淋球菌性尿道炎会引起头痛')))
65
66'''
67[('疾病和诊断', '淋球菌性尿道炎'), ('症状', '头痛')]
68'''