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

@@ -226,6 +226,46 @@ def test_brute_parser(
assert not comment
@pytest.mark.parametrize(
"unit, food, expect_unit_match, expect_food_match, expected_avg",
[
pytest.param("Cups", "potatoes", True, True, 1.0, id="all matched"),
pytest.param("Cups", "veryuniquefood", True, False, 0.75, id="unit matched only"),
pytest.param("veryuniqueunit", "potatoes", False, True, 0.75, id="food matched only"),
pytest.param("veryuniqueunit", "veryuniquefood", False, False, 0.5, id="neither matched"),
],
)
def test_brute_parser_confidence(
unit: str,
food: str,
expect_unit_match: bool,
expect_food_match: bool,
expected_avg: float,
unique_local_group_id: UUID4,
parsed_ingredient_data: tuple[list[IngredientFood], list[IngredientUnit]],
):
input_str = f"1 {unit} {food}"
with session_context() as session:
original_loop = asyncio.get_event_loop()
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
parser = get_parser(RegisteredParser.brute, unique_local_group_id, session)
parsed = loop.run_until_complete(parser.parse_one(input_str))
finally:
loop.close()
asyncio.set_event_loop(original_loop)
conf = parsed.confidence
assert conf.quantity == 1
assert conf.comment == 1
assert conf.unit == (1 if expect_unit_match or not unit else 0)
assert conf.food == (1 if expect_food_match or not food else 0)
assert conf.average == expected_avg
@pytest.mark.parametrize(
"input, expected_unit_name, expected_food_name, expect_unit_match, expect_food_match",
(