fix: match multi-word foods when the brute parser splits off an unknown unit (#8744)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fb05f2da33
commit
9ca95fbb29
2 files changed
+27
-3
No files matched your search
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user