-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_local_LR.py
199 lines (165 loc) · 6.13 KB
/
train_local_LR.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
import sys
import makesets
import pickle
from random import randint
def read_parse(k):
return pickle.load(open('s_data/'+str(int(k))+'.pickle', 'rb'))
def read_sets(k):
return pickle.load(open('madesets/'+str(int(k))+'.pickle','rb'))
def cleannum(n):
n = ''.join([x for x in n if x.isdigit() or x=='.' or x=='x' or x=='x*'])
return n
def training(trips,problem,story,target):
#this function take the trips and creates positive and negative training instances from them
texamples = {x:([],[]) for x in ["+","*",'/','-','=']}
for op,a,b in trips:
if op == '=':
vec = makesets.eqvector(a,b,problem,story,target)
else:
vec = makesets.vector(a,b,problem,story,target)
texamples[op][0].append(vec)
return texamples
def make_eq(q,a,equations):
bigtexamples = {x:([],[]) for x in ["+","*",'/','-','=']}
wps = q #open(q).readlines()
answs = a #open(a).readlines()
for k in range(len(wps)):
#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 + " "
print k
print problem
#story = nlp.parse(problem)
story = read_parse(int(equations[k]))
eqs = get_k_eqs(equations[k])
answers = [x[1] for x in eqs if x[0]==1]
if answers == []: continue
answers = list(set(answers))
print answers
#make story
#story = nlp.parse(problem)
#sets = makesets.makesets(story['sentences'])
sets = read_sets(equations[k])
i = 0
xidx = [i for i,x in enumerate(sets) if x[1].num=='x']
if not xidx:
print("NO X WHY");continue
numlist = [(cleannum(v.num),v) for k,v in sets]
numlist = [x for x in numlist if x[0]!='']
allnumbs = {str(k):v for k,v in numlist}
objs = {k:(0,v) for k,v in numlist}
#print objs.items()
consts = [x for x in answers[0].split(" ") if x not in ['(',')','+','-','/','*','=',]]
present = [x for x in consts if x in objs]
if present!=consts:
print(present,consts);print("missing thing");#continue
continue
oanswers = []
for eq 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
else: oanswers.append(eq)
if oanswers == []: continue
answers = oanswers
print answers
simpleanswers = [x for x in answers if x.split(" ")[-2]=="="]
if simpleanswers:
answers = simpleanswers
else: answers = [answers[randint(0,len(answers)-1)]]
print answers
#simpleanswers = []
for j,eq in enumerate(answers):
trips = []
print(j,eq)
l,r = [x.strip().split(' ') for x in eq.split('=')]
target = 'x'
target = (target,objs[target])
#find innermost parens?
sides = []
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:]
if True:
p,op,e = subeq
p = objs[p]
e = objs[e]
op = op.strip()
trips.append((op,p,e))
pute = (0,makesets.combine(p[1],e[1],op))
objs[substr]=pute
if pute == -1:
exit()
t = training(trips,problem,story,target)
for op in t:
bigtexamples[op][0].extend(t[op][0])
bigtexamples[op][1].extend(t[op][1])
pickle.dump(bigtexamples,open('data/'+sys.argv[1][-1]+".local.training",'wb'))
def parse_inp(inp):
q=[]
a=[]
e=[]
with open(inp) as f:
f = f.readlines()
i=0
while i<len(f):
q.append(f[i])
i+=1
e.append(f[i])
i+=1
a.append(f[i])
i+=1
return (q,a,e)
def get_k_eqs(i,k=100,g=False,a=False):
digit = "{0:0=3d}".format(int(i))
exprs = []
with open(eqsdir+"/q"+digit+".txt.out") as f:
f = f.readlines()[3:-1] # Discard the first three lines from the file
j = 0
while j<k:
if j>=len(f): break
line = f[j]
line = line.split(" | ")
good = line[0].split(": ")[1] #Discard the EXPR and Takes the value after that 0/1
exp = line[6] # Takes the INFIX equation
for s in ['(',')','+','-','*','/','=']:
exp = exp.replace(s,' '+s+' ')
exp = exp.replace(' ',' ').strip()
if g:
cons = int(line[3])
if cons == 0:
cons = 1
else:
cons = 1/(cons+1)
if a:
answ = line[5]
exprs.append((int(good),exp,cons,answ))
else:
exprs.append((int(good),exp,cons))
else:
exprs.append((int(good),exp))
j+=1
return exprs
eqsdir = "ILP.out"
if __name__=="__main__":
#q, a = sys.argv[1:3]
inp = sys.argv[1]
#eqsdir = sys.argv[2]
makesets.FOLD = sys.argv[1][-1]
q,a,e = parse_inp(inp)
make_eq(q,a,e)