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
- fifteen cats
- 20,000,000
- thirty-five million units
- 10K files
- 1 trill
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, extended_sources=True, float_digits=4) → Generator¶ Find possible amount references in the text. :param text: text :param return_sources: return amount AND source text :param extended_sources: return data around amount itself :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]
>>> text = "Twenty-seven days until the one-hundred and one dogs arrive."
>>> print(list(lexnlp.extract.en.amounts.get_amounts(text)))
[27, 101]
>>> text = "There are 10K documents."
>>> print(list(lexnlp.extract.en.amounts.get_amounts(text)))
[10000.0]
>>> text = "There is 10 trill Zimbabwean Paper Money for sale on eBay"
>>> print(list(lexnlp.extract.en.amounts.get_amounts(text)))
[10000000000000.0]
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