[docs]def eval_result(true_labels, pred_result, rel2id, logger, use_name=False):
correct = 0
total = len(true_labels)
correct_positive = 0
pred_positive = 0
gold_positive = 0
neg = -1
for name in ['NA', 'na', 'no_relation', 'Other', 'Others', 'none', 'None']:
if name in rel2id:
if use_name:
neg = name
else:
neg = rel2id[name]
break
for i in range(total):
if use_name:
golden = true_labels[i]
else:
golden = true_labels[i]
if golden == pred_result[i]:
correct += 1
if golden != neg:
correct_positive += 1
if golden != neg:
gold_positive += 1
if pred_result[i] != neg:
pred_positive += 1
acc = float(correct) / float(total)
try:
micro_p = float(correct_positive) / float(pred_positive)
except:
micro_p = 0
try:
micro_r = float(correct_positive) / float(gold_positive)
except:
micro_r = 0
try:
micro_f1 = 2 * micro_p * micro_r / (micro_p + micro_r)
except:
micro_f1 = 0
result = {'acc': acc, 'micro_p': micro_p, 'micro_r': micro_r, 'micro_f1': micro_f1}
logger.info('Evaluation result: {}.'.format(result))
return result