Skip to main content

Convert words to numbers and back again

Project description

w2n2w

Convert words to numbers and back again.
Compatible with Python 3.6 and up.

Why is this a thing?

There was one repo called word2number by Akshay Nagpal which converted words to numbers. There was another repo called num2word by MUKESHSIHAG which converted numbers to words.
To my knowledge there wasn't one that did both (also word2number appears un-maintained).
So I forked and re-wrote word2number and here we are.

What does it do?

Converts number words like "forty three" to integers/floats.

import w2n2w
print(w2n2w.word_to_num('forty three'))
> 43
print(w2n2w.word_to_num('twenty-two point nine one'))
> 22.91
print(w2n2w.word_to_num(('one hundred and twenty three million'
                         ' four hundred and fifty six thousand'
                         ' seven hundred and eighty nine')))
> 123456789
print(w2n2w.word_to_num('negative twelve'))
> -12
print(w2n2w.word_to_num('12 thousand and thirty 8'))
> 12038
print(w2n2w.word_to_num('seventy first'))
> 71

It can also convert numbers into words

import w2n2w
from pprint import pprint
print(w2n2w.num_to_word(1234))
> 'one thousand two hundred and thirty four'
# use pprint here to make long line more readable
pprint(w2n2w.num_to_word(123456789))
> ('one hundred and twenty three million four hundred and fifty six thousand '
>  'seven hundred and eighty nine')
print(w2n2w.num_to_word(1.52))
> 'one point five two'
print(w2n2w.num_to_word(-0.999))
> 'negative zero point nine nine nine'
print(w2n2w.num_to_word(0))
> 'zero'

In theory this library can handle any real number (positive or negative) but for numbers greater than or equal to one undecillion (10^36) you will start to face issues, as these numbers are represented in terms of the highest order of magnitude this library knows.
So 10^36 is represented as 10^33 * 1000 or 'one thousand decillion'.

Things to bear in mind

Some examples may produce unexpected behaviour.

import w2n2w
print(w2n2w.word_to_num('one two three'))
> 6
print(w2n2w.word_to_num('thousandth'), w2n2w.word_to_num('one thousandth'))
> 1000, 0.001
print(w2n2w.word_to_num('third'), w2n2w.word_to_num('one third'))
> 3, 0.3333333333333
print(w2n2w.word_to_num('forty seventh'), w2n2w.word_to_num('forty sevenths'))
> 47, 5.7142857142857135

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

w2n2w-0.3.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

w2n2w-0.3.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file w2n2w-0.3.1.tar.gz.

File metadata

  • Download URL: w2n2w-0.3.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.4

File hashes

Hashes for w2n2w-0.3.1.tar.gz
Algorithm Hash digest
SHA256 9b63fea69cb9f51d6a59b35e8eb06b75923ee7354cdd6f5264c13c35d934ff4b
MD5 e9ad4813c8a36b84e6badf0f00bcc587
BLAKE2b-256 eb36051e9d38aae71dc4ae36ca6bfa05a38820192ed994dc14565a191223bfb0

See more details on using hashes here.

File details

Details for the file w2n2w-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: w2n2w-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.4

File hashes

Hashes for w2n2w-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7ca11956d0797ccd4203531680e59f001028e7eb8ad5a3f6d538a6ed7a8e0644
MD5 c189a4ef8325e1c62737f4147a578b23
BLAKE2b-256 6f9f2b66a111df998c7c5e1ea071531f251958784d0fce5a54d48ce24b4a40fb

See more details on using hashes here.

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