-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_handler.py
101 lines (87 loc) · 3.48 KB
/
request_handler.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
import sys
sys.path.insert(0, './recipes/')
import Recipe
import Convert
import json
class request_handler:
def __init__(self,recipe):
self.recipe = recipe
def get_recipe(self):
return self.recipe
def handle_request(self,request):
intent = request.args.get('intent').encode('ascii','ignore')
print "handling a request of",intent
functions = {
'query_temp': self.query_temp,
'query_time': self.query_time,
'convert_unit': self.parse_unit_conversion,
'get_current_step': self.get_current_step,
'next_step': self.next_step,
'previous_step': self.previous_step,
'ingredients': self.ingredients,
'send_timer': self.send_time
}
return functions[intent](request)
def relevant_time(self,request):
pass
def query_temp(self,request):
return self.recipe.get_current_temp_text()
def query_time(self,request):
return self.recipe.get_current_time_text()
def get_current_step(self,request):
return self.recipe.get_current_step().get_step_text()
def next_step(self,request):
return self.recipe.next_step().get_step_text()
def ingredients(self, request):
return self.find_best_ingredient_amount(request.args.get('_text'))
def find_best_ingredient_amount(self,phrase):
words_in_phrase = phrase.split(" ")
ingredients = self.recipe.get_ingredients()
highest_matching = 0
best_match = None
for ingred in ingredients:
matches = 0
for i in ingred.get_raw().split(" "):
for word in words_in_phrase:
if(word.lower() == i.lower()):
matches += 1
if matches > highest_matching:
highest_matching = matches
best_match = ingred
return "You need " + best_match.get_amount_string() + " " + best_match.get_unit() + " of " + best_match.get_name()
def previous_step(self,request):
return self.recipe.previous_step().get_step_text()
def send_time(self, request):
raw_statement = request.args.get('_text')
words = raw_statement.split()
numbers = []
units = []
for i in range(len(words)):
try:
val = eval(words[i])
if type(val) == int or type(val) == float:
numbers.append(val)
units.append(words[i + 1])
except:
if words[i] == 'to' or words[i] == 'in':
units.append(words[i + 1])
continue
milliseconds = Convert.convert_unit(numbers[0], units[0], "milliseconds")
return "<" + str(milliseconds) + ">"
def parse_unit_conversion(self, request):
raw_statement = request.args.get('_text')
words = raw_statement.split()
numbers = []
units = []
for i in range(len(words)):
try:
val = eval(words[i])
if type(val) == int or type(val) == float:
numbers.append(val)
units.append(words[i + 1])
except:
if words[i] == 'to' or words[i] == 'in':
units.append(words[i + 1])
continue
final_string = str(numbers[0]) + " " + str(units[0]) + " is equal to " + str(Convert.convert_unit(numbers[0], units[0], units[1])) + " " + units[1]
return final_string