lixin12345 commited on
Commit
49c94ac
·
verified ·
1 Parent(s): faab47a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +117 -3
README.md CHANGED
@@ -1,3 +1,117 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ### Usage
3
+
4
+ ```python
5
+
6
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
7
+ import torch
8
+
9
+ class NER:
10
+ """
11
+ 实体命名实体识别
12
+ """
13
+ def __init__(self,model_path) -> None:
14
+ """
15
+ Args:
16
+ model_path:模型地址
17
+ """
18
+
19
+ self.model_path = model_path
20
+ self.tokenizer = AutoTokenizer.from_pretrained(model_path)
21
+ self.model = AutoModelForTokenClassification.from_pretrained(model_path)
22
+
23
+ def ner(self,sentence:str) -> list:
24
+ """
25
+ 命名实体识别
26
+ Args:
27
+ sentence:要识别的句子
28
+ Return:
29
+ 实体列表:[{'type':'LOC','tokens':[...]},...]
30
+ """
31
+ ans = []
32
+ for i in range(0,len(sentence),500):
33
+ ans = ans + self._ner(sentence[i:i+500])
34
+ return ans
35
+
36
+ def _ner(self,sentence:str) -> list:
37
+ if len(sentence) == 0: return []
38
+ inputs = self.tokenizer(
39
+ sentence, add_special_tokens=True, return_tensors="pt"
40
+ )
41
+
42
+ if torch.cuda.is_available():
43
+ self.model = self.model.to(torch.device('cuda:0'))
44
+ for key in inputs:
45
+ inputs[key] = inputs[key].to(torch.device('cuda:0'))
46
+
47
+ with torch.no_grad():
48
+ logits = self.model(**inputs).logits
49
+ predicted_token_class_ids = logits.argmax(-1)
50
+ predicted_tokens_classes = [self.model.config.id2label[t.item()] for t in predicted_token_class_ids[0]]
51
+ entities = []
52
+ entity = {}
53
+ for idx, token in enumerate(self.tokenizer.tokenize(sentence,add_special_tokens=True)):
54
+ if 'B-' in predicted_tokens_classes[idx] or 'S-' in predicted_tokens_classes[idx]:
55
+ if len(entity) != 0:
56
+ entities.append(entity)
57
+ entity = {}
58
+ entity['type'] = predicted_tokens_classes[idx].replace('B-','').replace('S-','')
59
+ entity['tokens'] = [token]
60
+ elif 'I-' in predicted_tokens_classes[idx] or 'E-' in predicted_tokens_classes[idx] or 'M-' in predicted_tokens_classes[idx]:
61
+ if len(entity) == 0:
62
+ entity['type'] = predicted_tokens_classes[idx].replace('I-','').replace('E-','').replace('M-','')
63
+ entity['tokens'] = []
64
+ entity['tokens'].append(token)
65
+ else:
66
+ if len(entity) != 0:
67
+ entities.append(entity)
68
+ entity = {}
69
+ if len(entity) > 0:
70
+ entities.append(entity)
71
+ return entities
72
+
73
+ ner_model = NER('lixin12345/chinese-medical-ner')
74
+ text = """
75
+ 患者既往慢阻肺多年;冠心病史6年,平素规律服用心可舒、保心丸等控制可;双下肢静脉血栓3年,保守治疗效果可;左侧腹股沟斜疝无张力修补术后2年。否认"高血压、糖尿病"等慢性病病史,否认"肝炎、结核"等传染病病史及其密切接触史,否认其他手术、重大外伤、输血史,否认"食物、药物、其他"等过敏史,预防接种史随社会。
76
+ """
77
+ ans = ner_model.ner(text)
78
+ # ans
79
+
80
+ # DiseaseNameOrComprehensiveCertificate
81
+ # 慢阻肺
82
+
83
+ # DiseaseNameOrComprehensiveCertificate
84
+ # 冠心病
85
+
86
+ # Drug
87
+ # 心可舒
88
+
89
+ # Drug
90
+ # 保心丸
91
+
92
+ # DiseaseNameOrComprehensiveCertificate
93
+ # 双下肢静脉血栓
94
+
95
+ # DiseaseNameOrComprehensiveCertificate
96
+ # 左侧腹股沟斜疝无张力修补术
97
+
98
+ # DiseaseNameOrComprehensiveCertificate
99
+ # 高血压
100
+
101
+ # DiseaseNameOrComprehensiveCertificate
102
+ # 糖尿病
103
+
104
+ # DiseaseNameOrComprehensiveCertificate
105
+ # 肝炎
106
+
107
+ # DiseaseNameOrComprehensiveCertificate
108
+ # 结核
109
+ ```
110
+
111
+ ### Source
112
+
113
+ From hit [wi](https://wi.hit.edu.cn/gk.htm)
114
+
115
+ ---
116
+ license: apache-2.0
117
+ ---