Skip to main content

Convert numbers expressed in English to its numeric value.

Project description

easyword2num

easyword2num is a Python library that converts English words representing numbers into their numerical values. It supports large numbers, fractions, decimals, negative numbers, and mixed decimal-unit expressions.

Install

pip install easyword2num

Usage

from easyword2num import word2num

# Basic usage
number = word2num("seven million three thousand and four point five")
print(number)  # Output: 7003004.5

# Handles negative numbers
number = word2num("negative sixteen")
print(number)  # Output: -16

# Fractions
number = word2num("one fifth")
print(number)  # Output: 0.2

# Decimals
number = word2num("two point three")
print(number)  # Output: 2.3

# Mixed decimal-unit expressions
number = word2num("1.2 million")

Handling Numerical Sequences

By default, word2num does not interpret numerical sequences like "twenty nineteen" as 2019. To enable this behavior, set the allow_numerical_sequences parameter to True.

number = word2num("twenty nineteen", allow_numerical_sequences=True)
print(number)  # Output: 2019

Error Handling

If the input string cannot be interpreted as a number, a UninterpretableNumberError is raised.

from easyword2num import word2num, UninterpretableNumberError

try:
    number = word2num("invalid input")
except UninterpretableNumberError as e:
    print(f"Error: {e}")

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request on GitHub.

Development Setup

If you use Poetry

poetry install
poetry run pytest

Otherwise...

python -m venv venv
source venv/bin/activate
pip install -r requirements-dev.txt
pip install -e .
pytest

License

This project is licensed under the MIT License.

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

easyword2num-0.1.3.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easyword2num-0.1.3-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file easyword2num-0.1.3.tar.gz.

File metadata

  • Download URL: easyword2num-0.1.3.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.1 Darwin/23.5.0

File hashes

Hashes for easyword2num-0.1.3.tar.gz
Algorithm Hash digest
SHA256 90e37ae621ac0d88fbcbcc546606c2e3b83f7cf497249e14c449bdb8f775a646
MD5 b1548542c8e2b54d364e9396d9f1eb94
BLAKE2b-256 e68e9099139f99f7c66c9142e81362822e9c5d0b351b76075dc0b67322d90c51

See more details on using hashes here.

File details

Details for the file easyword2num-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: easyword2num-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.1 Darwin/23.5.0

File hashes

Hashes for easyword2num-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 88f190794acd33dcc7f97482022ba20fbde97d5c15f6b0d3941e18380be1e4b8
MD5 77d3d4443b53e9f82e53e0ac838a0d76
BLAKE2b-256 6ce425aeb73cd288e380b840cc0bc9541a3ad98c5a6144ee4384edeab7465367

See more details on using hashes here.

Supported by

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