-
Notifications
You must be signed in to change notification settings - Fork 0
/
norm_utils.py
277 lines (223 loc) · 10.9 KB
/
norm_utils.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
from options import opt
from my_utils import normalize_word
from data import my_tokenize
from data_structure import Entity
import multi_sieve
import copy
import logging
import ensemble
from stopword import stop_word
def normalize_word(word):
new_word = ""
for char in word:
if char.isdigit():
new_word += '0'
else:
new_word += char
return new_word
def word_preprocess(word):
if opt.norm_number_normalized:
word = normalize_word(word)
word = word.lower()
return word
def build_alphabet(alphabet, data):
for document in data:
for sentence in document.sentences:
for token in sentence:
word = token['text']
if word in stop_word:
continue
alphabet.add(word_preprocess(word))
def open_alphabet(alphabet):
alphabet.open()
def fix_alphabet(alphabet):
alphabet.close()
def build_alphabet_from_dict(alphabet, dictionary, isMeddra_dict):
if isMeddra_dict:
for concept_id, concept_name in dictionary.items():
tokens = my_tokenize(concept_name)
for word in tokens:
if word in stop_word:
continue
alphabet.add(word_preprocess(word))
else:
for concept_id, concept in dictionary.items():
for concept_name in concept.names:
tokens = my_tokenize(concept_name)
for word in tokens:
if word in stop_word:
continue
alphabet.add(word_preprocess(word))
def get_dict_index(dict_alphabet, concept_id):
index = dict_alphabet.get_index(concept_id)-2 # since alphabet begin at 2
return index
def get_dict_name(dict_alphabet, concept_index):
name = dict_alphabet.get_instance(concept_index+2)
return name
def init_dict_alphabet(dict_alphabet, dictionary):
# concept_name may be string or umls_concept
for concept_id, concept_name in dictionary.items():
dict_alphabet.add(concept_id)
def get_dict_size(dict_alphabet):
return dict_alphabet.size()-2
def evaluate(documents, dictionary, dictionary_reverse, vsm_model, neural_model, ensemble_model, d, isMeddra_dict):
if vsm_model is not None :
vsm_model.eval()
if neural_model is not None:
neural_model.eval()
if ensemble_model is not None:
ensemble_model.eval()
ct_predicted = 0
ct_gold = 0
ct_correct = 0
# if opt.norm_rule and opt.norm_vsm and opt.norm_neural:
# ct_correct_rule = 0
# ct_correct_vsm = 0
# ct_correct_neural = 0
# ct_correct_all = 0
# ct_correct_rule_vsm = 0
# ct_correct_rule_neural = 0
# ct_correct_vsm_neural = 0
for document in documents:
# copy entities from gold entities
pred_entities = []
for gold in document.entities:
pred = Entity()
pred.id = gold.id
pred.type = gold.type
pred.spans = gold.spans
pred.section = gold.section
pred.name = gold.name
pred_entities.append(pred)
if opt.norm_rule and opt.norm_vsm and opt.norm_neural:
if opt.ensemble == 'learn':
ensemble_model.process_one_doc(document, pred_entities, dictionary, dictionary_reverse, isMeddra_dict)
else:
pred_entities2 = copy.deepcopy(pred_entities)
pred_entities3 = copy.deepcopy(pred_entities)
merge_entities = copy.deepcopy(pred_entities)
multi_sieve.runMultiPassSieve(document, pred_entities, dictionary, isMeddra_dict)
vsm_model.process_one_doc(document, pred_entities2, dictionary, dictionary_reverse, isMeddra_dict)
neural_model.process_one_doc(document, pred_entities3, dictionary, dictionary_reverse, isMeddra_dict)
elif opt.norm_rule:
multi_sieve.runMultiPassSieve(document, pred_entities, dictionary, isMeddra_dict)
elif opt.norm_vsm:
vsm_model.process_one_doc(document, pred_entities, dictionary, dictionary_reverse, isMeddra_dict)
elif opt.norm_neural:
neural_model.process_one_doc(document, pred_entities, dictionary, dictionary_reverse, isMeddra_dict)
else:
raise RuntimeError("wrong configuration")
if opt.norm_rule and opt.norm_vsm and opt.norm_neural:
# ct_gold += len(document.entities)
# ct_predicted += len(pred_entities)
# up bound of ensemble, if at least one system makes a correct prediction, we count it as correct.
# for idx, gold in enumerate(document.entities):
# if (pred_entities[idx].rule_id is not None and pred_entities[idx].rule_id in gold.norm_ids)\
# and (pred_entities2[idx].vsm_id is not None and pred_entities2[idx].vsm_id in gold.norm_ids) \
# and (pred_entities3[idx].neural_id is not None and pred_entities3[idx].neural_id in gold.norm_ids):
# ct_correct_all += 1
# ct_correct += 1
#
# if (pred_entities[idx].rule_id is not None and pred_entities[idx].rule_id in gold.norm_ids)\
# and (pred_entities2[idx].vsm_id is None or pred_entities2[idx].vsm_id not in gold.norm_ids) \
# and (pred_entities3[idx].neural_id is None or pred_entities3[idx].neural_id not in gold.norm_ids):
# ct_correct_rule += 1
# ct_correct += 1
#
# if (pred_entities[idx].rule_id is None or pred_entities[idx].rule_id not in gold.norm_ids)\
# and (pred_entities2[idx].vsm_id is not None and pred_entities2[idx].vsm_id in gold.norm_ids) \
# and (pred_entities3[idx].neural_id is None or pred_entities3[idx].neural_id not in gold.norm_ids):
# ct_correct_vsm += 1
# ct_correct += 1
#
# if (pred_entities[idx].rule_id is None or pred_entities[idx].rule_id not in gold.norm_ids)\
# and (pred_entities2[idx].vsm_id is None or pred_entities2[idx].vsm_id not in gold.norm_ids) \
# and (pred_entities3[idx].neural_id is not None and pred_entities3[idx].neural_id in gold.norm_ids):
# ct_correct_neural += 1
# ct_correct += 1
#
# if (pred_entities[idx].rule_id is not None and pred_entities[idx].rule_id in gold.norm_ids)\
# and (pred_entities2[idx].vsm_id is not None and pred_entities2[idx].vsm_id in gold.norm_ids) \
# and (pred_entities3[idx].neural_id is None or pred_entities3[idx].neural_id not in gold.norm_ids):
# ct_correct_rule_vsm += 1
# ct_correct += 1
#
# if (pred_entities[idx].rule_id is not None and pred_entities[idx].rule_id in gold.norm_ids)\
# and (pred_entities2[idx].vsm_id is None or pred_entities2[idx].vsm_id not in gold.norm_ids) \
# and (pred_entities3[idx].neural_id is not None and pred_entities3[idx].neural_id in gold.norm_ids):
# ct_correct_rule_neural += 1
# ct_correct += 1
#
# if (pred_entities[idx].rule_id is None or pred_entities[idx].rule_id not in gold.norm_ids)\
# and (pred_entities2[idx].vsm_id is not None and pred_entities2[idx].vsm_id in gold.norm_ids) \
# and (pred_entities3[idx].neural_id is not None and pred_entities3[idx].neural_id in gold.norm_ids):
# ct_correct_vsm_neural += 1
# ct_correct += 1
if opt.ensemble == 'learn':
if isMeddra_dict:
p1, p2, p3 = evaluate_for_fda(document.entities, pred_entities)
else:
p1, p2, p3 = evaluate_for_ehr(document.entities, pred_entities, dictionary)
ct_gold += p1
ct_predicted += p2
ct_correct += p3
else:
ensemble.merge_result(pred_entities, pred_entities2, pred_entities3, merge_entities, dictionary, isMeddra_dict, vsm_model.dict_alphabet, d)
if isMeddra_dict:
p1, p2, p3 = evaluate_for_fda(document.entities, merge_entities)
else:
p1, p2, p3 = evaluate_for_ehr(document.entities, merge_entities, dictionary)
ct_gold += p1
ct_predicted += p2
ct_correct += p3
else:
if isMeddra_dict:
p1, p2, p3 = evaluate_for_fda(document.entities, pred_entities)
else:
p1, p2, p3 = evaluate_for_ehr(document.entities, pred_entities, dictionary)
ct_gold += p1
ct_predicted += p2
ct_correct += p3
# if opt.norm_rule and opt.norm_vsm and opt.norm_neural:
# logging.info("ensemble correct. all:{} rule:{} vsm:{} neural:{} rule_vsm:{} rule_neural:{} vsm_neural:{}"
# .format(ct_correct_all, ct_correct_rule, ct_correct_vsm, ct_correct_neural, ct_correct_rule_vsm,
# ct_correct_rule_neural, ct_correct_vsm_neural))
#
# logging.info("gold:{} pred:{} correct:{}".format(ct_gold, ct_predicted, ct_correct))
if ct_gold == 0:
precision = 0
recall = 0
else:
precision = ct_correct * 1.0 / ct_predicted
recall = ct_correct * 1.0 / ct_gold
if precision+recall == 0:
f_measure = 0
else:
f_measure = 2*precision*recall/(precision+recall)
return precision, recall, f_measure
def evaluate_for_fda(gold_entities, pred_entities):
ct_gold = len(gold_entities)
ct_predicted = len(pred_entities)
ct_correct = 0
for idx, pred in enumerate(pred_entities):
gold = gold_entities[idx]
if len(pred.norm_ids) != 0 and pred.norm_ids[0] in gold.norm_ids:
ct_correct += 1
return ct_gold, ct_predicted, ct_correct
def evaluate_for_ehr(gold_entities, pred_entities, dictionary):
ct_norm_gold = len(gold_entities)
ct_norm_predict = len(pred_entities)
ct_norm_correct = 0
for predict_entity in pred_entities:
for gold_entity in gold_entities:
if predict_entity.equals_span(gold_entity):
if len(gold_entity.norm_ids) == 0:
# if gold_entity not annotated, we count it as TP
ct_norm_correct += 1
else:
if len(predict_entity.norm_ids) != 0 and predict_entity.norm_ids[0] in dictionary:
concept = dictionary[predict_entity.norm_ids[0]]
if gold_entity.norm_ids[0] in concept.codes:
ct_norm_correct += 1
break
return ct_norm_gold, ct_norm_predict, ct_norm_correct