Skip to content

Commit

Permalink
fix(nutrisight): improve extraction postprocessing (#1486)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 authored Dec 6, 2024
1 parent ca848df commit 4137468
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
37 changes: 18 additions & 19 deletions robotoff/prediction/nutrition_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,26 +551,25 @@ def match_nutrient_value(
# `entity_label` is in the form "energy_kcal_100g", so we can
# extract the unit from the 2nd part (index 1) of the entity name
unit = entity_label.split("_")[1].lower()
if (
any(
entity_label.startswith(target)
for target in (
"proteins",
"sugars",
"carbohydrates",
"fat",
"fiber",
"salt",
# we use "_" here as separator as '-' is only used in
# Product Opener, the label names are all separated by '_'
"saturated_fat",
"added_sugars",
"trans_fat",
)
if any(
entity_label.startswith(target)
for target in (
"proteins",
"sugars",
"carbohydrates",
"fat",
"fiber",
"salt",
# we use "_" here as separator as '-' is only used in
# Product Opener, the label names are all separated by '_'
"saturated_fat",
"added_sugars",
"trans_fat",
)
and value.endswith("9")
and "." in value
and not value.endswith(".9")
) and (
value in ("08", "09")
or (value.endswith("8") and "." in value and not value.endswith(".8"))
or (value.endswith("9") and "." in value and not value.endswith(".9"))
):
unit = "g"
value = value[:-1]
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/prediction/test_nutrition_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,13 @@ def test_aggregate_entities_multiple_entities(self):
("25.9", "iron_100g", ("25.9", None, True)),
("O g", "salt_100g", ("0", "g", True)),
("O", "salt_100g", ("0", None, True)),
("0,19", "saturated_fat_100g", ("0.1", "g", True)),
# Missing unit and value ends with '9' or '8'
("0.19", "saturated_fat_100g", ("0.1", "g", True)),
("0,18", "saturated_fat_100g", ("0.1", "g", True)),
("08", "saturated_fat_100g", ("0", "g", True)),
("09", "salt_100g", ("0", "g", True)),
# Missing unit but value does not end with '8' or '9'
("091", "proteins_100g", ("091", None, True)),
],
)
def test_match_nutrient_value(words_str: str, entity_label: str, expected_output):
Expand Down

0 comments on commit 4137468

Please sign in to comment.