-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
55 lines (42 loc) · 1.5 KB
/
app.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
from flask import Flask
from flask import request
from flask import make_response
import json
import random
import AfterTheDeadlineHelper as ATD
app = Flask("apiai-grammarchecker")
reply_no_errors = ["There seems to be no problem with this!", \
"I think this is perfectly alright!", \
"I don't think there is any problem with what you have there!", \
"It's all correct!"]
@app.route("/")
def home():
return "Hello Heroku!"
@app.route("/webhook", methods=["POST"])
def webhook():
req = request.get_json() # removed str
wrong_sentence = getWrongSentence(req)
response = getResponse(wrong_sentence)
r = {"speech": response, "displayText": response}
r = make_response(json.dumps(r))
r.headers['Content-Type'] = 'application/json'
return r
def getWrongSentence(request):
result = request.get("result")
parameters = result.get("parameters")
wrong_sentence = parameters.get("wrong-sentence")
return wrong_sentence
def getResponse(wrong_sentence):
response = ATD.checkDocument(wrong_sentence)
if response.errorCount == 0:
return reply_no_errors[random.randint(0, len(reply_no_errors)-1)]
for e in range(response.errorCount):
urltext = response.getURLText(e)
if urltext is not None and urltext != 0:
return urltext
correct_rand = random.randint(0, response.errorCount-1)
reply = "There seems to be a problem with '"+response.getErrorString(correct_rand)+"'. "
reply += "Did you mean '"+("' or '".join(response.getSuggestions(correct_rand)))+"'?"
return reply
if __name__ == "__main__":
app.run()