-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeWriter.py
More file actions
487 lines (393 loc) · 17.9 KB
/
RecipeWriter.py
File metadata and controls
487 lines (393 loc) · 17.9 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# -*- coding: utf-8 -*-
#
# MIT License
#
# Copyright (c) 2020 Mike Simms
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import CrawlerDatabase
import argparse
import collections
import json
import re
ID_KEY = '_id'
STYLE_KEY = 'style'
YIELD_SIZE_KEY = 'yield size'
GRAINS_KEY = 'grains'
HOPS_KEY = 'hops'
YEASTS_KEY = 'yeasts'
FERMENTABLES_KEY = 'Fermentable'
AMOUNT_KEY = 'Amount'
VARIETY_KEY = 'Variety'
BOIL_KEY = 'Boil'
COMMMON_HOPS = ['amarillo', 'cascade', 'centennial', 'citra', 'chinook', 'columbus', 'equinox', 'fuggles', 'kent goldings', 'golding', 'magnum', 'mosaic', 'victory', 'warrior']
UNITS = ['lb', 'oz', 'g', 'tsp', 'tbsp', 'pkg', 'ounce', 'teaspoon', 'cup', 'pound', 'gal']
SEARCH_LIST_FUNC = lambda x,y : x.find(y) >= 0
class RecipeWriter(object):
"""Reads beer recipes from the database and generates a new recipe."""
def __init__(self):
super(RecipeWriter, self).__init__()
def capitalize(self, input):
"""Utility function for capitalizing the first letter in a string."""
input_parts = list(input)
input_parts[0] = input_parts[0].upper()
return "".join(input_parts)
def remove_letters(self, input):
"""Utility function for removing all letters from a string."""
new = ""
for letter in input:
if not(letter.isalpha()):
new += letter
return new
def normalize_grain_name(self, grain_name):
"""Tries to cleanup the various names that people use for the same grain."""
pattern = re.compile("Caramel.*/.*Crystal", re.IGNORECASE)
grain_name = pattern.sub("Crystal", grain_name)
pattern = re.compile("American 2-row", re.IGNORECASE)
grain_name = pattern.sub("Pale 2-Row", grain_name)
pattern = re.compile("Pale Ale 2-Row", re.IGNORECASE)
grain_name = pattern.sub("Pale 2-Row", grain_name)
pattern = re.compile("2-row", re.IGNORECASE)
grain_name = pattern.sub("2-Row", grain_name)
pattern = re.compile("Barley, Flaked", re.IGNORECASE)
grain_name = pattern.sub("Flaked Barley", grain_name)
pattern = re.compile("Oats, Flaked", re.IGNORECASE)
grain_name = pattern.sub("Flaked Oats", grain_name)
pattern = re.compile("Crystal.*-", re.IGNORECASE)
grain_name = pattern.sub("Crystal", grain_name)
pattern = re.compile("Caramel", re.IGNORECASE)
grain_name = pattern.sub("Crystal", grain_name)
pattern = re.compile("Cara-Pils/Dextrine", re.IGNORECASE)
grain_name = pattern.sub("Carapils", grain_name)
pattern = re.compile("Cara-Pils", re.IGNORECASE)
grain_name = pattern.sub("Carapils", grain_name)
norm_name = ""
parts = grain_name.split(' ')
for part in parts:
part_lower = part.lower()
if len(part_lower) == 0:
continue
if part_lower[0] in ['(', '[']:
break
# The world 'malt' is redundant so ignore it.
if part_lower != 'malt':
# Make sure the first letter is capitalized.
part = self.capitalize(part)
norm_name += part
norm_name += " "
norm_name = norm_name.strip()
if norm_name == "Pale":
norm_name = "Pale 2-Row"
return norm_name
def to_number(self, num_str):
"""Utility method for string to number conversion and cleanup."""
new_str = self.remove_letters(num_str)
if new_str == "1--1/2": # Filty hack to handle an annoying case
return float(1.5)
fraction_parts = new_str.split('/')
if len(fraction_parts) == 1:
return float(new_str)
return float(fraction_parts[0]) / float(fraction_parts[1])
def normalize_amount_str(self, amount, scale):
"""Tries to cleanup the various ways people express amounts."""
parts = amount.split(' ')
if len(parts) < 2: # Sanity check
return amount
if any([SEARCH_LIST_FUNC(amount.lower(), i) for i in ['lb', 'pound']]):
parts[0] = str(self.to_number(parts[0]) * scale)
parts[1] = "lbs"
if any([SEARCH_LIST_FUNC(amount.lower(), i) for i in ['kg', 'kilogram']]):
parts[0] = str(self.to_number(parts[0]) * scale * 2.2)
parts[1] = "lbs"
if any([SEARCH_LIST_FUNC(amount.lower(), i) for i in ['oz', 'ounce']]):
parts[0] = str(self.to_number(parts[0]) * scale * 0.0625)
parts[1] = "lbs"
if any([SEARCH_LIST_FUNC(amount.lower(), i) for i in ['gal', 'gallon', 'us gallon']]):
parts[0] = str(self.to_number(parts[0]) * scale)
parts[1] = "gallons"
if any([SEARCH_LIST_FUNC(amount.lower(), i) for i in ['liter', 'litre']]):
parts[0] = str(self.to_number(parts[0]) * scale * 0.264172)
parts[1] = "gallons"
amount = parts[0] + " " + parts[1]
return amount
def normalize_grains_and_hops(self, grains, hops, scale):
"""Since the recipes were collected from multiple sites, this attempts to normalize their structure."""
"""Some recipe writers put the grains and the hops together, so we have to deal with that."""
if grains is None:
return grains, hops
new_grains = []
new_hops = []
ignore_strs = ['[', '#', 'dme', 'extract', 'gypsum', 'honey', 'moss', 'sugar', 'syrup', 'total', 'water', 'whirlfloc', 'yeast']
# Normalize grains, some websites lump the hops in with the grains for whatever reason.
for grain in grains:
# Was this formatted in a nice is python dictionary for us?
if isinstance(grain, dict):
# Do we have a dictionary with valid items?
if AMOUNT_KEY in grain and FERMENTABLES_KEY in grain:
amount = grain[AMOUNT_KEY]
fermentables = grain[FERMENTABLES_KEY]
# Cleanup the name. It might have the amount appended to it.
offset = fermentables.find(amount)
if offset > 0:
grain[FERMENTABLES_KEY] = fermentables[offset + len(amount):].strip()
# Does the string contain junk?
matched_ignore_strs = [SEARCH_LIST_FUNC(grain[FERMENTABLES_KEY].lower(), i) for i in ignore_strs]
if any(matched_ignore_strs):
continue
# Normalize the amount.
amount = self.normalize_amount_str(amount, scale)
grain[AMOUNT_KEY] = amount
# Normalize the grain name.
norm_name = self.normalize_grain_name(grain[FERMENTABLES_KEY])
if len(norm_name) > 1:
grain[FERMENTABLES_KEY] = norm_name
new_grains.append(grain)
else:
# Does the string contain junk?
matched_ignore_strs = [SEARCH_LIST_FUNC(grain.lower(), i) for i in ignore_strs]
if any(matched_ignore_strs):
continue
# These are things we might find in the string.
amount = ""
desc = ""
boil = ""
# Should start with an amount.
parts = grain.split(' ')
amount = parts[0]
del parts[0]
# Sanity check.
if len(parts) == 0:
continue
# Is the second part the units?
matched_units = [SEARCH_LIST_FUNC(parts[0].lower(), i) for i in UNITS]
if any(matched_units):
amount += " "
amount += parts[0]
amount = self.normalize_amount_str(amount, scale)
del parts[0]
if '-' in parts:
parts.remove('-')
# Sanity check.
if len(parts) == 0:
continue
# Build the description string and figure out if it is grains or hops
# because some websites lump them together.
is_grain = True
is_hop = False
in_boil = False
for part in parts:
part_lower = part.lower()
# Sanity check.
if len(part_lower) == 0:
continue
# Did someone put hops in the grains category?
if part_lower in COMMMON_HOPS:
is_grain = False
is_hop = True
elif is_hop:
in_boil = in_boil or part_lower.find('at') >= 0 or part_lower.find('boil')
if in_boil and part_lower.find('hop') >= 0:
boil += part
# Fairly sure this is part of the grains.
if not in_boil:
desc += part
desc += " "
# Cleanup whatever we extracted for name of the grain.
desc = self.normalize_grain_name(desc)
# Did we find grains?
if is_grain and len(desc) > 0:
grain = {}
grain[FERMENTABLES_KEY] = desc
if len(amount) > 0:
grain[AMOUNT_KEY] = amount
new_grains.append(grain)
# Did we find any hops?
elif is_hop and len(desc) > 0:
hops = {}
hops[VARIETY_KEY] = desc.strip()
if len(amount) > 0:
hops[AMOUNT_KEY] = amount
if len(boil) > 0:
hops[BOIL_KEY] = boil
new_hops.append(hops)
# Normalize hops.
for hop in hops:
if isinstance(hop, dict):
new_hops.append(hop)
return new_grains, new_hops
def compute_avg_amount(self, grains, grain_name):
"""Looks through the (normalized) grains list for the specified grain and compute the average amount used."""
avg_amount = 0.0
count = 0.0
for grain in grains:
if grain[FERMENTABLES_KEY] == grain_name:
if AMOUNT_KEY in grain:
amount_str_parts = grain[AMOUNT_KEY].split(' ')
temp_amount = float(amount_str_parts[0])
avg_amount = avg_amount + temp_amount
count = count + 1.0
return avg_amount / count
def generate_avg_recipe(self, db, style, desired_yield):
"""Looks through the database of crawled web pages, gets all beer recipes of the given style, normalizes the amounts"""
"""and writes a recipe using the most popular grains and hops and the avg amount in which they appear."""
all_pages = db.retrieve_all_pages()
lower_style = style.lower()
grains = []
hops = []
yeasts = []
# The recipes were collected from various sites and will need normalizing
# after we filter for the recipes that match the search criteria.
for page in all_pages:
# Filter for the desired beer style.
if STYLE_KEY in page and page[STYLE_KEY].lower().find(lower_style) >= 0:
grain_value = None
hops_value = None
yield_size = None
scale = None # Amount to scale the recipe by; will try to nomalize on a 3 gallon yield
if GRAINS_KEY in page:
grain_value = page[GRAINS_KEY]
if HOPS_KEY in page:
hops_value = page[HOPS_KEY]
if YIELD_SIZE_KEY in page:
yield_size = self.normalize_amount_str(page[YIELD_SIZE_KEY], 1.0)
scale = desired_yield / float(yield_size.split(' ')[0])
norm_grains, norm_hops = self.normalize_grains_and_hops(grain_value, hops_value, scale)
grains.extend(norm_grains)
hops.extend(norm_hops)
yeasts.extend(page[YEASTS_KEY])
#
# Print the normalized inputs.
#
print("------")
print("Grains")
print("------")
fermentable_names = []
for grain in grains:
fermentable_names.append(grain[FERMENTABLES_KEY])
counted_fermentables = collections.Counter(fermentable_names).most_common()
for fermentable in counted_fermentables:
print(fermentable)
print("----")
print("Hops")
print("----")
hop_names = []
for hop in hops:
hop_names.append(hop[VARIETY_KEY])
counted_hop_names = collections.Counter(hop_names).most_common()
for hop_name in counted_hop_names:
print(hop_name)
print("------")
print("Yeasts")
print("------")
counted_yeasts = collections.Counter(yeasts).most_common()
for yeast in counted_yeasts:
print(yeast)
#
# Do we have enough to write the recipe?
#
if len(counted_fermentables) < 3:
print("ERROR: Not enough data to write a recipe.")
if len(counted_hop_names) < 3:
print("ERROR: Not enough data to write a recipe.")
if len(counted_yeasts) < 1:
print("ERROR: Not enough data to write a recipe.")
#
# Write the recipe
#
print("------")
print("Recipe")
print("------")
# Grains
grain_name = counted_fermentables[0][0]
grain_amount = self.compute_avg_amount(grains, grain_name)
print(grain_name + ", {:.2f}".format(grain_amount) + " lbs")
grain_name = counted_fermentables[1][0]
grain_amount = self.compute_avg_amount(grains, grain_name)
print(grain_name + ", {:.2f}".format(grain_amount) + " lbs")
grain_name = counted_fermentables[2][0]
grain_amount = self.compute_avg_amount(grains, grain_name)
print(grain_name + ", {:.2f}".format(grain_amount) + " lbs")
grain_name = counted_fermentables[3][0]
grain_amount = self.compute_avg_amount(grains, grain_name)
print(grain_name + ", {:.2f}".format(grain_amount) + " lbs")
# Hops
print(counted_hop_names[0][0] + " Hops")
print(counted_hop_names[1][0] + " Hops")
print(counted_hop_names[2][0] + " Hops")
# Yeast
print(counted_yeasts[0][0])
def export_to_json(self, db):
"""Exports the beer recipes to JSON."""
all_data = []
all_pages = db.retrieve_all_pages()
for page in all_pages:
if GRAINS_KEY in page:
grain_value = page[GRAINS_KEY]
else: # Filter out pages that aren't recipes
continue
if HOPS_KEY in page:
hops_value = page[HOPS_KEY]
else:
hops_value = None
page[GRAINS_KEY], page[HOPS_KEY] = self.normalize_grains_and_hops(grain_value, hops_value, 1.0)
# This isn't serializable so get rid of it.
if ID_KEY in page:
del page[ID_KEY]
all_data.append(page)
return json.dumps(all_data)
def list_styles(self, db):
all_styles = set()
all_pages = db.retrieve_all_pages()
for page in all_pages:
if STYLE_KEY in page:
all_styles.add(page[STYLE_KEY])
return all_styles
def main():
"""This is the entry point that is used to perform unit tests on this module."""
# Command line options.
parser = argparse.ArgumentParser()
parser.add_argument("--style", default=None, help="Style of beers to dump.", required=False)
parser.add_argument("--mongodb-addr", default="localhost:27017", help="Address of the mongo database.", required=False)
parser.add_argument("--json", action="store_true", default=False, help="Exports the recipes as JSON.", required=False)
parser.add_argument("--list-styles", action="store_true", default=False, help="Prints all styles of beer found in the database.", required=False)
args = parser.parse_args()
# Instantiate the object that connects to the database.
db = None
if args.mongodb_addr is not None:
db = CrawlerDatabase.MongoDatabase()
db.connect(args.mongodb_addr)
# Sanity check.
if db is None:
print("ERROR: No database.")
# This option allows the user to dump recipes to stdout.
if args.style is not None:
writer = RecipeWriter()
writer.generate_avg_recipe(db, args.style, 3.0)
# Are we exporting the recipes?
if args.json:
writer = RecipeWriter()
data = writer.export_to_json(db)
print(data)
# Are we exporting the styles?
if args.list_styles:
writer = RecipeWriter()
data = writer.list_styles(db)
print(data)
if __name__ == "__main__":
main()