Natural Language to Number Converter
text2num (v3.0.1) is an actively maintained Python library designed to parse and convert numbers expressed in natural language words from French, Spanish, English, Portuguese, German, Dutch, or Italian into their digit representations. It handles both integer parsing (`text2num`) and detection/transcription of cardinal, ordinal, and decimal numbers within a text stream (`alpha2digit`). The library is regularly updated and recently underwent a significant rewrite with a Rust backend.
Common errors
-
ImportError: cannot import name 'text2num' from 'text2num'
cause Attempting to import from the PyPI package name (`text2num`) instead of the actual module name (`text_to_num`).fixChange your import statement to `from text_to_num import text2num` or `from text_to_num import alpha2digit`. -
ValueError: invalid literal for text2num: 'thousand thousand and two hundreds'
cause The input string does not represent a valid single number for parsing by `text2num`, or contains ambiguous/malformed numerical phrases.fixEnsure the input to `text2num` is a well-formed phrase representing a single number. For complex sentences with multiple numbers or non-standard phrasing, use `alpha2digit` which is designed for broader text transcription, or pre-process the input to isolate valid number phrases.
Warnings
- breaking Version 3.0.0 was a complete rewrite, transitioning from a pure Python implementation (v2.x) to a Rust backend. This introduced several backward incompatible changes.
- breaking Support for signed numbers was dropped in version 3.x, as the feature was previously broken. The library now focuses on positive numbers.
- breaking The signature of the `alpha2digit` function changed in v3.x; the `threshold` optional parameter now applies to both ordinals and cardinals, instead of separate parameters.
- gotcha The Russian and Catalan languages, which were supported in previous 2.x versions, have not yet been fully ported to the 3.x Rust-based backend.
Install
-
pip install text2num
Imports
- text2num
from text2num import text2num
from text_to_num import text2num
- alpha2digit
from text2num import alpha2digit
from text_to_num import alpha2digit
Quickstart
from text_to_num import text2num, alpha2digit
# Convert a number expressed in words to its digit representation
num_en = text2num("fifty-one million five hundred seventy-eight thousand three hundred two", "en")
print(f"English number: {num_en}") # Expected: 51578302
num_fr = text2num('quatre-vingt-quinze', "fr")
print(f"French number: {num_fr}") # Expected: 95
# Find and transcribe all numbers (cardinals, ordinals, decimals) in a text
text_en = "On May twenty-third, I bought twenty-five cows, twelve chickens and one hundred twenty five point five kg of potatoes."
alphad_en = alpha2digit(text_en, "en")
print(f"Processed English text: {alphad_en}") # Expected: 'On May 23rd, I bought 25 cows, 12 chickens and 125.5 kg of potatoes.'
text_es = "Compramos veinticinco vacas, doce gallinas y ciento veinticinco coma cuarenta kg de patatas."
alphad_es = alpha2digit(text_es, "es")
print(f"Processed Spanish text: {alphad_es}") # Expected: 'Compramos 25 vacas, 12 gallinas y 125,40 kg de patatas.'