diff --git a/cookbook/helper/ingredient_parser.py b/cookbook/helper/ingredient_parser.py index b310443ef..c26c21abb 100644 --- a/cookbook/helper/ingredient_parser.py +++ b/cookbook/helper/ingredient_parser.py @@ -219,6 +219,13 @@ class IngredientParser: if len(ingredient) == 0: raise ValueError('string to parse cannot be empty') + # some people/languages put amount and unit at the end of the ingredient string + # if something like this is detected move it to the beginning so the parser can handle it + if re.search(r'^([A-z])+(.)*[1-9](\d)*\s([A-z])+', ingredient): + match = re.search(r'[1-9](\d)*\s([A-z])+', ingredient) + print(f'reording from {ingredient} to {ingredient[match.start():match.end()] + " " + ingredient.replace(ingredient[match.start():match.end()], "")}') + ingredient = ingredient[match.start():match.end()] + ' ' + ingredient.replace(ingredient[match.start():match.end()], '') + # if the string contains parenthesis early on remove it and place it at the end # because its likely some kind of note if re.match('(.){1,6}\s\((.[^\(\)])+\)\s', ingredient): diff --git a/cookbook/tests/other/test_ingredient_parser.py b/cookbook/tests/other/test_ingredient_parser.py index 64ea58e70..21b1853e8 100644 --- a/cookbook/tests/other/test_ingredient_parser.py +++ b/cookbook/tests/other/test_ingredient_parser.py @@ -58,7 +58,7 @@ def test_ingredient_parser(): "2L Wasser": (2, "L", "Wasser", ""), "1 (16 ounce) package dry lentils, rinsed": (1, "package", "dry lentils, rinsed", "16 ounce"), "2-3 c Water": (2, "c", "Water", "2-3"), - "Pane (raffermo o secco) 80 g": (0, None, "Pane 80 g", "raffermo o secco"), # TODO this is actually not a good result but currently expected + "Pane (raffermo o secco) 80 g": (80, "g", "Pane", "raffermo o secco"), } # for German you could say that if an ingredient does not have # an amount # and it starts with a lowercase letter, then that @@ -70,4 +70,5 @@ def test_ingredient_parser(): for key, val in expectations.items(): count += 1 parsed = ingredient_parser.parse(key) + print(f'testing if {key} becomes {val}') assert parsed == val diff --git a/cookbook/views/views.py b/cookbook/views/views.py index 1234cd045..d0224afb1 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -666,7 +666,7 @@ def test(request): parser = IngredientParser(request, False) data = { - 'original': 'Pane (raffermo o secco) 80 g' + 'original': 'Creme Frainche' } data['parsed'] = parser.parse(data['original'])