Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def normalize_ontology_name(name):
return normalize_ingredient_name(name).replace('_', ' ')
def get_ingredients(tokenized_string, enum=False):
"""
Returns a tuple of (index, ingredient) or a list of ingredients from a
tokenized string.
>>> raw_input_string = "I like apples, cinnamon, and pepper."
>>> tokenizer = nltk.WordPunctTokenizer()
>>> tokenized_string = tokenizer.tokenize(raw_input_string)
>>> for i,w in get_ingredients(tokenized_string, enum=True): print i,w
2 apples
4 cinnamon
7 pepper
"""
words = [normalize_ingredient_name(x) for x in tokenized_string]
results = [x for x in enumerate(words) if is_ingredient(x[1])]
if enum:
return [(i, tokenized_string[i]) for i, w in results]
else:
return [tokenized_string[i] for i, w in results]
def get_ontology_node(self, name):
"""
Get the ontology node for the given name. Rather that performing
an exact match with the name, this uses a heuristic to find the
best-matching OntologyNode.
"""
return self._get_closest_ontology_node(normalize_ingredient_name(name))
To find recipes that have exactly 5 steps:
>>> recipes = db.get_recipes(num_steps=5)
To find Italian recipes:
>>> recipes = db.get_recipes(include_cuisines=["Italian"])
"""
# Make sure that include_* and exclude_* arguments are not strings:
for argument in [include_ingredients, exclude_ingredients,
include_cuisines, exclude_cuisines]:
if isinstance(argument, types.StringTypes):
raise ValueError('include_* and exclude_* must be iterables of'
' strings, not strings.')
# Normalize ingredient names, so that they match the names stored in
# the database.
include_ingredients = \
[normalize_ingredient_name(i) for i in include_ingredients]
exclude_ingredients = \
[normalize_ingredient_name(i) for i in exclude_ingredients]
# Construct the query
query = self._session.query(Recipe)
# Handle ingredient inclusion and exclusion
if include_ingredients or exclude_ingredients:
double_join = join(RecipeIngredientAssociation, Recipe)
triple_join = join(double_join, Ingredient)
join_query = query.select_from(triple_join)
query = join_query
for ingredient_name in include_ingredients:
query = query.intersect(
join_query.filter(Ingredient.name == ingredient_name))
for ingredient_name in exclude_ingredients:
query = query.except_(
join_query.filter(Ingredient.name == ingredient_name))
def get_ingredients(self, name=None):
"""
Get ingredients matching the given criteria.
"""
query = self._session.query(Ingredient)
if name != None:
name = normalize_ingredient_name(name)
query = query.filter_by(name=name)
return query