From 9ca95fbb2976413bb2fdd5cfb7ff997d2e24cbd2 Mon Sep 17 00:00:00 2001 From: Gustaf Ahlner Date: Wed, 21 Oct 2026 18:37:31 +0000 Subject: [PATCH] fix: match multi-word foods when the brute parser splits off an unknown unit (#8744) Co-authored-by: Claude Opus 5.5 --- mealie/services/parser_services/_base.py | 13 ++++++++++--- .../ingredient_parser/test_brute_parser.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/mealie/services/parser_services/_base.py b/mealie/services/parser_services/_base.py index 3da1cbf9c..592198af5 100644 --- a/mealie/services/parser_services/_base.py +++ b/mealie/services/parser_services/_base.py @@ -163,6 +163,9 @@ class ABCIngredientParser(ABC): async def parse(self, ingredients: list[str]) -> list[ParsedIngredient]: ... def find_ingredient_match(self, ingredient: ParsedIngredient) -> ParsedIngredient: + # Matching replaces the food with the database object, so keep the parsed name + parsed_food_name = ingredient.ingredient.food.name if ingredient.ingredient.food else "" + if ingredient.ingredient.food and (food_match := self.data_matcher.find_food_match(ingredient.ingredient.food)): ingredient.ingredient.food = food_match @@ -172,11 +175,15 @@ class ABCIngredientParser(ABC): # Parser might have wrongly split a food into a unit and food. food_is_matched = bool(ingredient.ingredient.food and ingredient.ingredient.food.id) unit_is_matched = bool(ingredient.ingredient.unit and ingredient.ingredient.unit.id) - food_name = ingredient.ingredient.food.name if ingredient.ingredient.food else "" unit_name = ingredient.ingredient.unit.name if ingredient.ingredient.unit else "" - if not food_is_matched and not unit_is_matched and food_name and unit_name: - food_match = self.data_matcher.find_food_match(f"{unit_name} {food_name}") + if not unit_is_matched and parsed_food_name and unit_name: + whole_food_name = f"{unit_name} {parsed_food_name}" + if food_is_matched: + # e.g. "white wine" split into "white" + "wine": only an exact match may replace "wine" + food_match = self.data_matcher.foods_by_alias.get(IngredientFoodModel.normalize(whole_food_name)) + else: + food_match = self.data_matcher.find_food_match(whole_food_name) if food_match: ingredient.ingredient.food = food_match diff --git a/tests/unit_tests/services_tests/ingredient_parser/test_brute_parser.py b/tests/unit_tests/services_tests/ingredient_parser/test_brute_parser.py index 408051d8a..bc066fd63 100644 --- a/tests/unit_tests/services_tests/ingredient_parser/test_brute_parser.py +++ b/tests/unit_tests/services_tests/ingredient_parser/test_brute_parser.py @@ -81,6 +81,7 @@ def build_parsed_ing(food: str | None, unit: str | None) -> ParsedIngredient: ), pytest.param("red pepper flakes", 0, "", "red pepper flakes", "", id="red pepper flakes"), pytest.param("1 bell peppers", 1, "", "bell peppers", "", id="1 bell peppers"), + pytest.param("1 green onion", 1, "", "green onion", "", id="1 green onion"), pytest.param("1 stalk bell peppers", 1, "Stalk", "bell peppers", "", id="1 big stalk bell peppers"), pytest.param("a big stalk bell peppers", 0, "Stalk", "bell peppers", "", id="a big stalk bell peppers"), pytest.param( @@ -227,6 +228,22 @@ def test_brute_parser_confidence( True, id="no unit match", ), + pytest.param( + build_parsed_ing(unit="green", food="onion"), + None, + "green onion", + False, + True, + id="multi-word food split into unit and food", + ), + pytest.param( + build_parsed_ing(unit="gren", food="onion"), + "gren", + "onion", + False, + True, + id="matched food not replaced by fuzzy multi-word match", + ), pytest.param( build_parsed_ing(unit=None, food="potatoes"), None,