-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluate_commonness.py
135 lines (122 loc) · 5.08 KB
/
evaluate_commonness.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
from __future__ import division
import sys
import codecs
import re
from collections import defaultdict
standardfile = codecs.open(sys.argv[1],"r","utf-8")
commonnessfile = codecs.open(sys.argv[2],"r","utf-8")
frogfile = codecs.open(sys.argv[3],"r","utf-8")
outfile_stats = open(sys.argv[4],"w")
outfile_commonness_fp = codecs.open(sys.argv[5],"w","utf-8")
outfile_commonness_fn = codecs.open(sys.argv[6],"w","utf-8")
outfile_frog_fp = codecs.open(sys.argv[7],"w",'utf-8')
outfile_frog_fn = codecs.open(sys.argv[8],"w","utf-8")
match = defaultdict(list)
#link entities to ids
#standard
for line in standardfile.readlines():
tokens = line.strip().split("\t")
tid = tokens[0]
entities = tokens[2].split(",")
if entities[0] == "x":
entities = []
match[tid].append(entities)
standardkeys = match.keys()
#commonness
for line in commonnessfile.readlines():
tokens = line.strip().split("\t")
tid = tokens[0]
if tid in standardkeys:
entities = tokens[6].split(" | ")
entities = [x for x in entities if not re.search(r"^#",x)]
if len(entities) > 0:
if entities[0] == "--":
entities = []
match[tid].append(entities)
#frog
for line in frogfile.readlines():
tokens = line.strip().split("\t")
tid = tokens[1]
if tid in standardkeys:
entities = tokens[8].split(" | ")
entities = [x.replace("_"," ").lower() for x in entities if not re.search(r"^#",x)]
if len(entities) > 0:
if entities[0] == "x":
entities = []
match[tid].append(entities)
tpc = 0
fpc = 0
fnc = 0
tpf = 0
fpf = 0
fnf = 0
tpcl = 0
fpcl = 0
fncl = 0
tpfl = 0
fpfl = 0
fnfl = 0
for key in match.keys():
lists = match[key]
print key,lists
if len(lists) != 3:
print key,len(lists)
tpc += len([x for x in lists[1] if x in lists[0]])
fpc += len([x for x in lists[1] if x not in lists[0]])
fnc += len([x for x in lists[0] if x not in lists[1]])
tpf += len([x for x in lists[2] if x in lists[0]])
fpf += len([x for x in lists[2] if x not in lists[0]])
fnf += len([x for x in lists[0] if x not in lists[2]])
reflist = lists[0][:]
for x in lists[1]:
tp = False
for r in reflist:
if re.search(x.replace("(","\(").replace(")","\)").replace("*","\*"),r.replace("(","\(").replace(")","\)").replace("*","\*")) or re.search(r.replace("(","\(").replace(")","\)").replace("*","\*"),x.replace("(","\(").replace(")","\)").replace("*","\*")):
tp = True
if tp:
tpcl += 1
else:
fpcl += 1
outfile_commonness_fp.write(x + "\n")
for r in reflist:
tp = False
for x in lists[1]:
if re.search(x.replace("(","\(").replace(")","\)").replace("*","\*"),r.replace("(","\(").replace(")","\)").replace("*","\*")) or re.search(r.replace("(","\(").replace(")","\)").replace("*","\*"),x.replace("(","\(").replace(")","\)").replace("*","\*")):
tp = True
if not tp:
fncl += 1
outfile_commonness_fn.write(r + "\n")
for x in lists[2]:
tp = False
for r in reflist:
print r,x
if re.search(x.replace("(","\(").replace(")","\)").replace("*","\*"),r.replace("(","\(").replace(")","\)").replace("*","\*")) or re.search(r.replace("(","\(").replace(")","\)").replace("*","\*"),x.replace("(","\(").replace(")","\)").replace("*","\*")):
tp = True
if tp:
tpfl += 1
else:
fpfl += 1
outfile_frog_fp.write(x+"\n")
for r in reflist:
tp = False
for x in lists[2]:
if re.search(x.replace("(","\(").replace(")","\)").replace("*","\*"),r.replace("(","\(").replace(")","\)").replace("*","\*")) or re.search(r.replace("(","\(").replace(")","\)").replace("*","\*"),x.replace("(","\(").replace(")","\)").replace("*","\*")):
tp = True
if not tp:
fnfl += 1
outfile_frog_fn.write(r + "\n")
print lists,"tpc",tpc,"fpc",fpc,"fnc",fnc,"tpf",tpf,"fpf",fpf,"fnf",fnf,"tpcl",tpcl,"fpcl",fpcl,"fncl",fncl,"tpfl",tpfl,"fpfl",fpfl,"fnfl",fnfl
precisionc = tpc / (tpc+fpc)
recallc = tpc / (tpc+fnc)
f1c = 2 * ((precisionc*recallc) / (precisionc+recallc))
precisionf = tpf / (tpf+fpf)
recallf = tpf / (tpf+fnf)
f1f = 2 * ((precisionf*recallf) / (precisionf+recallf))
precisioncl = tpcl / (tpcl+fpcl)
recallcl = tpcl / (tpcl+fncl)
f1cl = 2 * ((precisioncl*recallcl) / (precisioncl+recallcl))
precisionfl = tpfl / (tpfl+fpfl)
recallfl = tpfl / (tpfl+fnfl)
f1fl = 2 * ((precisionfl*recallfl) / (precisionfl+recallfl))
outfile_stats.write("commonness: precision - " + str(precisionc) + ",recall - " + str(recallc) + ",f1 - " + str(f1c) + "\nfrog: precision - " + str(precisionf) + ",recall - " + str(recallf) + ",f1 - " + str(f1f) +"\ncommonness lax: precision - " + str(precisioncl) + ",recall - " + str(recallcl) + ",f1 - " + str(f1cl) + "\nfrog lax: precision - " + str(precisionfl) + ",recall - " + str(recallfl) + ",f1 - " + str(f1fl) + "\n")
#print len(match.keys()),[x for x in match.values() if len(x) < 2]