fix: add confidence calculation for BruteForceParser (#5903)

Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com>
This commit is contained in:
Skye Samuels
2025-08-11 11:56:35 -07:00
committed by GitHub
parent d4e62c5ab6
commit 0b3fe2c8da
2 changed files with 67 additions and 4 deletions

View File

@@ -12,6 +12,8 @@ from mealie.schema.recipe.recipe_ingredient import (
CreateIngredientFood,
CreateIngredientUnit,
IngredientConfidence,
IngredientFood,
IngredientUnit,
ParsedIngredient,
RegisteredParser,
)
@@ -28,11 +30,11 @@ class BruteForceParser(ABCIngredientParser):
Brute force ingredient parser.
"""
async def parse_one(self, ingredient: str) -> ParsedIngredient:
bfi = brute.parse(ingredient, self)
async def parse_one(self, ingredient_string: str) -> ParsedIngredient:
bfi = brute.parse(ingredient_string, self)
parsed_ingredient = ParsedIngredient(
input=ingredient,
input=ingredient_string,
ingredient=RecipeIngredient(
unit=CreateIngredientUnit(name=bfi.unit),
food=CreateIngredientFood(name=bfi.food),
@@ -41,7 +43,28 @@ class BruteForceParser(ABCIngredientParser):
),
)
return self.find_ingredient_match(parsed_ingredient)
matched_ingredient = self.find_ingredient_match(parsed_ingredient)
qty_conf = 1
note_conf = 1
unit_obj = matched_ingredient.ingredient.unit
food_obj = matched_ingredient.ingredient.food
unit_conf = 1 if bfi.unit is None or isinstance(unit_obj, IngredientUnit) else 0
food_conf = 1 if bfi.food is None or isinstance(food_obj, IngredientFood) else 0
avg_conf = (qty_conf + unit_conf + food_conf + note_conf) / 4
matched_ingredient.confidence = IngredientConfidence(
average=avg_conf,
quantity=qty_conf,
unit=unit_conf,
food=food_conf,
comment=note_conf,
)
return matched_ingredient
async def parse(self, ingredients: list[str]) -> list[ParsedIngredient]:
return [await self.parse_one(ingredient) for ingredient in ingredients]