Common#

ingredient_parser._common.consume(iterator: Iterator, n: int | None) 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:
iteratorIterator

Iterator to advance.

nint | None

Number of iterations to advance by. If None, consume entire iterator.

Examples

>>> it = iter(range(10))
>>> consume(it, 3)
>>> next(it)
3
>>> it = iter(range(10))
>>> consume(it, None)
>>> next(it)
StopIteration
ingredient_parser._common.download_nltk_resources() None[source]#

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

ingredient_parser._common.group_consecutive_idx(idx: list[int]) Generator[Iterator[int], None, None][source]#

Yield groups of consecutive indices.

Given a list of integers, yield groups of integers where the value of each in a group is adjacent to the previous element’s value.

Parameters:
idxlist[int]

List of indices.

Yields:
list[list[int]]

List of lists, where each sub-list contains consecutive indices.

Examples

>>> groups = group_consecutive_idx([0, 1, 2, 4, 5, 6, 8, 9])
>>> [list(g) for g in groups]
[[0, 1, 2], [4, 5, 6], [8, 9]]
ingredient_parser._common.is_float(value: str) bool[source]#

Check if value can be converted to a float.

Parameters:
valuestr

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._common.is_range(value: str) bool[source]#

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

Parameters:
valuestr

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._common.show_model_card(lang: str = 'en') None[source]#

Open model card for specified language in default application.

Parameters:
langstr, optional

Selected language to open model card for.

Raises:
FileNotFoundError

Raised if model card not found at expected path.

ValueError

Raised if unsupported language provided in lang argument.