-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathILPformat.py
141 lines (114 loc) · 3.55 KB
/
ILPformat.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
import signal
import json
import sys
import makesets
import jsonrpclib
from simplejson import loads
import pickle
from random import randint
import RevisedEntityFileCreator as EF
OUT=None
class StanfordNLP:
def __init__(self, port_number=8080):
#self.server = jsonrpclib.Server("http://localhost:%d" % port_number)
self.server = jsonrpclib.Server("http://localhost:8080")
def parse(self, text):
return loads(self.server.parse(text))
nlp = StanfordNLP()
def cleannum(n):
return ''.join([x for x in n if x.isdigit() or x=='.' or x=='x' or x=='x*'])
def make_eq(q,a,VERBOSE,TRAIN):
bigtexamples = {x:([],[]) for x in ["+","*",'/','-','=']}
#wps = open(q).readlines()
#answs = open(a).readlines()
#VERBOSE=True
wps = q
for k in range(len(wps)):
if VERBOSE:
for i in range(len(wps)):
print(i,wps[i])
k = int(input())
print(k)
problem = wps[k]
#First preprocessing, tokenize slightly
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 + " "
#Percentage putting
Res = problem
problem=""
for i in range(len(Res)):
if Res[i]=='%':
problem += " percent"
else:
problem += Res[i]
##Change Percentage to times
problem = problem.strip().split(" ")
for i in range(len(problem)-1):
if (problem[i+1]=='percent') or (problem[i+1]=='percentage') or (problem[i+1]=='Percentage') or (problem[i+1]=='Percent'): #
strval = problem[i]
val=''
if strval[0]=='$':
val=strval[1:]
val=float(val)/100.0
problem[i]=str(val)
elif strval[0] in ['0','1','2','3','4','5','6','7','8','9']:
val=strval
val=float(val)/100.0
problem[i]=str(val)
problem[i+1]="times"
problem = ' '.join(problem)
problem = " " + problem + " "
print(problem)
story = nlp.parse(problem)
sets = makesets.makesets(story['sentences']) # Imported Makesets
pickle.dump(sets, open('madesets/'+str(k)+'.pickle','wb'))
EF.main(sets,k,a[k],sys.argv[1])
sets = [x for x in sets if makesets.floatcheck(x[1].num) or x[1].num == 'x']
print(sets)
for z in sets:
z[1].details()
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 parse_json(inp):
q = []
a = []
with open(inp) as df:
data = json.load(df)
for i in data:
q.append(i["sQuestion"])
a.append(i["lSolutions"][0])
return (q,a)
if __name__=="__main__":
#q, a = sys.argv[1:3]
inp = sys.argv[1]
#q,a,e = parse_inp(inp)
q,a = parse_json(inp)
VERBOSE=False
TRAIN=False
'''
if len(sys.argv)>3:
if sys.argv[3]=='v':
VERBOSE=True
elif sys.argv[3]=='t':
TRAIN = True
OUT = sys.argv[4]
'''
make_eq(q,a,VERBOSE,TRAIN)