-
Notifications
You must be signed in to change notification settings - Fork 1
/
ensemble.py
executable file
·606 lines (470 loc) · 23.4 KB
/
ensemble.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
import logging
import torch.nn as nn
from alphabet import Alphabet
from options import opt
import norm_utils
from data import build_pretrain_embedding, my_tokenize, load_data_fda
from my_utils import random_embedding, freeze_net
import torch
from torch.utils.data import DataLoader, Dataset
import numpy as np
import torch.optim as optim
import time
import os
from data_structure import Entity
import torch.nn.functional as functional
import math
import multi_sieve
import vsm
import norm_neural
import copy
from collections import Counter
class Ensemble(nn.Module):
def __init__(self, word_alphabet, word_embedding, embedding_dim, dict_alphabet, poses):
super(Ensemble, self).__init__()
self.word_alphabet = word_alphabet
self.embedding_dim = embedding_dim
self.word_embedding = word_embedding
self.dict_alphabet = dict_alphabet
self.gpu = opt.gpu
self.poses = poses
self.dict_size = norm_utils.get_dict_size(dict_alphabet)
self.vsm_linear = nn.Linear(self.embedding_dim, self.embedding_dim, bias=False)
self.vsm_linear.weight.data.copy_(torch.eye(self.embedding_dim))
self.neural_linear = nn.Linear(self.embedding_dim, self.dict_size, bias=False)
# self.hidden_size = 2500
# self.dropout = nn.Dropout(opt.dropout)
# self.hidden = nn.Linear(3*self.dict_size, self.hidden_size)
# self.relu = nn.ReLU()
#
# self.output = nn.Linear(self.hidden_size, self.dict_size)
self.output = nn.Linear(3*self.dict_size, self.dict_size)
self.criterion = nn.CrossEntropyLoss()
if opt.gpu >= 0 and torch.cuda.is_available():
self.word_embedding = self.word_embedding.cuda(self.gpu)
self.vsm_linear = self.vsm_linear.cuda(self.gpu)
self.neural_linear = self.neural_linear.cuda(self.gpu)
# self.hidden = self.hidden.cuda(self.gpu)
self.output = self.output.cuda(self.gpu)
# if torch.cuda.is_available():
# self.w1 = torch.nn.Parameter(torch.tensor([0.3]).cuda(self.gpu))
# self.w2 = torch.nn.Parameter(torch.tensor([0.4]).cuda(self.gpu))
# self.w3 = torch.nn.Parameter(torch.tensor([0.3]).cuda(self.gpu))
# else:
# self.w1 = torch.nn.Parameter(torch.tensor([0.3]))
# self.w2 = torch.nn.Parameter(torch.tensor([0.4]))
# self.w3 = torch.nn.Parameter(torch.tensor([0.3]))
def forward(self, words, rules, lengths):
length = words.size(1)
mention_word_emb = self.word_embedding(words)
mention_word_emb = mention_word_emb.unsqueeze_(1)
mention_word_pool = functional.avg_pool2d(mention_word_emb, (length, 1))
mention_word_pool = mention_word_pool.squeeze_(1).squeeze_(1)
length = self.poses.size(1)
pos_word_emb = self.word_embedding(self.poses)
pos_word_emb = pos_word_emb.unsqueeze_(1)
pos_word_pool = functional.avg_pool2d(pos_word_emb, (length, 1))
pos_word_pool = pos_word_pool.squeeze_(1).squeeze_(1)
m_W = self.vsm_linear(mention_word_pool)
vsm_confidences = torch.matmul(m_W, torch.t(pos_word_pool))
vsm_confidences = functional.softmax(vsm_confidences, dim=1)
# batch_size = words.size(0)
# rule_confidences = torch.zeros(batch_size, self.dict_size)
# if torch.cuda.is_available():
# rule_confidences = rule_confidences.cuda(self.gpu)
# rule_confidences = rule_confidences.scatter_(1, rules, 1)
rule_confidences = rules
neural_confidences = self.neural_linear(mention_word_pool)
neural_confidences = functional.softmax(neural_confidences, dim=1)
# confidences = self.w1*rule_confidences+self.w2*vsm_confidences+self.w3*neural_confidences
# confidences = self.w1 * rule_confidences + self.w2 * vsm_confidences \
# + self.w3 * neural_confidences
x = torch.cat((rule_confidences, vsm_confidences, neural_confidences), 1)
# x = self.relu(self.hidden(self.dropout(x)))
confidences = self.output(x)
return confidences
def loss(self, y_pred, y_gold):
return self.criterion(y_pred, y_gold)
# def normalize(self):
#
# e_w1 = torch.exp(self.w1.data)
# e_w2 = torch.exp(self.w2.data)
# e_w3 = torch.exp(self.w3.data)
# e = e_w1+e_w2+e_w3+1e-8
#
# self.w1.data = e_w1 / e
# self.w2.data = e_w2 / e
# self.w3.data = e_w3 / e
# min = 99999
# max = -99999
# for x in [self.w1, self.w2, self.w3]:
# if x < min:
# min = x.data
# if x > max:
# max = x.data
#
# self.w1.data = (self.w1.data-min)/(max-min)
# self.w2.data = (self.w2.data- min) / (max - min)
# self.w3.data = (self.w3.data- min) / (max - min)
def process_one_doc(self, doc, entities, dictionary, dictionary_reverse, isMeddra_dict):
Xs, Ys = generate_instances(doc, self.word_alphabet, self.dict_alphabet, dictionary, dictionary_reverse, isMeddra_dict)
data_loader = DataLoader(MyDataset(Xs, Ys), opt.batch_size, shuffle=False, collate_fn=my_collate)
data_iter = iter(data_loader)
num_iter = len(data_loader)
entity_start = 0
for i in range(num_iter):
words, rules, lengths, _ = next(data_iter)
y_pred = self.forward(words, rules, lengths)
values, indices = torch.max(y_pred, 1)
actual_batch_size = lengths.size(0)
for batch_idx in range(actual_batch_size):
entity = entities[entity_start+batch_idx]
norm_id = norm_utils.get_dict_name(self.dict_alphabet, indices[batch_idx].item())
if isMeddra_dict:
name = dictionary[norm_id]
entity.norm_ids.append(norm_id)
entity.norm_names.append(name)
else:
concept = dictionary[norm_id]
entity.norm_ids.append(norm_id)
entity.norm_names.append(concept.names)
entity_start += actual_batch_size
def generate_instances(document, word_alphabet, dict_alphabet, dictionary, dictionary_reverse, isMeddra_dict):
Xs = []
Ys = []
# 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)
multi_sieve.runMultiPassSieve(document, pred_entities, dictionary, isMeddra_dict)
for idx, entity in enumerate(document.entities):
if isMeddra_dict:
if len(entity.norm_ids) > 0:
Y = norm_utils.get_dict_index(dict_alphabet, entity.norm_ids[0])
if Y >= 0 and Y < norm_utils.get_dict_size(dict_alphabet):
Ys.append(Y)
else:
continue
else:
Ys.append(0)
else:
if len(entity.norm_ids) > 0:
if entity.norm_ids[0] in dictionary_reverse:
cui_list = dictionary_reverse[entity.norm_ids[0]]
Y = norm_utils.get_dict_index(dict_alphabet, cui_list[0]) # use the first id to generate instance
if Y >= 0 and Y < norm_utils.get_dict_size(dict_alphabet):
Ys.append(Y)
else:
raise RuntimeError("entity {}, {}, cui not in dict_alphabet".format(entity.id, entity.name))
else:
logging.info("entity {}, {}, can't map to umls, ignored".format(entity.id, entity.name))
continue
else:
Ys.append(0)
X = dict()
tokens = my_tokenize(entity.name)
word_ids = []
for token in tokens:
token = norm_utils.word_preprocess(token)
word_id = word_alphabet.get_index(token)
word_ids.append(word_id)
X['word'] = word_ids
if pred_entities[idx].rule_id is None:
X['rule'] = [0]*norm_utils.get_dict_size(dict_alphabet)
else:
X['rule'] = [0]*norm_utils.get_dict_size(dict_alphabet)
X['rule'][norm_utils.get_dict_index(dict_alphabet, pred_entities[idx].rule_id)] = 1
Xs.append(X)
return Xs, Ys
class MyDataset(Dataset):
def __init__(self, X, Y):
self.X = X
self.Y = Y
def __len__(self):
return len(self.X)
def __getitem__(self, idx):
return (self.X[idx], self.Y[idx])
def my_collate(batch):
x, y = zip(*batch)
words = [s['word'] for s in x]
rules = [s['rule'] for s in x]
lengths = [len(row) for row in words]
max_len = max(lengths)
words = pad_sequence(words, max_len)
rules = torch.tensor(rules, dtype=torch.float32)
lengths = torch.LongTensor(lengths)
y = torch.LongTensor(y).view(-1)
if opt.gpu >= 0 and torch.cuda.is_available():
words = words.cuda(opt.gpu)
rules = rules.cuda(opt.gpu)
lengths = lengths.cuda(opt.gpu)
y = y.cuda(opt.gpu)
return words, rules, lengths, y
def pad_sequence(x, max_len):
padded_x = np.zeros((len(x), max_len), dtype=np.int)
for i, row in enumerate(x):
padded_x[i][:len(row)] = row
padded_x = torch.LongTensor(padded_x)
return padded_x
def train(train_data, dev_data, test_data, d, dictionary, dictionary_reverse, opt, fold_idx, isMeddra_dict):
logging.info("train the ensemble normalization model ...")
external_train_data = []
if d.config.get('norm_ext_corpus') is not None:
for k, v in d.config['norm_ext_corpus'].items():
if k == 'tac':
external_train_data.extend(load_data_fda(v['path'], True, v.get('types'), v.get('types'), False, True))
else:
raise RuntimeError("not support external corpus")
if len(external_train_data) != 0:
train_data.extend(external_train_data)
logging.info("build alphabet ...")
word_alphabet = Alphabet('word')
norm_utils.build_alphabet_from_dict(word_alphabet, dictionary, isMeddra_dict)
norm_utils.build_alphabet(word_alphabet, train_data)
if opt.dev_file:
norm_utils.build_alphabet(word_alphabet, dev_data)
if opt.test_file:
norm_utils.build_alphabet(word_alphabet, test_data)
norm_utils.fix_alphabet(word_alphabet)
if d.config.get('norm_emb') is not None:
logging.info("load pretrained word embedding ...")
pretrain_word_embedding, word_emb_dim = build_pretrain_embedding(d.config.get('norm_emb'),
word_alphabet,
opt.word_emb_dim, False)
word_embedding = nn.Embedding(word_alphabet.size(), word_emb_dim, padding_idx=0)
word_embedding.weight.data.copy_(torch.from_numpy(pretrain_word_embedding))
embedding_dim = word_emb_dim
else:
logging.info("randomly initialize word embedding ...")
word_embedding = nn.Embedding(word_alphabet.size(), d.word_emb_dim, padding_idx=0)
word_embedding.weight.data.copy_(
torch.from_numpy(random_embedding(word_alphabet.size(), d.word_emb_dim)))
embedding_dim = d.word_emb_dim
dict_alphabet = Alphabet('dict')
norm_utils.init_dict_alphabet(dict_alphabet, dictionary)
norm_utils.fix_alphabet(dict_alphabet)
# rule
logging.info("init rule-based normer")
multi_sieve.init(opt, train_data, d, dictionary, dictionary_reverse, isMeddra_dict)
if opt.ensemble == 'learn':
logging.info("init ensemble normer")
poses = vsm.init_vector_for_dict(word_alphabet, dict_alphabet, dictionary, isMeddra_dict)
ensemble_model = Ensemble(word_alphabet, word_embedding, embedding_dim, dict_alphabet, poses)
if pretrain_neural_model is not None:
ensemble_model.neural_linear.weight.data.copy_(pretrain_neural_model.linear.weight.data)
if pretrain_vsm_model is not None:
ensemble_model.vsm_linear.weight.data.copy_(pretrain_vsm_model.linear.weight.data)
ensemble_train_X = []
ensemble_train_Y = []
for doc in train_data:
temp_X, temp_Y = generate_instances(doc, word_alphabet, dict_alphabet, dictionary, dictionary_reverse, isMeddra_dict)
ensemble_train_X.extend(temp_X)
ensemble_train_Y.extend(temp_Y)
ensemble_train_loader = DataLoader(MyDataset(ensemble_train_X, ensemble_train_Y), opt.batch_size, shuffle=True, collate_fn=my_collate)
ensemble_optimizer = optim.Adam(ensemble_model.parameters(), lr=opt.lr, weight_decay=opt.l2)
if opt.tune_wordemb == False:
freeze_net(ensemble_model.word_embedding)
else:
# vsm
logging.info("init vsm-based normer")
poses = vsm.init_vector_for_dict(word_alphabet, dict_alphabet, dictionary, isMeddra_dict)
# alphabet can share between vsm and neural since they don't change
# but word_embedding cannot
vsm_model = vsm.VsmNormer(word_alphabet, copy.deepcopy(word_embedding), embedding_dim, dict_alphabet, poses)
vsm_train_X = []
vsm_train_Y = []
for doc in train_data:
if isMeddra_dict:
temp_X, temp_Y = vsm.generate_instances(doc.entities, word_alphabet, dict_alphabet)
else:
temp_X, temp_Y = vsm.generate_instances_ehr(doc.entities, word_alphabet, dict_alphabet, dictionary_reverse)
vsm_train_X.extend(temp_X)
vsm_train_Y.extend(temp_Y)
vsm_train_loader = DataLoader(vsm.MyDataset(vsm_train_X, vsm_train_Y), opt.batch_size, shuffle=True, collate_fn=vsm.my_collate)
vsm_optimizer = optim.Adam(vsm_model.parameters(), lr=opt.lr, weight_decay=opt.l2)
if opt.tune_wordemb == False:
freeze_net(vsm_model.word_embedding)
if d.config['norm_vsm_pretrain'] == '1':
vsm.dict_pretrain(dictionary, dictionary_reverse, d, True, vsm_optimizer, vsm_model)
# neural
logging.info("init neural-based normer")
neural_model = norm_neural.NeuralNormer(word_alphabet, copy.deepcopy(word_embedding), embedding_dim, dict_alphabet)
neural_train_X = []
neural_train_Y = []
for doc in train_data:
if isMeddra_dict:
temp_X, temp_Y = norm_neural.generate_instances(doc.entities, word_alphabet, dict_alphabet)
else:
temp_X, temp_Y = norm_neural.generate_instances_ehr(doc.entities, word_alphabet, dict_alphabet, dictionary_reverse)
neural_train_X.extend(temp_X)
neural_train_Y.extend(temp_Y)
neural_train_loader = DataLoader(norm_neural.MyDataset(neural_train_X, neural_train_Y), opt.batch_size, shuffle=True, collate_fn=norm_neural.my_collate)
neural_optimizer = optim.Adam(neural_model.parameters(), lr=opt.lr, weight_decay=opt.l2)
if opt.tune_wordemb == False:
freeze_net(neural_model.word_embedding)
if d.config['norm_neural_pretrain'] == '1':
neural_model.dict_pretrain(dictionary, dictionary_reverse, d, True, neural_optimizer, neural_model)
best_dev_f = -10
best_dev_p = -10
best_dev_r = -10
bad_counter = 0
logging.info("start training ...")
for idx in range(opt.iter):
epoch_start = time.time()
if opt.ensemble == 'learn':
ensemble_model.train()
ensemble_train_iter = iter(ensemble_train_loader)
ensemble_num_iter = len(ensemble_train_loader)
for i in range(ensemble_num_iter):
x, rules, lengths, y = next(ensemble_train_iter)
y_pred = ensemble_model.forward(x, rules, lengths)
l = ensemble_model.loss(y_pred, y)
l.backward()
if opt.gradient_clip > 0:
torch.nn.utils.clip_grad_norm_(ensemble_model.parameters(), opt.gradient_clip)
ensemble_optimizer.step()
ensemble_model.zero_grad()
else:
vsm_model.train()
vsm_train_iter = iter(vsm_train_loader)
vsm_num_iter = len(vsm_train_loader)
for i in range(vsm_num_iter):
x, lengths, y = next(vsm_train_iter)
l, _ = vsm_model.forward_train(x, lengths, y)
l.backward()
if opt.gradient_clip > 0:
torch.nn.utils.clip_grad_norm_(vsm_model.parameters(), opt.gradient_clip)
vsm_optimizer.step()
vsm_model.zero_grad()
neural_model.train()
neural_train_iter = iter(neural_train_loader)
neural_num_iter = len(neural_train_loader)
for i in range(neural_num_iter):
x, lengths, y = next(neural_train_iter)
y_pred = neural_model.forward(x, lengths)
l = neural_model.loss(y_pred, y)
l.backward()
if opt.gradient_clip > 0:
torch.nn.utils.clip_grad_norm_(neural_model.parameters(), opt.gradient_clip)
neural_optimizer.step()
neural_model.zero_grad()
epoch_finish = time.time()
logging.info("epoch: %s training finished. Time: %.2fs" % (idx, epoch_finish - epoch_start))
if opt.dev_file:
if opt.ensemble == 'learn':
# logging.info("weight w1: %.4f, w2: %.4f, w3: %.4f" % (ensemble_model.w1.data.item(), ensemble_model.w2.data.item(), ensemble_model.w3.data.item()))
p, r, f = norm_utils.evaluate(dev_data, dictionary, dictionary_reverse, None, None, ensemble_model, d, isMeddra_dict)
else:
p, r, f = norm_utils.evaluate(dev_data, dictionary, dictionary_reverse, vsm_model, neural_model, None, d, isMeddra_dict)
logging.info("Dev: p: %.4f, r: %.4f, f: %.4f" % (p, r, f))
else:
f = best_dev_f
if f > best_dev_f:
logging.info("Exceed previous best f score on dev: %.4f" % (best_dev_f))
if opt.ensemble == 'learn':
if fold_idx is None:
torch.save(ensemble_model, os.path.join(opt.output, "ensemble.pkl"))
else:
torch.save(ensemble_model, os.path.join(opt.output, "ensemble_{}.pkl".format(fold_idx+1)))
else:
if fold_idx is None:
torch.save(vsm_model, os.path.join(opt.output, "vsm.pkl"))
torch.save(neural_model, os.path.join(opt.output, "norm_neural.pkl"))
else:
torch.save(vsm_model, os.path.join(opt.output, "vsm_{}.pkl".format(fold_idx+1)))
torch.save(neural_model, os.path.join(opt.output, "norm_neural_{}.pkl".format(fold_idx + 1)))
best_dev_f = f
best_dev_p = p
best_dev_r = r
bad_counter = 0
else:
bad_counter += 1
if len(opt.dev_file) != 0 and bad_counter >= opt.patience:
logging.info('Early Stop!')
break
logging.info("train finished")
if fold_idx is None:
multi_sieve.finalize(True)
else:
if fold_idx == opt.cross_validation-1:
multi_sieve.finalize(True)
else:
multi_sieve.finalize(False)
if len(opt.dev_file) == 0:
if opt.ensemble == 'learn':
torch.save(ensemble_model, os.path.join(opt.output, "ensemble.pkl"))
else:
torch.save(vsm_model, os.path.join(opt.output, "vsm.pkl"))
torch.save(neural_model, os.path.join(opt.output, "norm_neural.pkl"))
return best_dev_p, best_dev_r, best_dev_f
def merge_result(entities1, entities2, entities3, merge_entities, dictionary, isMeddra_dict, dict_alphabet, d):
if opt.ensemble == 'vote':
for idx, merge_entity in enumerate(merge_entities):
entity1 = entities1[idx]
entity2 = entities2[idx]
entity3 = entities3[idx]
if entity1.rule_id is None:
if entity2.vsm_id == entity3.neural_id:
merge_entity.norm_ids.append(entity2.norm_ids[0])
merge_entity.norm_names.append(entity2.norm_names[0])
else:
# if entity2.norm_confidences[0] >= entity3.norm_confidences[0]:
# merge_entity.norm_ids.append(entity2.norm_ids[0])
# merge_entity.norm_names.append(entity2.norm_names[0])
# else:
# merge_entity.norm_ids.append(entity3.norm_ids[0])
# merge_entity.norm_names.append(entity3.norm_names[0])
# vsm is prior to others
merge_entity.norm_ids.append(entity2.norm_ids[0])
merge_entity.norm_names.append(entity2.norm_names[0])
else:
id_and_ticket = Counter()
id_and_ticket[entity1.norm_ids[0]] = id_and_ticket[entity1.norm_ids[0]] +1
id_and_ticket[entity2.norm_ids[0]] = id_and_ticket[entity2.norm_ids[0]] +1
id_and_ticket[entity3.norm_ids[0]] = id_and_ticket[entity3.norm_ids[0]] +1
temp_id_name = {}
temp_id_name[entity1.norm_ids[0]] = entity1.norm_names[0]
temp_id_name[entity2.norm_ids[0]] = entity2.norm_names[0]
temp_id_name[entity3.norm_ids[0]] = entity3.norm_names[0]
top_id, top_ct = id_and_ticket.most_common(1)[0]
if top_ct == 1:
# the confidence of rule is always 1
# merge_entity.norm_ids.append(entity1.norm_ids[0])
# merge_entity.norm_names.append(entity1.norm_names[0])
# vsm is prior to others
merge_entity.norm_ids.append(entity2.norm_ids[0])
merge_entity.norm_names.append(entity2.norm_names[0])
else:
merge_entity.norm_ids.append(top_id)
merge_entity.norm_names.append(temp_id_name[top_id])
elif opt.ensemble == 'sum':
for idx, merge_entity in enumerate(merge_entities):
entity1 = entities1[idx]
entity2 = entities2[idx]
entity3 = entities3[idx]
if entity1.rule_id is None:
total = float(d.config['norm_ensumble_sum_weight']['1']['w2'])*entity2.norm_confidences[0] + \
float(d.config['norm_ensumble_sum_weight']['1']['w3'])*entity3.norm_confidences[0]
else:
total = float(d.config['norm_ensumble_sum_weight']['2']['w1'])*entity1.norm_confidences[0] + \
float(d.config['norm_ensumble_sum_weight']['2']['w2'])*entity2.norm_confidences[0] + \
float(d.config['norm_ensumble_sum_weight']['2']['w3'])*entity3.norm_confidences[0]
index = total.argmax()
norm_id = norm_utils.get_dict_name(dict_alphabet, index)
if isMeddra_dict:
name = dictionary[norm_id]
merge_entity.norm_ids.append(norm_id)
merge_entity.norm_names.append(name)
else:
concept = dictionary[norm_id]
merge_entity.norm_ids.append(norm_id)
merge_entity.norm_names.append(concept.names)
else:
raise RuntimeError("run configuration")
return merge_entities