From 24f331c2088e884e1b1409746e63ab613422e865 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Wed, 9 Nov 2022 14:24:12 +0100 Subject: [PATCH] improved ingredient parser handling of amount unit without space --- cookbook/helper/ingredient_parser.py | 4 ++++ cookbook/tests/other/test_ingredient_parser.py | 2 +- cookbook/views/views.py | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cookbook/helper/ingredient_parser.py b/cookbook/helper/ingredient_parser.py index 468f1128..45f4e553 100644 --- a/cookbook/helper/ingredient_parser.py +++ b/cookbook/helper/ingredient_parser.py @@ -235,6 +235,10 @@ class IngredientParser: # leading spaces before commas result in extra tokens, clean them out ingredient = ingredient.replace(' ,', ',') + # if amount and unit are connected add space in between + if re.match('([0-9])+([A-z])+\s', ingredient): + ingredient = re.sub(r'(?<=([a-z])|\d)(?=(?(1)\d|[a-z]))', ' ', ingredient) + tokens = ingredient.split() # split at each space into tokens if len(tokens) == 1: # there only is one argument, that must be the food diff --git a/cookbook/tests/other/test_ingredient_parser.py b/cookbook/tests/other/test_ingredient_parser.py index d61cbc69..020d729d 100644 --- a/cookbook/tests/other/test_ingredient_parser.py +++ b/cookbook/tests/other/test_ingredient_parser.py @@ -54,7 +54,7 @@ def test_ingredient_parser(): "3,5 l Wasser": (3.5, "l", "Wasser", ""), "3.5 l Wasser": (3.5, "l", "Wasser", ""), "400 g Karotte(n)": (400, "g", "Karotte(n)", ""), - "400g unsalted butter": (400, "g", "butter", "unsalted"), + "400g unsalted butter": (400, "g", "unsalted butter", ""), "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"), diff --git a/cookbook/views/views.py b/cookbook/views/views.py index 8dfa1bc9..2907e639 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -438,7 +438,7 @@ def test(request): parser = IngredientParser(request, False) data = { - 'original': '1 Porreestange(n) , ca. 200 g' + 'original': '90g golden syrup' } data['parsed'] = parser.parse(data['original'])