#nlp #numbers #language #natural #digits #english #spanish

text2num

Parse and convert numbers written in English, Dutch, Spanish, German, Italian or French into their digit representation

15 stable releases

2.5.2 Oct 22, 2024
2.4.1 Jul 18, 2024
2.1.4 Aug 29, 2023
2.1.3 Jun 15, 2023
1.7.0 Dec 6, 2021

#249 in Text processing

Download history 8/week @ 2024-07-27 98/week @ 2024-08-17 161/week @ 2024-08-24 28/week @ 2024-08-31 1/week @ 2024-09-07 34/week @ 2024-09-14 17/week @ 2024-09-21 87/week @ 2024-09-28 22/week @ 2024-10-05 15/week @ 2024-10-12 150/week @ 2024-10-19 22/week @ 2024-10-26

173 downloads per month

MIT license

175KB
4K SLoC

Parse and convert numbers written in English, Dutch, Spanish, German, Italian or French into their digit representation.

This crate provides a library for recognizing, parsing and transcribing into digits (base 10) numbers expressed in natural language.

Examples

check some string is a valid number in a given language

use text2num::{Language, text2digits};

let es = Language::spanish();
let utterance = "ochenta y cinco";

match text2digits(utterance, &es) {
    Ok(repr) => println!("'{}' means {} in Spanish", utterance, repr),
    Err(_) => println!("'{}' is not a number in Spanish", utterance)
}

When run, the above code should print 'ochenta y cinco' means 85 in Spanish on the standard output.

find and replace numbers in a natural speech string

Most often, you just want to rewrite a string containing natural speech so that the numbers it contains (cardinals, ordinals, decimal numbers) appear in digit (base 10) form instead.

As isolated smaller numbers may be easier to read in plain text, you can specify a threshold under which isolated simple cardinals and ordinals are not replaced.

use text2num::{Language, replace_numbers};

let en = Language::english();

let sentence = "Let me show you two things: first, isolated numbers are treated differently than groups like one, two, three. And then, that decimal numbers like three point one four one five are well understood.";

assert_eq!(
    replace_numbers(sentence, &en, 10.0),
    "Let me show you two things: first, isolated numbers are treated differently than groups like 1, 2, 3. And then, that decimal numbers like 3.1415 are well understood."
);

assert_eq!(
    replace_numbers(sentence, &en, 0.0),
    "Let me show you 2 things: 1st, isolated numbers are treated differently than groups like 1, 2, 3. And then, that decimal numbers like 3.1415 are well understood."
);

For more advances usages (e.g. on token streams), see the documentation.

Dependencies

~3.5MB
~66K SLoC