-
Notifications
You must be signed in to change notification settings - Fork 58
/
evaluate.py
42 lines (34 loc) · 1.08 KB
/
evaluate.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2017-02-11 19:46:08
@author: heshenghuan ([email protected])
http://github.com/heshenghuan
"""
import sys
import codecs as cs
from lib.utils import eval_ner
def read_corpus(corpus):
with cs.open(corpus, 'r', 'utf-8') as src:
labels = []
src = src.read().strip().split('\n\n')
for sent in src:
label = []
for pair in sent.strip().split('\n'):
label.append(pair.split()[-1])
labels.append(label)
return labels
def evaluate(predictions, groundtruth=None):
if groundtruth is None:
return None, predictions
results = eval_ner(predictions, groundtruth)
return results, predictions
if __name__ == '__main__':
if len(sys.argv) != 3:
print "Arguments ERROR!"
print "Usage: python evaluate gold pred"
sys.exit(-1)
gold = read_corpus(sys.argv[1])
pred = read_corpus(sys.argv[2])
result, _ = evaluate(pred, gold)
print "Test F1: %f, P: %f, R: %f" % (result['f1'], result['p'], result['r'])