forked from kaushal2161/Mathaware-Q-A-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculation.py
204 lines (169 loc) · 6.79 KB
/
calculation.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
from flask import Flask
from flask import request
from flask import render_template
import json
from process_latex import process_sympy
from flask.json import jsonify
from sympy.core.sympify import sympify
from sympy import Number, NumberSymbol, Symbol
from getidentifiers import Getidentifiers
import getidentifiers
import os
import re
import requests
from ppp_datamodel import Sentence, Request, Response
import latexformlaidentifiers
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.sys.path.insert(0,parentdir)
os.environ['PPP_QUESTIONPARSING_GRAMMATICAL_CONFIG'] = 'ppp_questionparsing_grammatical/nlp_classical_config.json'
from ppp_datamodel.communication import Request
from ppp_questionparsing_grammatical import RequestHandler
from getformula import FormulaRequestHandler
from getformula import HindiRequestHandler
from latexformlaidentifiers import Formulacalculation, prepformula
def getlhsrhs(formula,ext):
"""
Break the formula into lhs and rhs
"""
global lhs
global rhs
lhs,rhs=formula.split(ext,1)
def makeidentifier(symbol,values):
"""
Make dictionary for Values of Symbol
"""
symvalue={}
for i in symbol:
for j in values:
if i==Symbol(j):
value=values[j]
symvalue[i]=value
return symvalue
def makeresponse(formul):
"""
Make response for the API
"""
try:
reques=Formulacalculation(formul)
global identifiers
identifiers=reques.answer()
if identifiers:
listidentifiers=list(identifiers)
newlist= []
for item in listidentifiers:
newlist.append(str(item))
newlist.append(dict(formula=formul))
resp=json.dumps(newlist)
return resp
else:
return "#"+ str(formul)
except Exception as e : return ("System is not able to understand the formula")
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template("index.html")
@app.route('/getresponse', methods=['POST'])
def my_form_post():
"""
Get formula from the user input and process it
Return response
"""
try:
if request.form['formula']:
global formula
formula= request.form['formula']
#req=Formulacalculation(formula)
#listofsybol=req.answer()
#print(formula.replace('/\\\\/g',''))
global processedformula
processedformula=latexformlaidentifiers.prepformula(formula)
#print(processedformula)
if formula is not None:
return makeresponse(processedformula)
else:
return ("System is not able to find the result.")
except Exception as e : return ("System is not able to understand the formula")
@app.route('/getengformula', methods=['POST'])
def get_formula():
"""
Get English question from the user parse it to Questionparsing module to get Triple
Parse Triple (Subject, predicate, ?) to FormulaRequestHandler to get Formula from Wikidata
Return response
"""
try:
question=request.form['formula']
meas = {'accuracy': 0.5, 'relevance': 0.5}
q = RequestHandler(Request(language="en",id=1,tree=Sentence(question),measures=meas))
query = q.answer()
reques= FormulaRequestHandler(query)
global formula
formula=reques.answer()
#print(formula)
global processedformula
processedformula=latexformlaidentifiers.prepformula(formula)
print(formula)
if(formula):
return makeresponse(processedformula)
else:
return ("System is not able to find the result.")
except Exception : return ("System is not able to find the result.")
@app.route('/gethindiformula', methods=['POST'])
def get_hindiformula():
"""
Get Hindi question from the user and apply regex to get subject and predicate
Parse subject and predicate to HindiRequestHandler to get formula
Return response
"""
try:
question=request.form['formula']
matchObj = re.match( r'(.*)की (.*?) .*', question, re.M|re.I)
matchObj1 = re.match( r'(.*)के लिए (.*?) क्या है *', question, re.M|re.I)
matchObj2 = re.match( r'(.*)और (.*?) के बीच *', question, re.M|re.I)
if matchObj:
subject=matchObj.group(1)
predicate=matchObj.group(2)
if matchObj1:
subject=matchObj1.group(1)
predicate=matchObj1.group(2)
if matchObj2:
subject=matchObj2.group(1)
predicate=matchObj2.group(2)
reques = HindiRequestHandler("hi",subject,predicate)
global formula
formula=reques.answer()
global processedformula
processedformula=latexformlaidentifiers.prepformula(formula)
if formula is not None:
print(formula)
return makeresponse(processedformula)
else:
return ("System is not able to find the result.")
except Exception : return ("System is not able to find the result.")
@app.route('/getfinalresult', methods=['POST'])
def my_form_json():
"""
Get Values for the identifiers
Return Calculated result
"""
try:
identifiers1 = request.data.decode('utf-8')
json1=json.loads(identifiers1)
seprator= getidentifiers.formuladivision(formula)
if seprator is not None:
lhsrhs= getlhsrhs(processedformula,seprator)
f=process_sympy(rhs)
f1=process_sympy(lhs)
latexlhs=sympify(f1)
l=sympify(f)
#print(l)
symbolvalue=makeidentifier(identifiers,json1)
value=l.evalf(subs=symbolvalue)
return ("%s %s %.2e" % (latexlhs,seprator,value))
else:
l=sympify(formula)
value=l.evalf(subs=json1)
return ("%.2e" % value)
except Exception : return ("System is not able to calculate the result.")
if __name__ == '__main__':
app.run(debug=True)
#get_formula()