/
transformer.py
287 lines (245 loc) · 8.31 KB
/
transformer.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
import parsing
import scraper
import lists
import re
import pprint
import copy
# Lists
from lists import poultryAndGame as poultryAndGame,\
livestock as livestock, \
stocks as stocks, \
meats as meats, \
seafood as seafood, \
prepared as prepared, \
vegSubstitutions as vegSubstitutions, \
healthySubstitutions as healthySubstitutions,\
legumes as legumes
# TODO: add seafoods for veggie transformer
# TODO: healthy transformer, qty, method, and meat substitutions
##############################
##### HEALTHY TRANSFORMER ####
##############################
def healthyTransformer(recipe):
healthyRecipe = recipe
for ingredient in healthyRecipe.ingredients:
substitution = ""
if ingredient.name in healthySubstitutions:
baseIng = healthySubstitutions[ingredient.name]
if ingredient.descriptor in baseIng:
print baseIng[ingredient.descriptor]
substitution = baseIng[ingredient.descriptor]
else:
if baseIng[""]:
print baseIng[""]
substitution = baseIng[""]
else:
print "Nothing to substitute"
# PERFORM SUBSTITUTION
if substitution:
newIng = performHealthySub(ingredient, substitution)
ingredient = newIng
return healthyRecipe
'''
Substitutions not in list:
QTY SUBSTITUTIONS
Reduce sugar (75%)
two egg whties - one whole egg
METHODS
oven/pan-fry - deep fry (cut fat)
steam - boil (steaming removes fewer nutrients)
OTHER
white meat poultry - dark meat poultry
beef - bison
ground beef - ground turkey
'''
def performHealthySub(ingredient, recipe, substitution):
ingredients = healthySubIngredients(ingredient, recipe.ingredients, substitution)
# steps = healthySubSteps(ingredient, ingredients["origIng"], recipe.directions)
recipe.ingredients = ingredients["ingredients"]
# recipe.directions = steps
return recipe
def healthySubIngredients(ingredient, substitution):
newIng = parsing.parseIngredient({"name":substitution, "amount": ""})
newIng.amount = ingredient.amount
newIng.unit = ingredient.unit
return newIng
##############################
##### VEGGIE TRANSFORMER #####
##############################
def veggieTransformer(recipe):
ingredients = recipe.ingredients
vegRecipe = recipe
numMeats = 0
for ingredient in ingredients:
substitution = ""
name = ingredient.name
descriptorMeat = findMeatDescriptor(ingredient.descriptor)
# STOCKS
if name in stocks:
substitution = vegSubstitutions["stock"]
# MEATS
elif name in meats or descriptorMeat in meats:
descriptorMeat = findMeatDescriptor(ingredient.descriptor)
if re.search("(?i)ground", ingredient.descriptor):
if ingredient.name in poultryAndGame:
substitution = vegSubstitutions["ground poultry"]
else:
substitution = vegSubstitutions["ground livestock"]
elif name in poultryAndGame or descriptorMeat in poultryAndGame or isStirFry(recipe) or isDeepFried(recipe):
substitution = vegSubstitutions["poultry"]
elif name in livestock or descriptorMeat in livestock or name in prepared:
substitution = vegSubstitutions["livestock"]
elif name in seafood or descriptorMeat in seafood:
substitution = vegSubstitutions["seafood"]
else:
substitution = None
# PERFORM SUBSTITUTION
if substitution:
vegRecipe = performVegSub(ingredient, vegRecipe, substitution)
numMeats += 1
if not numMeats:
unveggieTransformer(recipe)
else:
printRecipe(vegRecipe, "Vegetarian")
return vegRecipe
def unveggieTransformer(recipe):
# If recipe has legumes, replace with meat
ingredients = recipe.ingredients
steps = recipe.steps
for ingredient in ingredients:
name = ingredient.name
fullName = ingredient.descriptor + ingredient.name
if name in legumes or fullName in legumes:
oldIng = copy.deepcopy(ingredient)
ingredient.name = "chicken"
ingredient.descriptor = "skinless, boneless"
steps = meatifySteps(ingredient, oldIng, steps)
break
recipe.ingredients = ingredients
recipe.steps = steps
printRecipe(recipe, "Un-Veggie")
return recipe
def meatifySteps(ingredient, oldIng, steps):
name = oldIng.name
fullName = oldIng.descriptor + oldIng.name
for step in steps:
direction = step.direction
if "bean" not in fullName:
if re.search("(?i)(%s|%s)" % (name, fullName) , direction):
step.direction = re.sub("(?i)(%s|%s)" % (name, fullName), ingredient.name, direction)
else:
if re.search("(?i)(%s|%s)" % (name, fullName) , direction):
step.direction = re.sub("(?i)%s" % fullName, ingredient.name, direction)
return steps
##### SUBSTITUION METHODS ######
def performVegSub(ingredient, recipe, substitution):
origIng = copy.deepcopy(ingredient)
vegSubIngredients(ingredient, recipe.ingredients, substitution)
steps = vegSubSteps(ingredient, origIng, recipe.steps)
recipe.steps = steps
return recipe
def vegSubIngredients(ingredient, ingList, substitution):
if substitution:
for field in substitution:
if substitution[field]:
if field == "name":
ingredient.name = substitution[field]
elif field == "descriptor":
ingredient.descriptor = substitution[field]
elif field == "preparation":
ingredient.preparation = substitution[field]
else:
ingList.pop(ingList.index(ingredient))
def vegSubSteps(newIng, origIng, steps):
newSteps = steps
# print "!!! Substituting ", origIng.name," ", origIng.descriptor, " with ", newIng.name, " ", newIng.descriptor
for i,step in enumerate(steps):
if hasattr(step, "direction"):
step = step.direction
newStep = step
# STOCKS
if origIng.name in stocks:
if re.search("(?i)(bouillon|stock|broth)", step):
splitWord = re.search("(?i)(bouillon|stock|broth)", step).group()
descriptor = step.split(splitWord)[0].split(' ')[-2]
sub = " ".join([newIng.descriptor, newIng.name])
if descriptor in origIng.descriptor:
newStep = re.sub("(?i)%s%s" % (origIng.descriptor, origIng.name), sub, step)
else:
newStep = re.sub("(?i)%s" % origIng.name, sub, newStep)
# MEATS
if origIng.name in meats:
if re.search("(?i)ground", origIng.descriptor):
newStep = re.sub("(?i)ground %s" % origIng.name, newIng.name, step)
elif re.search("(?i).*%s.*" % origIng.name, step):
newStep = re.sub("(?i)%s" % origIng.name, newIng.name, newStep)
elif findMeatDescriptor(origIng.descriptor):
meat = findMeatDescriptor(origIng.descriptor)
if re.search("(?i).*%s.*" % meat, step):
newStep = re.sub("(?i)%s" % meat, newIng.name, newStep)
newStep = sanitizeMeatDirections(newStep, newIng)
# Replace udpated step
newSteps[i].direction = newStep
return newSteps
def findMeatDescriptor(descriptor):
words = descriptor.split(' ')
meat = ''
# print words
for word in words:
if word in meats:
meat = word
break
return meat
def sanitizeMeatDirections(step, newIng): # Get rid of meat related directions
meatyWords = ["grease", "until", "fat", "meat"]
sentences = step.split(".")
meatlessStep = ""
for sentence in sentences:
for word in meatyWords:
if word == "until":
if re.search("(?i).*(%s|%s).*" % (newIng.name, 'meat'), sentence):
if re.search("(?i).*%s" % word, sentence):
sentence = re.sub("(?i) until.*[;]", ";", sentence)
sentence = re.sub("(?i) until.*$", "", sentence)
elif word == "meat":
if re.search("(?i).* %s" % word, sentence):
sentence = ""
else:
if re.search("(?i).*%s" % word, sentence):
if re.search("(?i).*%s.*[,;]" % word, sentence):
sentence = re.sub("(?i).*%s.*[,;]" % word, "", sentence)
else:
sentence = re.sub("(?i).*%s.*" % word, "", sentence)
sentence = re.sub('^[,:;] ', "", sentence)
sentence = re.sub("^\s+", "", sentence).capitalize()
if sentence:
if meatlessStep:
meatlessStep += "." + sentence
else:
meatlessStep += sentence
return meatlessStep
##### HELPERS ######
def isStirFry(recipe):
pat = "stir-fry"
isStirFry = False
if re.search("(?i).*%s.*" % pat, recipe.name):
isStirFry = True
return isStirFry
def isDeepFried(recipe):
isFried = False
if "fry" in recipe.primaryMethods:
isFried = True
return isFried
##### PRINTING ######
def printRecipe(recipe, transformType):
recipe.name = "%s Version of - " % transformType + recipe.name
print recipe.unicode()
##### RECIPE INFO ######
def getRecipe(recipeURL):
#temporary
lists.ingredientDB = parsing.readIngredientsFromFile('FOOD_DATA/FOOD_DES.txt')
lists.updateNameDB()
###
recipeInfo = scraper.retrieveRecipe(recipeURL)
recipe = parsing.buildRecipeObject(recipeInfo)
return recipe