forked from MathProblems/August
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newinference.py
183 lines (160 loc) · 5.59 KB
/
newinference.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
import sys
sys.path.insert(0, '/Users/rikka/libsvm-3.18/python')
import svmutils
import makesets
import utils
multi = None
glob = None
def make_eq(q, a, equations):
wps = q # open(q).readlines()
right = 0
wrong = 0
for k in range(len(wps)):
answers = utils.get_k_eqs(equations[k], g=True, a=True)
if answers == []:
continue
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 = utils.preprocess_problem(wps[k])
print(problem)
#make story
#story = nlp.parse(problem)
story = utils.read_parse(int(equations[k]))
sets = makesets.makesets(story['sentences'])
i = 0
######
for x in sets:
x[1].details()
#continue
xidx = [i for i, x in enumerate(sets) if x[1].num == 'x']
if not xidx:
print("NO X WHY")
continue
#TODO look for 2 xes
xidx = xidx[0]
numlist = [(utils.cleannum(v.num), v) for k, v in sets]
numlist = [x for x in numlist if x[0] != '']
objs = {k: (0, v) for k, 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("missing thing")
continue
if len([x for x in objs if x not in consts]) > 0:
print("missing thing")
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]
#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(" = ")
target = 'x'
target = (target, objs[target])
#find innermost parens?
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]
compound += [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)
objs[substr] = pute
if pute == -1:
exit()
score, c, vals = pute
thisscore.append(score)
sides.append(objs[compound[0]])
p = sides[0]
e = sides[1]
score = 1
for s in thisscore:
score *= s
score *= compute(
p, '=', e, target, problem, story, order, score, cons
)[0]
scores.append((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:
wrong += 1
print("TOP SCORING NO CORRECT SOLUTION \nINCORRECT")
continue
if len(scores) > 0:
if scores[0][1] == 1:
right += 1
print("CORRECT")
else:
wrong += 1
print("INCORRECT")
else:
wrong += 1
print("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 = svmutils.svm_predict(
[-1], [vec], glob, '-q -b 1'
)
else:
vec = makesets.vector(p, e, problem, story, target)
op_label, op_acc, op_val = svmutils.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]
multi = svmutils.svm_load_model(mfile)
glob = svmutils.svm_load_model(gfile)
#q, a = sys.argv[1:3]
inp = sys.argv[1]
makesets.FOLD = sys.argv[1][-1]
q, a, e = utils.parse_inp(inp)
right, wrong = make_eq(q, a, e)
print(right, wrong, right / len(q))