mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-04 21:58:54 -05:00
Merge pull request #322 from jakobwenzel/fixIngredientParser
Fix ingredient parser to allow Plural-suffixes
This commit is contained in:
@@ -58,6 +58,9 @@ def parse_ingredient(tokens):
|
|||||||
ingredient = ''
|
ingredient = ''
|
||||||
note = ''
|
note = ''
|
||||||
if tokens[-1].endswith(')'):
|
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
|
# last argument ends with closing bracket -> look for opening bracket
|
||||||
start = len(tokens) - 1
|
start = len(tokens) - 1
|
||||||
while not tokens[start].startswith('(') and not start == 0:
|
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
|
# only two arguments, first one is the amount which means this is the ingredient
|
||||||
ingredient = tokens[1]
|
ingredient = tokens[1]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# can't parse first argument as amount -> no unit -> parse everything as ingredient
|
try:
|
||||||
ingredient, note = parse_ingredient(tokens)
|
# 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()
|
return amount, unit.strip(), ingredient.strip(), note.strip()
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import json
|
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.helper.recipe_url_import import get_from_html
|
||||||
from cookbook.tests.test_setup import TestBase
|
from cookbook.tests.test_setup import TestBase
|
||||||
|
|
||||||
@@ -74,7 +74,8 @@ class TestEditsRecipe(TestBase):
|
|||||||
"ägg": (0, "", "ägg", ""),
|
"ägg": (0, "", "ägg", ""),
|
||||||
"50 g smör eller margarin": (50, "g", "smör eller margarin", ""),
|
"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", "")
|
"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.")
|
# 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
|
# does not apply to English tho
|
||||||
@@ -83,5 +84,5 @@ class TestEditsRecipe(TestBase):
|
|||||||
count = 0
|
count = 0
|
||||||
for key, val in expectations.items():
|
for key, val in expectations.items():
|
||||||
count += 1
|
count += 1
|
||||||
parsed = parse_ingredient(key)
|
parsed = parse(key)
|
||||||
self.assertNotEqual(val, parsed)
|
self.assertEqual(val, parsed)
|
||||||
|
|||||||
Reference in New Issue
Block a user