lexnlp.extract.en.amounts: Extracting amounts

The lexnlp.extract.en.amounts module contains methods that allow for the extraction of amounts from text. Sample amounts that are covered by this module include:

  • THIRTY-SIX THOUSAND TWO-HUNDRED SIXTY-SIX AND 2/100
  • total 2 million people
  • total 2.035 billion tons of
  • total 2.035 billion tons of
  • fifteen cats
  • 20,000,000
  • thirty-five million units

The full list of current unit test cases can be found here: https://github.com/LexPredict/lexpredict-lexnlp/tree/master/test_data/lexnlp/extract/en/tests/test_amounts

Extracting amounts

lexnlp.extract.en.amounts.get_amounts(text, return_sources=False, float_digits=4) → typing.Generator

Find possible amount references in the text. :param text: text :param return_sources: return amount AND source text :param float_digits: round float to N digits, don’t round if None :return: list of amounts

Example

>>> import lexnlp.extract.en.amounts
>>> text = "There are ten cows in the dozen acre pasture."
>>> print(list(lexnlp.extract.en.amounts.get_amounts(text)))
[10, 12]

>>> import lexnlp.extract.en.amounts
>>> text = "Twenty-seven days until the one-hundred and one dogs arrive."
>>> print(list(lexnlp.extract.en.amounts.get_amounts(text)))
[27, 101]

Converting text to numbers

lexnlp.extract.en.amounts.text2num(s, search_fraction=True)

Convert written amount into integer/float. :param s: written number :param search_fraction: extract fraction :return: integer/float

Example

>>> import lexnlp.extract.en.amounts
>>> print(lexnlp.extract.en.amounts.text2num("seventy one"))
71
>>> print(lexnlp.extract.en.amounts.text2num("one billion and seventy"))
1000000070