Natural Language to Number Converter

3.0.1 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

The `text2num` function converts a full number phrase into an integer, while `alpha2digit` finds and transcribes all numbers (cardinals, ordinals, decimals) within a longer text. Both require specifying the language code (e.g., 'en', 'fr', 'es').

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.'

view raw JSON →