Utilities#

ingredient_parser._utils.consume(iterator: Iterator, n: int) None[source]#

Advance the iterator n-steps ahead. If n is none, consume entirely. See consume from https://docs.python.org/3/library/itertools.html#itertools-recipes

Parameters:
  • iterator (Iterator) – Iterator to advance.

  • n (int) – Number of iterations to advance by.

Examples

>>> it = iter(range(10))
>>> consume(it, 3)
>>> next(it)
3
>>> it = iter(range(10))
>>> consume(it, None)
>>> next(it)
StopIteration
ingredient_parser._utils.convert_to_pint_unit(unit: str, imperial_units: bool = False) str | Unit[source]#

Convert a unit to a pint.Unit object, if possible. If the unit is not found in the pint Unit Registry, just return the input unit.

Parameters:
  • unit (str) – Unit to find in pint Unit Registry

  • imperial_units (bool, optional) – If True, use imperial units instead of US customary units for the following: fluid ounce, cup, pint, quart, gallon. Default is False, which results in US customary units being used.

Returns:

str | pint.Unit

Examples

>>> convert_to_pint_unit("")
''
>>> convert_to_pint_unit("oz")
<Unit('ounce')>
>>> convert_to_pint_unit("fl oz")
<Unit('fluid_ounce')>
>>> convert_to_pint_unit("cup", imperial_units=True)
<Unit('imperial_cup')>
ingredient_parser._utils.download_nltk_resources() None[source]#

Check if required nltk resources can be found and if not, download them.

ingredient_parser._utils.is_float(value: str) bool[source]#

Check if the value can be converted to a float.

Parameters:

value (str) – Value to check

Returns:

bool – True if the value can be converted to float, else False

Examples

>>> is_float("3")
True
>>> is_float("2.5")
True
>>> is_float("1-2")
False
ingredient_parser._utils.is_range(value: str) bool[source]#

Check if the value is a range e.g. 100-200.

Parameters:

value (str) – Value to check

Returns:

bool – True if the value is a range, else False

Examples

>>> is_range("1-2")
True
>>> is_float("100-500")
True
>>> is_float("1")
False
ingredient_parser._utils.pluralise_units(sentence: str) str[source]#

Pluralise units in the sentence.

Use the same UNITS dictionary as PreProcessor to make any units in sentence plural

Parameters:

sentence (str) – Input sentence

Returns:

str – Input sentence with any words in the values of UNITS replaced with their plural version

Examples

>>> pluralise_units("2 bag")
'2 bags'
>>> pluralise_units("13 ounce")
'13 ounces'
>>> pluralise_units("1.5 loaf bread")
'1.5 loaves bread'
ingredient_parser._utils.show_model_card() None[source]#

Open model card in default application.