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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file easyword2num-0.1.1.tar.gz.
File metadata
- Download URL: easyword2num-0.1.1.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.1 Darwin/23.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4cd617fd2ae73e034787c4ce0e78215e50d0fb3edfd089a560aa3ab2cbeb6469
|
|
| MD5 |
64098d36ebe532d7b26d91f7d8df1dd8
|
|
| BLAKE2b-256 |
7249c052ce3e0467a33c96eaaac12f849d9408c3b175ea973b2cfc743e05bdca
|
File details
Details for the file easyword2num-0.1.1-py3-none-any.whl.
File metadata
- Download URL: easyword2num-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29bfc83012c9445bb9dc11475c1b2df1cca15c7e221576bf17403b5453cd281a
|
|
| MD5 |
cb133ac4b012a47187ab36e9bb2eb1d1
|
|
| BLAKE2b-256 |
084af396960d454cd5efffa7eb9a3e4e75d548a3ede9b14709ae65a8d4ec01b9
|