From 8931fa855755704bb34f60233ee292d8124dc9ce Mon Sep 17 00:00:00 2001 From: Jakob Wenzel Date: Thu, 7 Jan 2021 19:05:12 +0100 Subject: [PATCH 1/4] fix ingredient parser testcase * call correct function * flip assertion --- cookbook/tests/other/test_edits_recipe.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cookbook/tests/other/test_edits_recipe.py b/cookbook/tests/other/test_edits_recipe.py index 6831cef14..25bc92452 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 @@ -83,5 +83,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) From fd255fd6ad9a3edf99fb0e5c9200db3e4948d0e7 Mon Sep 17 00:00:00 2001 From: Jakob Wenzel Date: Thu, 7 Jan 2021 19:07:49 +0100 Subject: [PATCH 2/4] added failing testcase for ingredient parser --- cookbook/tests/other/test_edits_recipe.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cookbook/tests/other/test_edits_recipe.py b/cookbook/tests/other/test_edits_recipe.py index 25bc92452..0456eb6e9 100644 --- a/cookbook/tests/other/test_edits_recipe.py +++ b/cookbook/tests/other/test_edits_recipe.py @@ -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 From 8ed2562454d3d63f6f30bd7324e9afb5ae59572c Mon Sep 17 00:00:00 2001 From: Jakob Wenzel Date: Thu, 7 Jan 2021 19:20:46 +0100 Subject: [PATCH 3/4] allow plural-suffixes in ingredient parser --- cookbook/helper/ingredient_parser.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cookbook/helper/ingredient_parser.py b/cookbook/helper/ingredient_parser.py index 1f1b510a9..47fc8759a 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: From 29903af085a515233120d27fa92505fa16d9fb90 Mon Sep 17 00:00:00 2001 From: Jakob Wenzel Date: Thu, 7 Jan 2021 19:49:02 +0100 Subject: [PATCH 4/4] catch error when trying to parse into ingredient/note --- cookbook/helper/ingredient_parser.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cookbook/helper/ingredient_parser.py b/cookbook/helper/ingredient_parser.py index 47fc8759a..299e4718f 100644 --- a/cookbook/helper/ingredient_parser.py +++ b/cookbook/helper/ingredient_parser.py @@ -129,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()