diff --git a/cookbook/helper/ingredient_parser.py b/cookbook/helper/ingredient_parser.py index 1f1b510a9..299e4718f 100644 --- a/cookbook/helper/ingredient_parser.py +++ b/cookbook/helper/ingredient_parser.py @@ -58,6 +58,9 @@ def parse_ingredient(tokens): ingredient = '' note = '' if tokens[-1].endswith(')'): + # Check if the matching opening bracket is in the same token + if ((not tokens[-1].startswith('(')) and ('(' in tokens[-1])): + return parse_ingredient_with_comma(tokens) # last argument ends with closing bracket -> look for opening bracket start = len(tokens) - 1 while not tokens[start].startswith('(') and not start == 0: @@ -126,6 +129,9 @@ def parse(x): # only two arguments, first one is the amount which means this is the ingredient ingredient = tokens[1] except ValueError: - # can't parse first argument as amount -> no unit -> parse everything as ingredient - ingredient, note = parse_ingredient(tokens) + try: + # can't parse first argument as amount -> no unit -> parse everything as ingredient + ingredient, note = parse_ingredient(tokens) + except ValueError: + ingredient = ' '.join(tokens[1:]) return amount, unit.strip(), ingredient.strip(), note.strip() diff --git a/cookbook/tests/other/test_edits_recipe.py b/cookbook/tests/other/test_edits_recipe.py index 6831cef14..0456eb6e9 100644 --- a/cookbook/tests/other/test_edits_recipe.py +++ b/cookbook/tests/other/test_edits_recipe.py @@ -1,6 +1,6 @@ import json -from cookbook.helper.ingredient_parser import parse_ingredient +from cookbook.helper.ingredient_parser import parse from cookbook.helper.recipe_url_import import get_from_html from cookbook.tests.test_setup import TestBase @@ -74,7 +74,8 @@ class TestEditsRecipe(TestBase): "ägg": (0, "", "ägg", ""), "50 g smör eller margarin": (50, "g", "smör eller margarin", ""), "3,5 l Wasser": (3.5, "l", "Wasser", ""), - "3.5 l Wasser": (3.5, "l", "Wasser", "") + "3.5 l Wasser": (3.5, "l", "Wasser", ""), + "400 g Karotte(n)": (400, "g", "Karotte(n)", "") } # for German you could say that if an ingredient does not have an amount and it starts with a lowercase letter, then that is a unit ("etwas", "evtl.") # does not apply to English tho @@ -83,5 +84,5 @@ class TestEditsRecipe(TestBase): count = 0 for key, val in expectations.items(): count += 1 - parsed = parse_ingredient(key) - self.assertNotEqual(val, parsed) + parsed = parse(key) + self.assertEqual(val, parsed)