-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmkrcp.py
executable file
·207 lines (181 loc) · 7.7 KB
/
mkrcp.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
#! /usr/bin/python
import re
import json
import os
import sys
import urllib
import gzip
import pdb
import random
import string
from collections import Counter, deque
db_path = "data/slim_recipes.json"
recipe_path = "data/recipes.json.gz"
recipe_source = "http://openrecipes.s3.amazonaws.com/recipeitems-latest.json.gz"
ings_path = "data/ings.json"
measure_words = ["gallon", "gal", "quart", "q", "cup", "tablespoon",
"tbsp", "teaspoon", "tsp", "ml", "dash", "dashes",
"pinch", "pinches", "pound", "ounce", "ounces", "oz",
"fl oz", "fl. oz", "clove", "whole", "box", "boxes",
"package", "stick", "weight", "fluid", "\d+", "oz.",
"jar", "can", "slice", "slices", "tbs.", "pint"]
RE_AMOUNT = re.compile(r"\d+g|([\d\xbc-\xbe/]+ )+"+"s?|([\d\xbc-\xbe]+ )+".join(measure_words), flags=re.I|re.U)
def setup():
if not os.path.isfile(ings_path):
print("No database found at {}. Building...".format(ings_path))
try:
recipes = gzip.open(recipe_path, "rb")
except IOError:
download_choice = raw_input("No recipe data. Download from {}? (Y/n)".format(recipe_source))
if download_choice in ("Y", "y", ""):
print("Ok, downloading...")
urllib.urlretrieve(recipe_source, db_path)
print("Done. Downloaded to {}".format(recipe_path))
recipes =gzip.open(recipe_path, "rb")
elif download_choice in ("N", "n"):
print("Ok, exiting...")
sys.exit()
finally:
lines = recipes.readlines()
recipe_list = (json.loads(line) for line in lines)
db = [{"ingredients": recipe["ingredients"].split("\n"),
"yield": recipe.get("recipeYield")} for recipe in recipe_list]
with open(db_path, "wb") as db_file:
json.dump(db, db_file)
with open(db_path, "rb") as db_file:
db = json.loads(db_file.read())
return db
def main(args):
if os.path.isfile(ings_path):
with open(ings_path, "rb") as ings_file:
ings = json.loads(ings_file.read())
else:
db = setup()
db = [extract_ingredient(recipe["ingredients"]) for recipe in db]
db_dict, ing_ctr = count_ingredients(db)
most_common_ingredients = [_[0] for _ in ing_ctr.most_common(1000)]
most_common_pairings = [dict(db_dict[ing].most_common(100)) for ing in most_common_ingredients]
ings = dict(zip(most_common_ingredients, most_common_pairings))
with open(ings_path, "wb") as ings_file:
json.dump(ings, ings_file)
entrees = ["potatoes", "chicken breasts", "chicken thighs",
"ground beef", "pork chops", "uncooked white rice",
"basmati rice", "quinoa"]
drinks = ["brandy", "bourbon", "vodka", "gin", "rum"]
food_type = {"drinks": drinks, "entrees": entrees}
from time import sleep
for i in range(args.number):
ing1 = random.choice(food_type[args.genre])
ing2 = random.choice(ings.keys())
recipe = link_ingredients(ing1, ing2, ings, args)
args.outfile.write(string.capwords(u"{} with {}\n".format(ing1, ing2)).encode("utf-8"))
if recipe:
args.outfile.write(u"\nCommonness Index: {0:.2f}".format(recipe[0]*1000).encode("utf-8"))
try:
args.outfile.write(u"\nRecipe:\n- "+u"\n- ".join(recipe[1]).encode("utf-8"))
except UnicodeDecodeError:
args.outfile.write("\nUnicode is the way of the devil!")
else:
args.outfile.write("\nRecipe:\nNo path can guide the wicked.")
args.outfile.write("\n"+"="*80+"\n\n")
if args.outfile == sys.stdout:
sleep(5)
def extract_ingredient(ing_list):
new_ing_list = []
for ing in ing_list:
new_ing = re.sub(RE_AMOUNT, "", ing)
new_ing = re.sub(r"[:%\d/,()\. -]+", lambda x: " ", new_ing)
new_ing = new_ing.strip()
new_ing = new_ing.lower()
new_ing_list.append(new_ing)
return new_ing_list
def count_ingredients(db):
ing_ctr = Counter()
ing_dict = {}
for ing_list in db:
for ing in ing_list:
ing_ctr[ing] += 1
if ing not in ing_dict:
ing_dict[ing] = Counter()
for other_ing in ing_list:
ing_dict[ing][other_ing] += 1
return ing_dict, ing_ctr
def make_recipes_n_prob(n, num_ingredients, num_recipes, ings):
recipes = []
for i in range(num_recipes):
recipe = [random.choice(ings.keys())]
while len(recipe) < num_ingredients:
recipe = n_probable(n, recipe, ings)
recipes.append(recipe)
return recipes
def n_probable(n, recipe, ings):
seed_dict = None
while seed_dict == None:
seed = random.choice(recipe)
seed_dict = ings.get(seed)
seed_dict.pop(seed, None)
seed_sorted = sorted(seed_dict, key=seed_dict.get, reverse=True)
max_size = seed_dict[seed_sorted[0]]
possible_ings = []
additional_ings = set()
while len(possible_ings) < n:
rand = random.randint(1, max_size)
possible_ings = {_ for _ in ings[seed].keys() if rand > ings[seed][_]} - set(recipe)
for i in range(n):
rand_choice = random.choice(list(possible_ings))
additional_ings.add(rand_choice)
possible_ings.remove(rand_choice)
return recipe+list(additional_ings)
def link_ingredients(source, end, ings, args):
queue = deque([(source, [])])
visited = Counter({source: len(ings[source].keys())})
paths = []
while len(queue) > 0:
test_ing, test_path = queue.popleft()
new_test_path = test_path + [test_ing]
if test_ing == end:
paths.append((get_average_weight(new_test_path, ings), new_test_path))
else:
try:
ing_size = len(ings[test_ing].keys())
ing_sorted = sorted(ings[test_ing].keys(), key=ings[test_ing].get, reverse=args.normal)
possible_ings = ing_sorted[:int(ing_size/4)]
for ing in possible_ings:
if ing == test_ing:
continue
if visited[ing] < 1:
queue.append((ing, new_test_path))
except KeyError:
continue
visited[test_ing] += 1
if paths != []:
return sorted(paths, reverse = args.normal)[0]
else:
return None
def get_average_weight(ing_list, ings):
edges = zip(ing_list[:-1], ing_list[1:])
sum = 0
for e in edges:
size = float(ings[e[0]][e[0]])
weight = float(ings[e[0]][e[1]])
sum += weight/size
return sum/len(edges)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--clean", action="store_true", default=False,
help="Removes any existing refined recipe data.")
parser.add_argument("-g", "--genre", default="entrees",
help="Currently only accepts 'drinks' and 'entrees'. Defaults to 'entrees'.")
parser.add_argument("-n", "--normal", action="store_true", default=False,
help="Tries to make probable recipes instead of improbable ones.")
parser.add_argument("outfile", nargs="?", default=sys.stdout, type=argparse.FileType("w"),
help="File to output recipes to. If none given, prints to stdout.")
parser.add_argument("-N", "--number", type=int, default=10,
help="Number of recipes to generate.")
args = parser.parse_args()
if args.clean and os.path.isfile(db_path):
os.remove(db_path)
if args.clean and os.path.isfile(ings_path):
os.remove(ings_path)
main(args)