mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-08-03 04:25:24 +02:00
feat: ✨ Add brute strategy to ingredient processor (#744)
* fix UI column width * words * update parser to support diff strats * add new model url * make button more visible * fix nutrition error * feat(backend): ✨ add 'brute' strategy for parsing ingredients * satisfy linter * update UI for creation page * feat(backend): ✨ log 422 errors in detail when not in PRODUCTION * add strategy selector Co-authored-by: Hayden <hay-kot@pm.me>
This commit is contained in:
parent
60908e5a88
commit
3b920babe3
25 changed files with 961 additions and 131 deletions
|
@ -3,6 +3,7 @@ from fractions import Fraction
|
|||
|
||||
import pytest
|
||||
|
||||
from mealie.services.parser_services import RegisteredParser, get_parser
|
||||
from mealie.services.parser_services.crfpp.processor import CRFIngredient, convert_list_to_crf_model
|
||||
|
||||
|
||||
|
@ -15,6 +16,12 @@ class TestIngredient:
|
|||
comments: str
|
||||
|
||||
|
||||
def crf_exists() -> bool:
|
||||
import shutil
|
||||
|
||||
return shutil.which("crf_test") is not None
|
||||
|
||||
|
||||
# TODO - add more robust test cases
|
||||
test_ingredients = [
|
||||
TestIngredient("½ cup all-purpose flour", 0.5, "cup", "all-purpose flour", ""),
|
||||
|
@ -24,12 +31,6 @@ test_ingredients = [
|
|||
]
|
||||
|
||||
|
||||
def crf_exists() -> bool:
|
||||
import shutil
|
||||
|
||||
return shutil.which("crf_test") is not None
|
||||
|
||||
|
||||
@pytest.mark.skipif(not crf_exists(), reason="CRF++ not installed")
|
||||
def test_nlp_parser():
|
||||
models: list[CRFIngredient] = convert_list_to_crf_model([x.input for x in test_ingredients])
|
||||
|
@ -41,3 +42,34 @@ def test_nlp_parser():
|
|||
assert model.comment == test_ingredient.comments
|
||||
assert model.name == test_ingredient.food
|
||||
assert model.unit == test_ingredient.unit
|
||||
|
||||
|
||||
def test_brute_parser():
|
||||
# input: (quantity, unit, food, comments)
|
||||
expectations = {
|
||||
# Dutch
|
||||
"1 theelepel koffie": (1, "theelepel", "koffie", ""),
|
||||
"3 theelepels koffie": (3, "theelepels", "koffie", ""),
|
||||
"1 eetlepel tarwe": (1, "eetlepel", "tarwe", ""),
|
||||
"20 eetlepels bloem": (20, "eetlepels", "bloem", ""),
|
||||
"1 mespunt kaneel": (1, "mespunt", "kaneel", ""),
|
||||
"1 snuf(je) zout": (1, "snuf(je)", "zout", ""),
|
||||
"2 tbsp minced cilantro, leaves and stems": (2, "tbsp", "minced cilantro", "leaves and stems"),
|
||||
"1 large yellow onion, coarsely chopped": (1, "large", "yellow onion", "coarsely chopped"),
|
||||
"1 1/2 tsp garam masala": (1.5, "tsp", "garam masala", ""),
|
||||
"2 cups mango chunks, (2 large mangoes) (fresh or frozen)": (
|
||||
2,
|
||||
"cups",
|
||||
"mango chunks, (2 large mangoes)",
|
||||
"fresh or frozen",
|
||||
),
|
||||
}
|
||||
parser = get_parser(RegisteredParser.brute)
|
||||
|
||||
for key, val in expectations.items():
|
||||
parsed = parser.parse_one(key)
|
||||
|
||||
assert parsed.ingredient.quantity == val[0]
|
||||
assert parsed.ingredient.unit.name == val[1]
|
||||
assert parsed.ingredient.food.name == val[2]
|
||||
assert parsed.ingredient.note in {val[3], None}
|
Loading…
Add table
Add a link
Reference in a new issue