From 394f24c29f1d41b38e1777b89aee237a6e8d45f1 Mon Sep 17 00:00:00 2001 From: Marcus Wolschon Date: Sun, 15 Jan 2023 13:49:16 +0100 Subject: [PATCH] handle special cases in ingredients --- cookbook/helper/ingredient_parser.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cookbook/helper/ingredient_parser.py b/cookbook/helper/ingredient_parser.py index 47e4058f..e9fe9310 100644 --- a/cookbook/helper/ingredient_parser.py +++ b/cookbook/helper/ingredient_parser.py @@ -235,6 +235,19 @@ class IngredientParser: # leading spaces before commas result in extra tokens, clean them out ingredient = ingredient.replace(' ,', ',') + # Handle special cases of units that contain a space in their name + # "2 geh TL XYZ" => "2 geh.TL XYZ" => [amount=2, unit="geh.TL", food="XYZ"] + ingredient = ingredient.replace("geh. TL", "geh.TL") \ + .replace("geh. TL", "geh.TL") \ + .replace("geh. EL", "geh.EL") \ + .replace("ges. TL", "ges.TL") \ + .replace("ges. EL", "ges.EL") + # add others here until we find a better way to handle these + + # handle "(from) - (to)" amounts by using the minimum amount and adding the range to the description + # "10.5 - 200 g XYZ" => "100 g XYZ (10.5 - 200)" + ingredient = re.sub("(\d+|\d+[\\.,]\d+) - (\d+|\d+[\\.,]\d+) (.*)", "\\1 \\3 (\\1 - \\2)", ingredient) + # 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)