-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
240 lines (205 loc) · 7.35 KB
/
inference.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import signal
import sys
import json
import jsonrpclib
import makesets
import pickle
from random import randint
from train_local import get_k_eqs
from train_local import read_parse
from train_local import read_sets
from train_local import parse_inp
from functools import reduce
sys.path.insert(0, 'libsvm/python')
from svmutil import *
class StanfordNLP:
def __init__(self, port_number=8080):
self.server = jsonrpclib.Server("http://localhost:%d" % port_number)
def parse(self, text):
return json.loads(self.server.parse(text))
nlp = StanfordNLP()
def cleannum(n):
n = ''.join([x for x in n if x.isdigit() or x=='.' or x=='x' or x=='x*'])
return n
multi = None
glob = None
Lb = None
def make_eq(q,a,equations):
wps = q #open(q).readlines()
answs = a #open(a).readlines()
right = 0
wrong = 0
#print(len(wps))
for k in range(len(wps)):
#print(k)
answers = get_k_eqs(equations[k],g=True,a=True)
seeneq = []
seen = []
for x in answers:
if x[1] not in seeneq:
seen.append(x)
seeneq.append(x[1])
answers = seen
answers = list(set(answers))
#First preprocessing, tokenize slightly
problem = wps[k]#.lower()
problem = problem.strip().split(" ")
for i,x in enumerate(problem):
if len(x)==0:continue
if x[-1] in [',','.','?']:
problem[i] = x[:-1]+" "+x[-1]
problem = ' '.join(problem)
problem = " " + problem + " "
Cmp = int(equations[k])
#print(equations[k])
#print(problem)
if len(answers)==0:
print("0 Answers \n"+str(equations[k])+" INCORRECT")
if Cmp<=278:
wrong += 1
continue
#make story
story = read_parse(int(equations[k]))
#sets = makesets.makesets(story['sentences'])
sets = read_sets(int(equations[k]))
i = 0
for x in sets:
x[1].details()
xidx = [i for i,x in enumerate(sets) if x[1].num=='x']
if not xidx:
print(str(equations[k])+" INCORRECT NO X WHY")
if Cmp<=278:
wrong += 1
continue
xidx = xidx[0]
#print k
numlist = [(cleannum(v.num),v) for m,v in sets]
numlist = [x for x in numlist if x[0]!='']
allnumbs = {str(m):v for m,v in numlist}
objs = {m:(0,v) for m,v in numlist}
#print(objs.items())
consts = [x for x in answers[0][1].split(" ") if x not in ['(',')','+','-','/','*','=',]]
present = [x for x in consts if x in objs]
if consts!=present:
print(present,consts)
print(str(equations[k])+" INCORRECT missing thing")
if Cmp<=278:
wrong += 1
continue
if len([x for x in objs if x not in consts])>0:
print(str(equations[k])+" INCORRECT missing thing")
if Cmp<=278:
wrong += 1
continue
scores = []
for j,eq,cons,guess in answers:
consts = [x for x in eq.split(" ") if x not in ['(',')','+','-','/','*','=',]]
order = int(consts==[x[0] for x in numlist])
if order == 0: continue
#j = randint(0,len(answers)-1)
#eq = answers[j]
trips = []
#print(j,eq)
l,r = [x.strip().split(' ') for x in eq.split('=')]
consts = " ".join([x for x in answers[0][1].split(" ") if x not in ['(',')','+','-','/','*',]])
consts = consts.split(" = ")
sp = (objs[consts[0].split(" ")[-1]],objs[consts[1].split(" ")[0]])
target = 'x'
target = (target,objs[target])
#find innermost parens?
#print(eq)
sides = []
thisscore = []
for i,compound in enumerate([l,r]):
while len(compound)>1:
if "(" in compound:
rpidx = (len(compound) - 1) - compound[::-1].index('(')
lpidx = rpidx+compound[rpidx:].index(")")
subeq = compound[rpidx+1:lpidx]
substr = "("+''.join(subeq)+")"
compound = compound[:rpidx]+[substr]+compound[lpidx+1:]
else:
subeq = compound[0:3]
substr = "("+''.join(subeq)+")"
compound = [substr]+compound[3:]
p,op,e = subeq
p = objs[p]
e = objs[e]
op = op.strip()
pute = compute(p,op,e,target,problem,story,order,thisscore, cons)
objs[substr]=pute
if pute == -1:
exit()
score,c,vals = pute
thisscore.append(score)
#print(subeq,score)
sides.append(objs[compound[0]])
p = sides[0]; e = sides[1]
score = 1
for s in thisscore: score *= s
gscore = compute(p,'=',e,target,problem,story,order,score,cons)[0]
#print("gscore ",gscore)
#score has the Local Model Score
#gscore is the Global Model Score
Gb = 1.0 - Lb
proposed_score = (Lb* score+Gb* gscore)
#score *= gscore
scores.append((proposed_score,j,eq,guess))
scores = sorted(scores,reverse=True)
righties = [x for x in scores if x[1]==1]
#print(scores[:3])
if not righties:
if Cmp<=278:
wrong += 1
print("TOP SCORING NO CORRECT SOLUTION ,"+str(equations[k])+" INCORRECT")
continue
else:
corr = righties[0][3]
if len(scores)>0:
if scores[0][1]==1:
if Cmp<=278:
right += 1
#print k
#print equations[k]
print(str(equations[k])+" CORRECT")
else:
if Cmp<=278:
wrong += 1
print(str(equations[k])+" INCORRECT")
else:
if Cmp<=278:
wrong += 1
print(str(equations[k])+" INCORRECT")
return (right,wrong)
def compute(p,op,e,target,problem,story,order,score=None,cons=None):
if op == '=':
vec = [order,score,cons]
vec.extend(makesets.vector(p,e,problem,story,target))
op_label, op_acc, op_val = svm_predict([-1], [vec], glob ,'-q -b 1')
else:
vec = makesets.vector(p,e,problem,story,target)
op_label, op_acc, op_val = svm_predict([-1], [vec], multi ,'-q -b 1')
op_val=op_val[0]
if op == '+':
val = op_val[0]
if op == '-':
val = op_val[1]
if op == '*':
val = op_val[2]
if op == '/':
val = op_val[3]
if op == '=':
val = op_val[0]
c = makesets.combine(p[1],e[1],op)
return (val,c,op_val)
if __name__=="__main__":
inp, mfile, gfile = sys.argv[1:4]
Lb = 0.1 * float(sys.argv[4])
multi = svm_load_model(mfile)
glob = svm_load_model(gfile)
#q, a = sys.argv[1:3]
inp = sys.argv[1]
makesets.FOLD = sys.argv[1][-1]
q,a,e = parse_inp(inp)
right, wrong = make_eq(q,a,e)
print(right,wrong,right/len(q))