Skip to main content

Python library for converting numerical words (textual numbers) to numbers

Project description

Installation

pip3 install numwords_to_nums

Usage

Python 3 only!

from numwords_to_nums.numwords_to_nums import NumWordsToNum
num = NumWordsToNum()
num.numerical_words_to_numbers("twenty ten and twenty one")
> 2010 and 21

To use our operator converter 
num = NumWordsToNum()
text = "one point two plus two"
num.numerical_words_to_numbers(text, convert_operator = True)
> 1.2+2

To get the evaluation result of any numerical expression
num = NumWordsToNum()
text = "one point two plus two"
result = num.numerical_words_to_numbers(text, convert_operator = True)
num.evaluate(result)
> 3.2

It can handle a variety of phrases. It also maintains ordinals such as first--> 1st and also supports negative numbers.

"This is just a random sentence." -> 'This is just a random sentence.'
"I am twenty five years old and my dad is 50 years old. I would like to get my father two cars!" -> 'I am 25 years old and my dad is 50 years old. I would like to get my father 2 cars!'
"I was born in twenty ten" -> 'I was born in 1997'
"The temperature of the room was minus seven so I had to turn on the heater." -> 'The temperature of the room was -7 so I had to turn on the heater.'
"In the year twenty twenty one, the forty sixth President of the United States was inaugurated." -> 'In the year 2021, the 46th President of the United States was inaugurated.'
"Joe Biden became the oldest person to assume the presidency at the age of seventy eight." -> 'Joe Biden became the oldest person to assume the presidency at the age of 78.'
"He was elected in November twenty twenty after defeating the incumbent, Donald Trump." -> 'He was elected in November 2020 after defeating the incumbent, Donald Trump.'
"Bidens inauguration took place on January twentieth, which marked the fifty ninth quadrennial presidential inauguration." -> 'Bidens inauguration took place on January 20th, which marked the 59th quadrennial presidential inauguration.'
"The event was held at the U.S. Capitol in Washington, D.C., and was attended by a limited number of people due to the COVID-nineteen pandemic." -> 'The event was held at the U.S. Capitol in Washington, D.C., and was attended by a limited number of people due to the COVID-19 pandemic.'
"Despite the challenges, the fifty ninth presidential inauguration was a historic moment for the country." -> 'Despite the challenges, the 59th presidential inauguration was a historic moment for the country.'
"three forty five" -> '345'
"one point two plus two" -> '1.2+2'  (To use this make sure to use flag (convert_operator = True))
"one point two plus two" -> '3.2' (To use this make sure to use evaluate function)
"zero point five plus zero point five" -> '0.5+0.5'
"zero point five plus zero point five" -> '1.0'
"Please calculate 24-34+50*70" -> '3490'

I find this useful if converting audio to text and have to convert the text to digits, operators, numerical expressions as well as evaluating them. It also find it helpful when converting ordinals to numbers as it maintains the suffix.

Improvements/Issues

  • Complex arithmetic operations.
  • Please email us if you find any issues.

Acknowledgements

I have heavily used code from the SO answers from here: https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers and improved upon them

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numwords_to_nums-1.1.6.tar.gz (13.3 kB view hashes)

Uploaded Source

Built Distribution

numwords_to_nums-1.1.6-py3-none-any.whl (13.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page