-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
178 lines (152 loc) · 4.68 KB
/
predict.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
import warnings
warnings.filterwarnings("ignore")
import os
import sys
stderr=sys.stderr
sys.stderr=open(os.devnull,'w')
import csv
import argparse
import numpy as np
from utils import util
from models.bownn import BOWNN,BOWNN_author
sys.stderr=stderr
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
parser=argparse.ArgumentParser(description='predict.py')
parser.add_argument('-input',default='data_sample/vocab/ConCode2Vid.json',type=str,help="input dictionary")
parser.add_argument('-output',default='data_sample/vocab/ConCode2Vid.json',type=str,help="output dictionary")
parser.add_argument('-author',default='data_sample/vocab/FAuthor2Vid.json',type=str,help="author dictionary")
parser.add_argument('-vocab',default='data_sample/vocab/Vid2Name.json',type=str,help="entity dictionary")
parser.add_argument('-A',default=False,type=bool,help="train with author info")
parser.add_argument('-E',default=False,type=bool,help="evaluate with real body")
parser.add_argument('-abstract',default='data_sample/sample_author/abs0.csv',type=str,help="abstract to predict")
parser.add_argument('-authors',default='data_sample/sample_author/authors0.json',type=str,help="author information")
parser.add_argument('-body',default='data_sample/sample_author/body0.csv',type=str,help="body to evaluate")
parser.add_argument('-path',default='model.h5',type=str,help="path for loading the model")
parser.add_argument('-threshold',default=0.0,type=float,help="threshold of prediction, 0.0~1.0")
opt=parser.parse_args()
def get_prediction(model,abs_path,in_dict):
cc2vid_input=in_dict
abs_vec=[0.0 for i in range(0,len(cc2vid_input))]
abs_count=0.0
with open(abs_path,'r',encoding='utf-8') as cf:
rd=csv.reader(cf)
for item in rd:
if item[0]=="Mention":
continue
try:
abs_vec[cc2vid_input[item[1]]]+=1.0
abs_count+=1.0
except:
pass
if not abs_count==0.0:
abs_vec=list(np.array(abs_vec)/abs_count)
pred=model.model.predict(np.array([abs_vec]))[0]
pred/=np.linalg.norm(pred)
return abs_vec,list(pred)
def get_prediction_author(model,abs_path,authors_path,in_dict,author_dict):
cc2vid_input=in_dict
fa2vid=author_dict
abs_vec=[0.0 for i in range(0,len(cc2vid_input))]
abs_count=0.0
with open(abs_path,'r',encoding='utf-8') as cf:
rd=csv.reader(cf)
for item in rd:
if item[0]=="Mention":
continue
try:
abs_vec[cc2vid_input[item[1]]]+=1.0
abs_count+=1.0
except:
pass
if not abs_count==0.0:
abs_vec=list(np.array(abs_vec)/abs_count)
author_vec=[0.0 for i in range(0,len(fa2vid))]
authors=util.load_sups(authors_path)
for author in authors:
try:
author_vec[fa2vid[author]]=1.0
except:
pass
sample_input=np.array([abs_vec+author_vec])
pred=model.model.predict([sample_input[:,:len(cc2vid_input)],sample_input[:,len(cc2vid_input):]])[0]
pred/=np.linalg.norm(pred)
return abs_vec,list(pred)
def print_vec(prediction,entity_dict,threshold=0.0):
for i,v in enumerate(prediction):
if v>threshold:
try:
print(entity_dict[i],end=',')
except:
pass
def eval_prediction(prediction,body_path,out_dict,threshold=0.0):
cc2vid_output=out_dict
body_vec=[0.0 for i in range(0,len(cc2vid_output))]
with open(body_path,'r',encoding='utf-8') as cf:
rd=csv.reader(cf)
for item in rd:
if item[0]=="Mention":
continue
try:
body_vec[cc2vid_output[item[1]]]=1.0
except:
pass
tp=0.0
fp=0.0
fn=0.0
for i in range(0,len(prediction)):
if body_vec[i]:
if prediction[i]>threshold:
tp+=1.0
else:
fn+=1.0
else:
if prediction[i]>threshold:
fp+=1.0
try:
P=tp/(tp+fp)
except:
P=0.0
try:
R=tp/(tp+fn)
except:
R=0.0
try:
F=2*P*R/(P+R)
except:
F=0.0
return P,R,F
def main():
in_path=opt.input
out_path=opt.output
entity_path=opt.vocab
in_dict=util.load_sups(in_path)
out_dict=util.load_sups(out_path)
entity_dict=util.load_sups(entity_path)
abs_path=opt.abstract
load_path=opt.path
threshold=opt.threshold
author=opt.A
evaluate=opt.E
if not author:
bownn_model=BOWNN()
bownn_model.load_model(load_path)
abs_vec,prediction=get_prediction(bownn_model,abs_path,in_dict)
else:
author_path=opt.author
author_dict=util.load_sups(author_path)
authors_path=opt.authors
bownn_model=BOWNN_author()
bownn_model.load_model(load_path)
abs_vec,prediction=get_prediction_author(bownn_model,abs_path,authors_path,in_dict,author_dict)
print("Entity mentions in this abstract:")
print_vec(abs_vec,entity_dict)
print("\n")
print("Entity mentions predicted:")
print_vec(prediction,entity_dict,threshold)
print("\n")
if evaluate:
body_path=opt.body
P,R,F=eval_prediction(prediction,body_path,out_dict,threshold)
print("Threshold: %.3f, Precision: %.3f, Recall: %.3f, F1: %.3f."%(threshold,P,R,F))
if __name__=='__main__':
main()