Files

120 lines
5.1 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Day 4: text → phonemes for the reading voices. Gold pronunciations written for this repository in espeak-ng's
//! conventions (the voices were trained on them); the accuracy per language is stated in the app's
//! `docs/04_listening.md` and held here as a floor.
use liber_lang::Lang;
use liber_lang::g2p::{Lexicon, en, ipa, num, tokens, word};
fn fixture(text: &str) -> Vec<(String, String)> {
text.lines()
.map(|l| l.trim_end_matches('\r'))
.filter(|l| !l.is_empty() && !l.starts_with('#'))
.map(|l| {
let (w, p) = l.split_once('\t').expect("word<TAB>ipa");
(w.to_owned(), p.to_owned())
})
.collect()
}
fn accuracy(lang: Lang, cases: &[(String, String)]) -> (usize, Vec<String>) {
let mut wrong = Vec::new();
let ok = cases
.iter()
.filter(|(w, want)| {
let got = word(w, lang, None);
let hit = got == *want;
if !hit {
wrong.push(format!("{w} → {got} (want {want})"));
}
hit
})
.count();
(ok, wrong)
}
#[test]
fn czech_words() {
let cases = fixture(include_str!("fixtures/g2p_cs.tsv"));
let (ok, wrong) = accuracy(Lang::Cs, &cases);
println!("cs: {ok}/{} — {}", cases.len(), wrong.join("; "));
assert!(ok as f64 / cases.len() as f64 >= 0.95, "{wrong:?}");
}
#[test]
fn german_words() {
let cases = fixture(include_str!("fixtures/g2p_de.tsv"));
let (ok, wrong) = accuracy(Lang::De, &cases);
println!("de: {ok}/{} — {}", cases.len(), wrong.join("; "));
assert!(ok as f64 / cases.len() as f64 >= 0.9, "{wrong:?}");
}
#[test]
fn english_from_cmudict_and_rules() {
// CMUdict lines (the format the app reads; the words' ARPABET is common knowledge, not the file).
let lex = Lexicon::from_cmudict(
";;; a comment\nHELLO HH AH0 L OW1\nHELLO(2) HH EH0 L OW1\nWORLD W ER1 L D\nBETTER B EH1 T ER0\nTHE DH AH0\nMOTHER M AH1 DH ER0\nFATHER F AA1 DH ER0\nCAT K AE1 T\nTHINKING TH IH1 NG K IH0 NG\nBOY B OY1\nTOMORROW T AH0 M AA1 R OW2\nBAD B AE1 D XX9\n",
)
.expect("parses");
let cases = [
("hello", "həlˈoʊ"),
("world", "wˈɜːld"),
("better", "bˈɛɾɚ"),
("the", "ðə"),
("mother", "mˈʌðɚ"),
("father", "fˈɑːðɚ"),
("cat", "kˈæt"),
("thinking", "θˈɪŋkɪŋ"),
("boy", "bˈɔɪ"),
("tomorrow", "təmˈɑːɹˌoʊ"),
];
for (w, want) in cases {
assert_eq!(word(w, Lang::En, Some(&lex)), want, "{w}");
}
assert!(lex.get("bad").is_none(), "an unknown ARPABET symbol drops the entry");
assert_eq!(lex.len(), 10, "alternates and bad lines skipped");
// Words not in the dictionary still get a reading: stressed, from the voice's symbols.
let inventory = "ˈˌːabdefhijklmnoprstuvwzæðŋɐɑɔəɚɛɜɡɪɹʃʊʌʒθɾ";
for w in ["make", "night", "station", "zork", "liber"] {
let r = en::rules(w);
assert!(r.starts_with(|c: char| c != ' ') && r.contains('ˈ'), "{w} → {r}");
assert!(r.chars().all(|c| inventory.contains(c)), "{w} → {r}");
}
assert_eq!(en::rules("make"), "mˈeɪk");
assert_eq!(en::rules("night"), "nˈaɪt");
}
#[test]
fn sentences_numbers_and_punctuation() {
assert_eq!(ipa("Dobrý den, jak se máte?", Lang::Cs, None), "dˈobriː dˈen, jˈak se mˈaːte?");
assert_eq!(ipa("Tři kočky.", Lang::Cs, None), "tr̝̊ˈi kˈotʃki.");
// Numbers are read as words in the sentence's language.
assert_eq!(num::words(1847, Lang::En).as_deref(), Some("eighteen forty seven"));
assert_eq!(num::words(1847, Lang::De).as_deref(), Some("achtzehnhundertsiebenundvierzig"));
assert_eq!(num::words(1847, Lang::Cs).as_deref(), Some("tisíc osm set čtyřicet sedm"));
assert_eq!(num::words(21, Lang::De).as_deref(), Some("einundzwanzig"));
assert_eq!(num::words(2_000_000_000, Lang::En), None);
assert!(ipa("V roce 1847 koupil mlýn.", Lang::Cs, None).contains("cˈisiːts"), "tisíc: ti is ť");
// Quotes and dashes never become sounds; a dash is a pause.
assert_eq!(ipa("»Ano« – řekl.", Lang::Cs, None), "ˈano, r̝ˈekl̩.");
}
#[test]
fn tokens_merge_the_voices_clusters() {
let clusters: Vec<(String, String)> = [("a", "ɪ"), ("a", "ʊ"), ("e", "ɪ"), ("o", "ʊ"), ("ɔ", "ɪ")]
.iter()
.map(|(a, b)| ((*a).to_owned(), (*b).to_owned()))
.collect();
assert_eq!(tokens("hˈaʊs", &clusters), ["h", "ˈ", "aʊ", "s"]);
// ř is r + a combining mark (and a second for the voiceless one): separate symbols, as the voice learned them.
assert_eq!(tokens("tr̝̊ˈi", &clusters), ["t", "r", "\u{31D}", "\u{30A}", "ˈ", "i"]);
// ç decomposes (NFD) to c + combining cedilla, the way the German voice was trained.
assert_eq!(tokens("ˈɪç", &clusters), ["ˈ", "ɪ", "c", "\u{327}"]);
}
#[test]
fn hostile_dictionaries_are_refused() {
assert!(Lexicon::from_cmudict(&"A AH0\n".repeat(3_000_000)).is_none(), "over the byte cap");
let odd = Lexicon::from_cmudict("WORD\n\n \nX \u{0}\u{0}\n").expect("parses");
assert!(odd.is_empty());
}