-
Notifications
You must be signed in to change notification settings - Fork 2
/
score.py
32 lines (27 loc) · 891 Bytes
/
score.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
import sys
gold_file = sys.argv[1]
pred_file = sys.argv[2]
# Load the gold standard
gold = {}
for line in open(gold_file):
record, string, entity = line.strip().split('\t', 2)
gold[(record, string)] = entity
n_gold = len(gold)
print('Gold entities: %s' % n_gold)
# Load the predictions
pred = {}
for line in open(pred_file):
record, string, entity = line.strip().split('\t', 2)
pred[(record, string)] = entity
n_predicted = len(pred)
print('Linked entities: %s' % n_predicted)
# Evaluate predictions
n_correct = sum( int(pred[i]==gold[i]) for i in set(gold) & set(pred) )
print('Correct mappings: %s' % n_correct)
# Calculate scores
precision = float(n_correct) / float(n_predicted)
print('Precision: %s' % precision )
recall = float(n_correct) / float(n_gold)
print('Recall: %s' % recall )
f1 = 2 * ( (precision * recall) / (precision + recall) )
print('F1: %s' % f1 )