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.2.tar.gz (7.3 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.2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: easyword2num-0.1.2.tar.gz
  • Upload date:
  • Size: 7.3 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.2.tar.gz
Algorithm Hash digest
SHA256 f5d4d13a6768aa151e113e8e870f6981e99e3730f85089ecf0a057b1a0a0571f
MD5 c12aaac127c6ddb3d2836b75b290aa5b
BLAKE2b-256 d2eab2b14e85d491680843912ecfc60e40f7701bcd309548331fc2c91dc6b72f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: easyword2num-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ae66151bc2386c2f7dfad50bdb812fd403a6fd0a4c9f6e2bfafc0be5a9fc0638
MD5 75a414127fc6f33f5bf4cf4f7fa25df4
BLAKE2b-256 d0fb39c44ca92a8f9be97d38d77aa363adf513a1c1c2915f7a216053e92dd6dc

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