-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
335 lines (299 loc) · 13.4 KB
/
helper.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize
import re
import fractions
def is_measure_word(word):
quantifier1 = ['bowl','dish','pound','piece','can','box','bag','carton','jar','loaf','slice','package','dash','cube','pack','head','ear','kernel','gain','grain','stalk','spear','clove']
quantifier2 = ['teaspoon','tablespoon','glass','cup','pint','quart','half gallon','gallon','tank','jug','bottle','keg','shot','drop']
quantifier3 = ['bar','tube','container','stick','roll','ball','spool','skein','yard','meter','foot','piece','pad','roll','stick','pair']
for q in quantifier1:
if re.search(q,word):
return True
for q in quantifier2:
if re.search(q,word):
return True
for q in quantifier3:
if re.search(q,word):
return True
return False
# print(is_measure_word('jars'))
def tokenize_ingredient(text):
ingredient = {}
tokenized = sent_tokenize(text)
quantity = ''
ingredient_name = ''
measurement = ''
preparation = ''
measure_word = False
ignore = False
for token in tokenized:
wordsList = nltk.word_tokenize(token)
tagged = nltk.pos_tag(wordsList)
for tag in tagged:
if tag[1] == 'CD' and not ignore:
quantity += tag[0]+' '
measure_word = True
elif tag[0]== '(':
m = re.search(r"\(([0-9]* [A-Za-z0-9_]+)\)", text)
if m==None:
continue
quantity += '(' +m.group(1)+') '
ignore = True
elif tag[0] == ')':
ignore = False
measure_word = True
elif (tag[1] == 'NN' or tag[1]=='NNS' or tag[1]=='VBZ') and not ignore:
word = tag[0]
# print(word)
if measure_word:
if is_measure_word(word):
measurement += word+' '
else:
ingredient_name += word+', '
measure_word = False
else:
ingredient_name += word+' '
elif tag[0]==',':
preparation = token.split(',')[1]
break
elif not ignore:
ingredient_name+=tag[0]+' '
# print(tagged)
ingredient['ingredient_name']=ingredient_name.lower()
ingredient['quantity'] = quantity
ingredient['measurement'] = measurement
ingredient['preparation'] = preparation
# print(ingredient)
return ingredient
def get_ingredient(word, ingredients):
bad_words = ['(',')','[',']','{','}']
for ingredient in ingredients:
if word in bad_words:
continue
# print('word',word,'ingredient',ingredient)
if re.search(word, ingredient):
return ingredient
return False
# print(get_ingredient('heat',['wheat']))
def get_cooking_method(word):
methods1 = ['[Ss]tand','[Ww]ait','[Bb]oil.*','[Ss]aut[eé]','[Bb]ak(e|ing)','[Ff]r(y|ied)','[Rr]oast', '[Gg]rill','[Ss]team','[Pp]oach','[Ss]immer','[Bb]roil','[Bb]lanch','brais(e|ing)','[Ss]tew']
methods2 =['[Re]move','[Bb]roil','[Bb]rais(e|ing)','[Cc]hop','[Dd]ic(e|ing)','[Mm]inc(e|ing)','[Mm]uddl(e|ing)','[Ss]i[nm]mer','[G]rat(e|ing)','[Ss]tir','[Ss]hak(e|ing)','[Cc]rush','[Ss]queez(e|ing)','[Cc]ook','[Rr]educ(e|ing)','[Dd]rain','[Mm]ix','[Tt]op','[Ss]prinkl(e|ing)','[Cc]ombin(e|ing)','[Ss]pread','[Ss]ear','[Bb]rown','[Cc]har','[Rr]ub','[Cc]hill','[Rr]inc(e|ing)','[Dd]ip','[Hh]eat','[Cc]over']
for method in methods1:
if re.search(method,word):
return True
for method in methods2:
if re.search(method,word):
return True
return False
result = get_cooking_method("chopped")
# print(result)
def get_cooking_tools(word):
tools1 = ['[Pp]ot','[Kk]nife','[Pp]an.?','[Kk]nives','[Gg]rater','[Bb]oard','[Oo]pener','[Cc]up.?','[Ss]poon.?','[Bb]owl.?','[Cc]olander.?','[Pp]eeler.?','[Mm]asher.?','[Ww]hisk.?','[Ss]pinner.?','[Gg]rater.?','[Ss]hear.?','[Jj]uicer','[Pp]ress','[Ss]teel','[Ss]harpener.?']
tools2 = ['[Ff]oil','skillet','plate.?','[Ss]patula.?','[Ss]poon.?','[Tt]ong.?','[Ll]adle.?','[Mm]itt.?','[Oo]ven','[Tt]rivet.?','[Gg]uard','[Tt]hermometer.?','[Bb]lender.?','[Ss]cale.?','[Cc]ontainer.?']
# print(tools[2])
# print(re.search(tools[4],'spoon'))
for tool in tools1:
if re.search(tool,word):
return True
for tool in tools2:
if re.search(tool,word):
return True
return False
import copy
def analyze_sentence(text, ingredients):
text = text.replace("Watch Now"," ")
stop_words = set(stopwords.words('english'))
tokenized = sent_tokenize(text)
steps = {}
methods = []
tools = []
ingredient = []
time = []
for i in tokenized:
wordsList = nltk.word_tokenize(i)
# removing stop words from wordList
wordsList = [w for w in wordsList if not w in stop_words]
# tagged = nltk.pos_tag(wordsList)
pre_word = ''
for word in wordsList:
if get_cooking_method(word):
methods.append(word.lower())
if get_cooking_tools(word):
tools.append(word.lower())
if get_ingredient(word, ingredients)!=False:
ingredient.append(get_ingredient(word, ingredients))
if word=='minute' or word == 'minutes':
time.append(pre_word+' '+word)
if word == 'hours' or word=='hour':
time.append(pre_word+' '+word)
pre_word = word
steps['ingredient'] = ingredient
steps['cooking tools'] = tools
steps['cooking methods'] = methods
steps['time'] = time
return steps
def refine_step_ingredients(step, ingredients):
ingredient_names = copy.deepcopy(ingredients)
step_ingredients = step['ingredient']
ingredients = []
for ingredient in step_ingredients:
if ingredient in ingredient_names:
ingredients.append(ingredient)
ingredient_names.remove(ingredient)
step['ingredient'] = ingredients
return step
def refine_step_methods(step):
methods1 = ['[Ss]tand','[Ww]ait','[Bb]oil.*','[Ss]aut[eé]','[Bb]ak(e|ing)','[Ff]r(y|ied)','[Rr]oast', '[Gg]rill','[Ss]team','[Pp]oach','[Ss]immer','[Bb]roil','[Bb]lanch','brais(e|ing)','[Ss]tew']
methods2 =['[Re]move','[Bb]roil','[Bb]rais(e|ing)','[Cc]hop','[Dd]ic(e|ing)','[Mm]inc(e|ing)','[Mm]uddl(e|ing)','[Ss]i[nm]mer','[G]rat(e|ing)','[Ss]tir','[Ss]hak(e|ing)','[Cc]rush','[Ss]queez(e|ing)','[Cc]ook','[Rr]educ(e|ing)','[Dd]rain','[Mm]ix','[Tt]op','[Ss]prinkl(e|ing)','[Cc]ombin(e|ing)','[Ss]pread','[Ss]ear','[Bb]rown','[Cc]har','[Rr]ub','[Cc]hill','[Rr]inc(e|ing)','[Dd]ip','[Hh]eat','[Cc]over']
step_methods = step['cooking methods']
methods = []
for method in step_methods:
for m in methods1:
flag = False
if re.search(m, method):
methods.append(method)
methods1.remove(m)
flag = True
break
if flag:
continue
for m in methods2:
if re.search(m, method):
methods.append(method)
methods2.remove(m)
break
step['cooking methods'] = methods
return step
def refine_step_tools(step):
tools1 = ['[Pp]ot','[Kk]nife','[Pp]an.?','[Kk]nives','[Gg]rater','[Bb]oard','[Oo]pener','[Cc]up.?','[Ss]poon.?','[Bb]owl.?','[Cc]olander.?','[Pp]eeler.?','[Mm]asher.?','[Ww]hisk.?','[Ss]pinner.?','[Gg]rater.?','[Ss]hear.?','[Jj]uicer','[Pp]ress','[Ss]teel','[Ss]harpener.?']
tools2 = ['[Ff]oil','skillet','plate.?','[Ss]patula.?','[Ss]poon.?','[Tt]ong.?','[Ll]adle.?','[Mm]itt.?','[Oo]ven','[Tt]rivet.?','[Gg]uard','[Tt]hermometer.?','[Bb]lender.?','[Ss]cale.?','[Cc]ontainer.?']
step_tools = step['cooking tools']
tools = []
for tool in step_tools:
for t in tools1:
flag = False
if re.search(t, tool):
tools.append(tool)
tools1.remove(t)
flag = True
break
if flag:
continue
for t in tools2:
if re.search(t, tool):
tools.append(tool)
tools2.remove(t)
break
step['cooking tools'] = tools
return step
def get_tools_and_methods(directions):
methods1 = ['[Ss]tand','[Ww]ait','[Bb]oil.*','[Ss]aut[eé]','[Bb]ak(e|ing)','[Ff]r(y|ied)','[Rr]oast', '[Gg]rill','[Ss]team','[Pp]oach','[Ss]immer','[Bb]roil','[Bb]lanch','brais(e|ing)','[Ss]tew']
methods2 =['[Re]move','[Bb]roil','[Bb]rais(e|ing)','[Cc]hop','[Dd]ic(e|ing)','[Mm]inc(e|ing)','[Mm]uddl(e|ing)','[Ss]i[nm]mer','[G]rat(e|ing)','[Ss]tir','[Ss]hak(e|ing)','[Cc]rush','[Ss]queez(e|ing)','[Cc]ook','[Rr]educ(e|ing)','[Dd]rain','[Mm]ix','[Tt]op','[Ss]prinkl(e|ing)','[Cc]ombin(e|ing)','[Ss]pread','[Ss]ear','[Bb]rown','[Cc]har','[Rr]ub','[Cc]hill','[Rr]inc(e|ing)','[Dd]ip','[Hh]eat','[Cc]over']
tools1 = ['[Pp]ot','[Kk]nife','[Pp]an.?','[Kk]nives','[Gg]rater','[Bb]oard','[Oo]pener','[Cc]up.?','[Ss]poon.?','[Bb]owl.?','[Cc]olander.?','[Pp]eeler.?','[Mm]asher.?','[Ww]hisk.?','[Ss]pinner.?','[Gg]rater.?','[Ss]hear.?','[Jj]uicer','[Pp]ress','[Ss]teel','[Ss]harpener.?']
tools2 = ['[Ff]oil','skillet','plate.?','[Ss]patula.?','[Ss]poon.?','[Tt]ong.?','[Ll]adle.?','[Mm]itt.?','[Oo]ven','[Tt]rivet.?','[Gg]uard','[Tt]hermometer.?','[Bb]lender.?','[Ss]cale.?','[Cc]ontainer.?']
methods = []
tools = []
stop_words = set(stopwords.words('english'))
for direction in directions:
tokenized = sent_tokenize(direction)
for i in tokenized:
wordsList = nltk.word_tokenize(i)
wordsList = [w for w in wordsList if not w in stop_words]
for word in wordsList:
for m in methods1:
flag = False
if re.search(m, word):
methods.append(word.lower())
methods1.remove(m)
flag = True
break
if flag:
continue
for m in methods2:
if re.search(m, word):
methods.append(word.lower())
methods2.remove(m)
break
for t in tools1:
flag = False
if re.search(t, word):
tools.append(word.lower())
tools1.remove(t)
flag = True
break
if flag:
continue
for t in tools2:
if re.search(t, word):
tools.append(word.lower())
tools2.remove(t)
break
methods = list(dict.fromkeys(methods))
tools = list(dict.fromkeys(tools))
return methods, tools
def get_steps(directions, ingredients):
steps = []
tools = []
methods = []
for direction in directions:
sentences = re.split(r"\.|;", direction)
for s in sentences:
if s.strip()=='Watch Now' or s.strip()=='':
continue
# print(s)
step = analyze_sentence(s,ingredients)
step = refine_step_ingredients(step,ingredients)
step = refine_step_methods(step)
step = refine_step_tools(step)
steps.append([s,step])
# print(step)
# print()
methods,tools = get_tools_and_methods(directions)
return tools, methods, steps
def double_ingredient_size(ingredients):
for ingredient in ingredients:
quantity = ingredient['quantity']
fraction_str = ''
keep_str = ''
interrupt=False
for c in quantity:
if (c=='(' or c=='[') and not interrupt:
interrupt = True
if not interrupt:
fraction_str += c
else:
keep_str += c
# print(quantity)
# print(fraction_str)
fraction_obj = sum(map(fractions.Fraction, fraction_str.split()))
# print(float(fraction_obj))
fraction_obj *= 2
# print(keep_str)
ingredient['quantity'] = str(fraction_obj ) + " "+ keep_str
# print("new quantity: ",ingredient['quantity'])
# print()
return ingredients
def cut_ingredient_size(ingredients):
for ingredient in ingredients:
quantity = ingredient['quantity']
fraction_str = ''
keep_str = ''
interrupt=False
for c in quantity:
if (c=='(' or c=='[') and not interrupt:
interrupt = True
if not interrupt:
fraction_str += c
else:
keep_str += c
# print(quantity)
# print(fraction_str)
fraction_obj = sum(map(fractions.Fraction, fraction_str.split()))
# print(float(fraction_obj))
fraction_obj /= 2
# print(keep_str)
ingredient['quantity'] = str(fraction_obj ) + " "+ keep_str
# print("new quantity: ",ingredient['quantity'])
# print()
return ingredients