Skip to content

Commit

Permalink
Fixed wrong file name, fixed error with italian language, provided mo…
Browse files Browse the repository at this point in the history
…re italian tests
  • Loading branch information
TurconiAndrea committed Aug 24, 2021
1 parent 10840e0 commit 566c2d2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
27 changes: 18 additions & 9 deletions recipe_tagger/recipe_waterfootprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

waterfootprint_embedding_paths = {
"en": "data/ingredient_waterfootprint_en.npy",
"it": "data/ingredient_waterfootprint_ita.npy",
"it": "data/ingredient_waterfootprint_it.npy",
}


Expand Down Expand Up @@ -52,10 +52,6 @@ def __get_quantites_formatted(ingredients, quantities, language):
embedding = get_embedding(waterfootprint_embedding_paths[language])
units = {"ml": 0.001, "gr": 1.0, "kg": 1000.0, "L": 1000.0, "l": 1000.0}
values_units = [re.findall(r"[A-Za-z]+|\d+", q) for q in quantities]
# return [
# float(v[0]) * units[v[1]] / units["gr"] if len(v) == 2 else float(v[0])
# for v in values_units
# ]
quantities = []
for i in range(len(values_units)):
value_unit = values_units[i]
Expand All @@ -70,7 +66,9 @@ def __get_quantites_formatted(ingredients, quantities, language):
return quantities


def get_ingredient_waterfootprint(ingredient, quantity, process=False, language="en"):
def get_ingredient_waterfootprint(
ingredient, quantity, embedding=None, process=False, language="en"
):
"""
Get the water footprint of the provided ingredient based on the quantity.
If the ingredient is not found in the embedding, the recipe tagger module is
Expand All @@ -79,11 +77,16 @@ def get_ingredient_waterfootprint(ingredient, quantity, process=False, language=
:param ingredient: the name of the ingredient.
:param quantity: the quantity of ingredient to calculate water footprint. (in gr)
:param embedding: the water footprint embedding.
:param process: a bool indicating if the provided ingredient must be processed.
:param language: the language of the ingredient.
:return: the water footprint of the provided ingredient.
"""
wf_embedding = get_embedding(waterfootprint_embedding_paths[language])
wf_embedding = (
get_embedding(waterfootprint_embedding_paths[language])
if not embedding
else embedding
)
ingredient = (
process_ingredients(ingredient, language=language) if process else ingredient
)
Expand Down Expand Up @@ -111,13 +114,19 @@ def get_recipe_waterfootprint(
param is setted to true, return also a dictionary with all ingredients and theirs
computed water footprints.
"""
proc_ingredients = [process_ingredients(ing) for ing in ingredients]
wf_embedding = get_embedding(waterfootprint_embedding_paths[language])
proc_ingredients = [
process_ingredients(ing, language=language) for ing in ingredients
]
quantities = __get_quantites_formatted(proc_ingredients, quantities, language)
total_wf = 0
information_wf = {}
for i in range(len(ingredients)):
ing_wf = get_ingredient_waterfootprint(
proc_ingredients[i], quantities[i], language
proc_ingredients[i],
quantities[i],
embedding=wf_embedding,
language=language,
)
information_wf[ingredients[i]] = ing_wf
total_wf = round(total_wf + ing_wf, 2)
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
setup(
name="recipe-tagger",
packages=find_packages(include=["recipe_tagger"]),
version="0.4.0",
version="0.4.1",
description="A library for tagging and classify recipes",
author="Andrea Turconi",
license="MIT",
long_description=README,
long_description_content_type="text/markdown",
url="https://github.com/TurconiAndrea/recipe-tagger",
download_url="https://github.com/TurconiAndrea/recipe-tagger/archive/refs/tags/0.4.0.tar.gz",
keywords=["food", "recipe", "tag", "tagging", "ingredient"],
download_url="https://github.com/TurconiAndrea/recipe-tagger/archive/refs/tags/0.4.1.tar.gz",
keywords=["food", "recipe", "tag", "tagging", "ingredient", "water footprint"],
install_requires=[
"wikipedia-api",
"PyDictionary",
Expand Down
16 changes: 8 additions & 8 deletions tests/test_recipe_tagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from recipe_tagger import recipe_tagger


# @pytest.mark.skip()
@pytest.mark.skip()
def test_is_ingredient_vegan():
"""
Test for is_ingredient_vegan method.
Expand All @@ -18,7 +18,7 @@ def test_is_ingredient_vegan():
assert recipe_tagger.is_ingredient_vegan("chicken") == False


# @pytest.mark.skip()
@pytest.mark.skip()
def test_is_recipe_vegan():
"""
Test for is_recipe_vegan method.
Expand All @@ -29,7 +29,7 @@ def test_is_recipe_vegan():
assert recipe_tagger.is_recipe_vegan(["apple", "pear"]) == True


# @pytest.mark.skip()
@pytest.mark.skip()
def test_add_ingredient():
"""
Test for add_ingredient method.
Expand All @@ -38,7 +38,7 @@ def test_add_ingredient():
assert recipe_tagger.add_ingredient("milk", "dairy") == False


# @pytest.mark.skip()
@pytest.mark.skip()
def test_search_ingredient_hypernyms():
"""
Test for search_ingredient_hypernyms method.
Expand All @@ -52,7 +52,7 @@ def test_search_ingredient_hypernyms():
assert recipe_tagger.search_ingredient_hypernyms("egg") == "egg"


# @pytest.mark.skip()
@pytest.mark.skip()
def test_search_ingredient_class():
"""
Test for search_ingredient_class method.
Expand All @@ -63,7 +63,7 @@ def test_search_ingredient_class():
assert "meat" in recipe_tagger.search_ingredient_class("chicken")


# @pytest.mark.skip()
@pytest.mark.skip()
def test_get_ingredient_class():
"""
Test for get_ingredient_class method.
Expand All @@ -80,7 +80,7 @@ def test_get_ingredient_class():
assert recipe_tagger.get_ingredient_class("chips") == "snack"


# @pytest.mark.skip()
@pytest.mark.skip()
def test_get_recipe_class_percentage():
"""
Test for get_recipe_class_percentage method.
Expand All @@ -92,7 +92,7 @@ def test_get_recipe_class_percentage():
) == [("meat", "66.67%"), ("fruit", "33.33%")]


#  @pytest.mark.skip()
@pytest.mark.skip()
def test_get_recipe_tags():
"""
Test for get_recipe_tags method.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_recipe_waterfootprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def test_get_ingredient_waterfootprint():
wf.get_ingredient_waterfootprint("chicken", 20, process=True, language="en")
== 86.5
)
assert (
wf.get_ingredient_waterfootprint("ombrina", 20, process=True, language="it")
== 51.8
)


# @pytest.mark.skip()
Expand Down Expand Up @@ -70,3 +74,9 @@ def test_get_recipe_waterfootprint():
)
== (4658.08, {"tomato": 4.28, "apple": 328.8, "chicken": 4325.0, "mango": 0.0})
)
assert (
wf.get_recipe_waterfootprint(
["pomodori", "mela", "pollo"], ["20gr", "5ml", "1l"], language="it"
)
== 4329.28
)

0 comments on commit 566c2d2

Please sign in to comment.