forked from denizsagnaklar/RecipeParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_ingredients.py
288 lines (246 loc) · 9.56 KB
/
parse_ingredients.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
from scraper import scrapeRecipe
import re
from pattern.en import pluralize
# using for testing because allrecipes got mad
ings = [u'6 egg yolks', u'3/4 cup white sugar', u'2/3 cup milk',
u'1 1/4 cups heavy cream', u'1/2 teaspoon vanilla extract', u'1 pound mascarpone cheese',
u'1/4 cup strong brewed coffee, room temperature', u'2 tablespoons rum',
u'2 (3 ounce) packages ladyfinger cookies', u'1 tablespoon unsweetened cocoa powder',
u'2 cups rose water chilled', u'1 cup shiitake chopped']
ingredient_book = {}
fraction_match = r"(\d+[\/\d. ]*|\d)" # /g means global match
measurements = [r'([a-z]+)spoons?', r'cloves?', r'cups?', r'pounds?', r'ounces?', r'large',
r'medium', r'small', r'packs?', r'pints?', r'quarts?', r'gallons?', r'bushel', r'grams?']
def frac_to_float(frac_str):
try:
return float(frac_str)
except ValueError:
num, denom = frac_str.split('/')
try:
leading, num = num.split(' ')
whole = float(leading)
except ValueError:
whole = 0
frac = float(num) / float(denom)
return whole - frac if whole < 0 else whole + frac
def unique_list(l):
ulist = []
[ulist.append(x) for x in l if x not in ulist]
return ulist
'''
ex. get_all_names(recipe_ingredient_list_from_scraper, ingredient_book) -->
['egg yolk', 'white sugar', 'milk', 'cream', 'vanilla extract', 'mascarpone cheese', 'coffee', 'rum', 'ladyfinger cookies', 'cocoa']
'''
def get_all_names(ings, ingredient_book):
#scrape
# recipe = scrapeRecipe(url)
# ings = recipe[0]
ings = map(lambda x:x.lower(), ings)
# get bbc ingredients
# all_i = open("new_ing.txt", "r")
# all_i = all_i.read()
all_ingredients = ingredient_book.keys()
# lst = all_i.split(',')
# all_ingredients = []
# for i in lst:
# i = i.replace('u\'', '').replace('\'', '').strip()
# all_ingredients.append(i)
#get names
t = []
rejected = []
for true_ing in all_ingredients: # real ings from bbc
ing_regex_single = r'\b{0}\b'.format(true_ing)
ing_regex_plural = r'\b{0}\b'.format(pluralize(true_ing))
match_single = re.search(ing_regex_single, true_ing.lower())
match_plural = re.search(ing_regex_plural, true_ing.lower())
if match_single:
t.append(true_ing.lower())
elif match_plural:
t.append(pluralize(true_ing.lower()))
seen = set()
seen_add = seen.add
t = [x for x in t if not (x in seen or seen_add(x))]
t.sort(key=lambda x: len(x.split()), reverse=True)
names = []
desc_and_prep = []
for i in ings:
for db_ingredient in t:
if db_ingredient in i.lower():
names.append(db_ingredient)
# db_ingredient is now the name of the Ingredient
desc_and_prep.append(i.split(db_ingredient))
break
else:
rejected.append(i)
return [names, desc_and_prep, rejected] #[ ['white sugar'], [[u'1 cup', u'']]]
def get_ing_quantity(ing_front):
ingredient_split_list = ing_front.split()
amount = 0
for num in ingredient_split_list:
frac_match = re.search(fraction_match, num)
if frac_match:
amount += frac_to_float(frac_match.group(0))
return amount
def get_ing_measurement(ing_front):
ingredient_split_list = ing_front.split()
measurement = ""
no_nums_ingredient = [w for w in ingredient_split_list if not re.search(fraction_match, w)]
if(len(no_nums_ingredient) > 0):
i = no_nums_ingredient[0]
measurement = i if re.search(r"(?=("+'|'.join(measurements)+r"))", i) else ""
regex = re.compile(r'[^a-zA-Z]')
regex.sub('', measurement)
return measurement.lower()
def get_ing_descriptor(ing_front):
ingredient_split_list = ing_front.split()
descriptor = ""
no_nums_ingredient = [w for w in ingredient_split_list if not re.search(fraction_match, w)]
no_nums_ingredient = map(lambda x:x.lower(), no_nums_ingredient)
m = get_ing_measurement(ing_front).lower()
descriptor = ' '.join(no_nums_ingredient).replace(m, '').strip()
return descriptor.strip()
def get_ing_preparation(ing_back):
return ing_back.strip()
'''
input: allrecipes url
output: prints -->
N: parmesan cheese
Q: 0.25
M: cup
D: grated
P:
"
'''
def print_ingredients(ings, ingredient_book):
# recipe = scrapeRecipe(url)
# ings = recipe[0]
all_results = get_all_names_plus_fixed_rejects(ings, ingredient_book)
names = all_results[0]
desc_and_preps = all_results[1]
for x in range(len(names)):
name = names[x]
print "Name:", name
qty = get_ing_quantity(desc_and_preps[x][0])
print "Quantity:", qty
msrmnt = get_ing_measurement(desc_and_preps[x][0])
print "Measurement:", msrmnt
desc = get_ing_descriptor(desc_and_preps[x][0])
print "Descriptor:", desc
if len(desc_and_preps[x]) > 1:
prep = get_ing_preparation(desc_and_preps[x][1])
print "Preparation:", prep
print '\n'
'''
input: ings (recipe ings), ingredient_book(full ing dict)
output:
{'olive oil': {'preparation': u'', 'descriptor': u'', 'measurement': u'tablespoon', 'name': 'olive oil', 'quantity': 1.0}, 'parmesan cheese': {'preparation': u'', 'descriptor'
: u'grated', 'measurement': u'cup', 'name': 'parmesan cheese', 'quantity': 0.25}, etc.}
use: ex. quantity of parmesan in : p["parmesan cheese"]["quantity"] --> 0.25
'''
def parse_ingredients(ings, ingredient_book):
# recipe = scrapeRecipe(url)
# ings = recipe[0]
all_results = get_all_names(ings, ingredient_book)
names = all_results[0]
desc_and_preps = all_results[1]
all_parsed_ings = {}
for x in range(len(names)):
parsed = {"name":"", "quantity":"", "measurement":"", "descriptor":"", "preparation":""}
name = names[x]
parsed["name"] = name
qty = get_ing_quantity(desc_and_preps[x][0])
parsed["quantity"] = qty
msrmnt = get_ing_measurement(desc_and_preps[x][0])
parsed["measurement"] = msrmnt
desc = get_ing_descriptor(desc_and_preps[x][0])
parsed["descriptor"] = desc
if len(desc_and_preps[x]) > 1:
prep = get_ing_preparation(desc_and_preps[x][1])
parsed["preparation"] = prep
all_parsed_ings[name] = parsed
# print all_parsed_ings
return all_parsed_ings
def fix_rejects(rejects, ingredient_book):
fixed = []
for r in rejects:
r_split = r.split()
# r_split = [w for w in r_split if not re.search(fraction_match, w)] # take out nums
# print r_split
# get bbc ingredients
# all_i = open("new_ing.txt", "r")
# all_i = all_i.read()
all_ingredients = ingredient_book.keys()
# lst = all_i.split(',')
# all_ingredients = []
# for i in lst:
# i = i.replace('u\'', '').replace('\'', '').strip()
# all_ingredients.append(i)
t = []
for true_ing in all_ingredients: # real ings from bbc
ing_regex_single = r'\b{0}\b'.format(true_ing)
ing_regex_plural = r'\b{0}\b'.format(pluralize(true_ing))
match_single = re.search(ing_regex_single, true_ing.lower())
match_plural = re.search(ing_regex_plural, true_ing.lower())
if match_single:
t.append(true_ing.lower())
elif match_plural:
t.append(pluralize(true_ing.lower()))
full_name = []
for s in r_split: # word in rejected ingredient
match_flag = False
for i in t: # i in ing_book_keys
s_regex_single = r'\b{0}\b'.format(s)
s_regex_plural = r'\b{0}\b'.format(pluralize(s))
match_single = re.search(s_regex_single, i.lower())
match_plural = re.search(s_regex_plural, i.lower())
if match_single:
match_flag = True
full_name.append(i)
# fixed.append(i)
elif match_plural:
match_flag = True
full_name.append(i)
# fixed.append(i)
else:
if s not in full_name:
full_name.append(s)
if match_flag:
break
f = ' '.join(full_name)
full_name_result = ' '.join(unique_list(f.split()))
fixed.append(full_name_result)
return fixed
'''
pretty much exactly like get_all_names, except this allows us
to include ingredients that we don't have in our db
returns:
index 0 --> list of ingredient names
1 --> desc_and_prep
2 --> rejected stuff
3 --> merged list of ingredients including rejects
'''
def get_all_names_plus_fixed_rejects(ings, ingredient_book):
a = get_all_names(ings, ingredient_book)
b = fix_rejects(a[2], ingredient_book)
merge_list = ings + b
c = get_all_names(merge_list, ingredient_book)
c.append(merge_list)
return c
#
# # print d[0], '\n'
# # print d[1], '\n'
# # print d[3]
#
# e = parse_ingredients(d[3], ingredient_book)
# print e["rose wine"]
def print_parsed_ingredients(ings, ingredient_book):
parsed = []
ing_dict = parse_ingredients(get_all_names_plus_fixed_rejects(ings, ingredient_book)[3], ingredient_book)
for k in ing_dict.keys():
b = "{0} {1} {2} {3} {4}\n".format(ing_dict[k]["quantity"], ing_dict[k]["measurement"], ing_dict[k]["descriptor"], ing_dict[k]["name"], ing_dict[k]["preparation"])
c = ' '.join(b.split())
parsed.append(c)
# for i in parsed:
# print i
return parsed
# print_parsed_ingredients(d[3], ingredient_book)