Skip to content

Commit

Permalink
Fixed search ingredient class.
Browse files Browse the repository at this point in the history
  • Loading branch information
TurconiAndrea committed Sep 27, 2021
1 parent b69a9c2 commit 8d191d8
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions recipe_tagger/recipe_tagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,27 @@ def search_ingredient_class(ingredient, language="en"):
if " " in ingredient:
ingredient = ingredient.split(" ")[-1]

categories = []
dictionary = PyDictionary()
wiki = wikipediaapi.Wikipedia(language)

page = wiki.page(ingredient)
meaning = (
dictionary.meaning(ingredient, disable_errors=True)["Noun"]
if dictionary.meaning(ingredient, disable_errors=True)
else None
)
ontology = ", ".join(meaning) if meaning else ""

categories = []
for category in FoodCategory:
if page and re.search(r"\b({0})\b".format(category.name), page.summary):
categories.append(category.name)
if ontology and re.search(r"\b({0})\b".format(category.name), ontology):
categories.append(category.name)
return max(categories, key=categories.count) if len(categories) else None
try:
page = wiki.page(ingredient)
meaning = (
dictionary.meaning(ingredient, disable_errors=True)["Noun"]
if dictionary.meaning(ingredient, disable_errors=True)
else None
)
ontology = ", ".join(meaning) if meaning else ""

for category in FoodCategory:
if page and re.search(r"\b({0})\b".format(category.name), page.summary):
categories.append(category.name)
if ontology and re.search(r"\b({0})\b".format(category.name), ontology):
categories.append(category.name)
except:
pass
return max(categories, key=categories.count) if categories else None


def get_ingredient_class(ingredient, language="en"):
Expand Down

0 comments on commit 8d191d8

Please sign in to comment.