garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /speech/src/numbers.rs

raw

//! Numbers as the words they are read with.
//!
//! English writes the parts of a number apart ("two thousand and
//! twenty-six", the "and" before the last part as British English says
//! it); Dutch writes everything below a million as one word, the units
//! before the tens ("zesentwintig", six-and-twenty) and "honderd" and
//! "duizend" without a "one" before them. Numbers from a billion up are
//! read a digit at a time, as are those written with a leading zero
//! (a code, "007"), so nothing a person types is read wrongly as a
//! quantity.

use alloc::string::String;

use crate::Language;

/// The largest number read as one quantity.
pub const LARGEST: u32 = 999_999_999;

const ONES: [&str; 20] = [
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    "ten",
    "eleven",
    "twelve",
    "thirteen",
    "fourteen",
    "fifteen",
    "sixteen",
    "seventeen",
    "eighteen",
    "nineteen",
];

const TENS: [&str; 10] = [
    "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety",
];

const EENHEDEN: [&str; 20] = [
    "nul",
    "een",
    "twee",
    "drie",
    "vier",
    "vijf",
    "zes",
    "zeven",
    "acht",
    "negen",
    "tien",
    "elf",
    "twaalf",
    "dertien",
    "veertien",
    "vijftien",
    "zestien",
    "zeventien",
    "achttien",
    "negentien",
];

const TIENTALLEN: [&str; 10] = [
    "", "", "twintig", "dertig", "veertig", "vijftig", "zestig", "zeventig", "tachtig", "negentig",
];

/// `n` in words; `n` above [`LARGEST`] is given digit by digit.
pub fn words(n: u32, language: Language) -> String {
    let mut out = String::new();
    if n > LARGEST {
        let mut digits = String::new();
        let mut rest = n;
        loop {
            digits.insert(0, char::from(b'0' + (rest % 10) as u8));
            rest /= 10;
            if rest == 0 {
                break;
            }
        }
        for (i, d) in digits.bytes().enumerate() {
            if i > 0 {
                out.push(' ');
            }
            out.push_str(digit(d, language));
        }
        return out;
    }
    match language {
        Language::English => english(n, &mut out),
        Language::Dutch => dutch(n, &mut out),
    }
    out
}

/// The word for one digit, `b'0'..=b'9'`.
pub fn digit(d: u8, language: Language) -> &'static str {
    let i = usize::from(d.saturating_sub(b'0').min(9));
    match language {
        Language::English => ONES[i],
        Language::Dutch => EENHEDEN[i],
    }
}

fn english(n: u32, out: &mut String) {
    let n = n as usize;
    if n >= 1_000_000 {
        english((n / 1_000_000) as u32, out);
        out.push_str(" million");
        english_rest((n % 1_000_000) as u32, out);
    } else if n >= 1000 {
        english((n / 1000) as u32, out);
        out.push_str(" thousand");
        english_rest((n % 1000) as u32, out);
    } else if n >= 100 {
        out.push_str(ONES[n / 100]);
        out.push_str(" hundred");
        english_rest((n % 100) as u32, out);
    } else if n >= 20 {
        out.push_str(TENS[n / 10]);
        if !n.is_multiple_of(10) {
            out.push('-');
            out.push_str(ONES[n % 10]);
        }
    } else {
        out.push_str(ONES[n]);
    }
}

/// What follows "thousand" or "hundred": "and" before a last part under a
/// hundred, as in "two thousand and six".
fn english_rest(rest: u32, out: &mut String) {
    if rest == 0 {
        return;
    }
    out.push_str(if rest < 100 { " and " } else { " " });
    english(rest, out);
}

fn dutch(n: u32, out: &mut String) {
    let n = n as usize;
    if n >= 1_000_000 {
        dutch((n / 1_000_000) as u32, out);
        out.push_str(" miljoen");
        if !n.is_multiple_of(1_000_000) {
            out.push(' ');
            dutch((n % 1_000_000) as u32, out);
        }
    } else if n >= 1000 {
        if n / 1000 > 1 {
            dutch((n / 1000) as u32, out);
        }
        out.push_str("duizend");
        if !n.is_multiple_of(1000) {
            dutch((n % 1000) as u32, out);
        }
    } else if n >= 100 {
        if n / 100 > 1 {
            out.push_str(EENHEDEN[n / 100]);
        }
        out.push_str("honderd");
        if !n.is_multiple_of(100) {
            dutch((n % 100) as u32, out);
        }
    } else if n >= 20 {
        let ones = n % 10;
        if ones != 0 {
            let unit = EENHEDEN[ones];
            out.push_str(unit);
            // "twee" + "en" meet as "tweeën": the diaeresis says the e of
            // "en" starts a syllable of its own.
            out.push_str(if unit.ends_with('e') { "ën" } else { "en" });
        }
        out.push_str(TIENTALLEN[n / 10]);
    } else {
        out.push_str(EENHEDEN[n]);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use Language::{Dutch, English};

    #[test]
    fn english_numbers() {
        assert_eq!(words(0, English), "zero");
        assert_eq!(words(7, English), "seven");
        assert_eq!(words(21, English), "twenty-one");
        assert_eq!(words(40, English), "forty");
        assert_eq!(words(100, English), "one hundred");
        assert_eq!(words(115, English), "one hundred and fifteen");
        assert_eq!(words(2026, English), "two thousand and twenty-six");
        assert_eq!(
            words(12_345, English),
            "twelve thousand three hundred and forty-five"
        );
        assert_eq!(words(1_000_000, English), "one million");
        assert_eq!(
            words(999_999_999, English),
            "nine hundred and ninety-nine million nine hundred and ninety-nine thousand \
             nine hundred and ninety-nine"
        );
        assert_eq!(
            words(1_234_567_890, English),
            "one two three four five six seven eight nine zero"
        );
    }

    #[test]
    fn dutch_numbers() {
        assert_eq!(words(0, Dutch), "nul");
        assert_eq!(words(13, Dutch), "dertien");
        assert_eq!(words(21, Dutch), "eenentwintig");
        assert_eq!(words(22, Dutch), "tweeëntwintig");
        assert_eq!(words(80, Dutch), "tachtig");
        assert_eq!(words(100, Dutch), "honderd");
        assert_eq!(words(101, Dutch), "honderdeen");
        assert_eq!(words(345, Dutch), "driehonderdvijfenveertig");
        assert_eq!(words(1000, Dutch), "duizend");
        assert_eq!(words(2026, Dutch), "tweeduizendzesentwintig");
        assert_eq!(words(1_000_000, Dutch), "een miljoen");
        assert_eq!(words(3_200_000, Dutch), "drie miljoen tweehonderdduizend");
    }
}