anguswg-ucsb/ingredient-slicer

KeyError: casual quantity term when it appears in uppercase.

Closed this issue · 3 comments

Perhaps it's not perfect English grammar, but this kind of text does appear in recipe ingredients on the web and it causes a KeyError exception when attempting to parse it with this library:

>>> from ingredient_slicer import IngredientSlicer
>>> IngredientSlicer("Few dashes of salt")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./recipe-scrapers/.venv/lib/python3.11/site-packages/ingredient_slicer/_ingredient_slicer.py", line 71, in __init__
    self._parse()
  File "./recipe-scrapers/.venv/lib/python3.11/site-packages/ingredient_slicer/_ingredient_slicer.py", line 1674, in _parse
    self._standardize()
  File "./recipe-scrapers/.venv/lib/python3.11/site-packages/ingredient_slicer/_ingredient_slicer.py", line 777, in _standardize
    method()
  File "./recipe-scrapers/.venv/lib/python3.11/site-packages/ingredient_slicer/_ingredient_slicer.py", line 209, in _find_and_replace_casual_quantities
    self._standardized_ingredient = _utils._find_and_replace_casual_quantities(self._standardized_ingredient)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./recipe-scrapers/.venv/lib/python3.11/site-packages/ingredient_slicer/_utils.py", line 1128, in _find_and_replace_casual_quantities
    replacement_str = _constants.CASUAL_QUANTITIES[match_string]
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
KeyError: 'Few'

(file paths trimmed for brevity)

I will get to this later today or tomorrow morning.
Thanks for bringing to my attention

@jayaddison Closing this issue. Bug was due to the capitalized casual quantity "Few". I fixed this so it won't be an issue in the future. Issue is resolved in new pip versions (>= 1.0.7)