[lang] Read-aloud: our own G2P in espeak-ng's conventions (Czech, German, English with CMUdict), numbers as words, the voices' tokens; Lang::Cs

This commit is contained in:
2026-11-18 17:30:19 +01:00
parent 388e8e01c4
commit 8f43cb2c4c
14 changed files with 1272 additions and 7 deletions
+38
View File
@@ -0,0 +1,38 @@
# Czech words and the IPA espeak-ng conventions give them (stress before the first vowel), written for this repository.
dobrý dˈobriː
den dˈen
jak jˈak
máte mˈaːte
řeka r̝ˈeka
tři tr̝̊ˈi
moře mˈor̝e
dítě ɟˈiːce
něco ɲˈetso
čtyři tʃtˈir̝i
chleba xlˈeba
hrad hrˈat
kde ɡdˈe
vlk vˈl̩k
krk kˈr̩k
pouze pˈoʊze
auto ˈaʊto
mě mɲˈe
pět pjˈet
banka bˈaŋka
ještě jˈeʃce
kůň kˈuːɲ
džus dʒˈus
nejlepší nˈeɪlepʃiː
vůz vˈuːs
léto lˈeːto
odpověď ˈotpovjec
kniha kɲˈiha
otec ˈotets
matka mˈatka
dívka ɟˈiːfka
sníh sɲˈiːx
lidé lˈideː
Praha prˈaha
v v
a a
že ʒe
1 # Czech words and the IPA espeak-ng conventions give them (stress before the first vowel), written for this repository.
2 dobrý dˈobriː
3 den dˈen
4 jak jˈak
5 máte mˈaːte
6 řeka r̝ˈeka
7 tři tr̝̊ˈi
8 moře mˈor̝e
9 dítě ɟˈiːce
10 něco ɲˈetso
11 čtyři tʃtˈir̝i
12 chleba xlˈeba
13 hrad hrˈat
14 kde ɡdˈe
15 vlk vˈl̩k
16 krk kˈr̩k
17 pouze pˈoʊze
18 auto ˈaʊto
19 mě mɲˈe
20 pět pjˈet
21 banka bˈaŋka
22 ještě jˈeʃce
23 kůň kˈuːɲ
24 džus dʒˈus
25 nejlepší nˈeɪlepʃiː
26 vůz vˈuːs
27 léto lˈeːto
28 odpověď ˈotpovjec
29 kniha kɲˈiha
30 otec ˈotets
31 matka mˈatka
32 dívka ɟˈiːfka
33 sníh sɲˈiːx
34 lidé lˈideː
35 Praha prˈaha
36 v v
37 a a
38 že ʒe
+31
View File
@@ -0,0 +1,31 @@
# German words and the IPA espeak-ng conventions give them (stress before the stressed vowel), written for this repository.
haus hˈaʊs
mann mˈan
vater fˈaːtɐ
kinder kˈɪndɐ
schule ʃˈuːlə
ich ˈɪç
nacht nˈaxt
bäcker bˈɛkɐ
tag tˈaːk
liebe lˈiːbə
zeit tsˈaɪt
freund fɾˈɔʏnt
stein ʃtˈaɪn
gehen ɡˈeːən
verstehen fɛɐʃtˈeːən
sprechen ʃpɾˈɛçən
über ˈyːbɐ
schön ʃˈøːn
können kˈœnən
bruder bɾˈuːdɐ
mit mˈɪt
das dˈas
wasser vˈasɐ
zug tsˈuːk
jahr jˈaːɐ
milch mˈɪlç
buch bˈuːx
kind kˈɪnt
fisch fˈɪʃ
morgen mˈɔɐɡən
1 # German words and the IPA espeak-ng conventions give them (stress before the stressed vowel), written for this repository.
2 haus hˈaʊs
3 mann mˈan
4 vater fˈaːtɐ
5 kinder kˈɪndɐ
6 schule ʃˈuːlə
7 ich ˈɪç
8 nacht nˈaxt
9 bäcker bˈɛkɐ
10 tag tˈaːk
11 liebe lˈiːbə
12 zeit tsˈaɪt
13 freund fɾˈɔʏnt
14 stein ʃtˈaɪn
15 gehen ɡˈeːən
16 verstehen fɛɐʃtˈeːən
17 sprechen ʃpɾˈɛçən
18 über ˈyːbɐ
19 schön ʃˈøːn
20 können kˈœnən
21 bruder bɾˈuːdɐ
22 mit mˈɪt
23 das dˈas
24 wasser vˈasɐ
25 zug tsˈuːk
26 jahr jˈaːɐ
27 milch mˈɪlç
28 buch bˈuːx
29 kind kˈɪnt
30 fisch fˈɪʃ
31 morgen mˈɔɐɡən
+119
View File
@@ -0,0 +1,119 @@
//! 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());
}
+1
View File
@@ -9,6 +9,7 @@ fn fixture(lang: Lang) -> Vec<(String, String)> {
Lang::En => include_str!("fixtures/lemma_en.tsv"),
Lang::De => include_str!("fixtures/lemma_de.tsv"),
Lang::La => include_str!("fixtures/lemma_la.tsv"),
Lang::Cs => "",
};
text.lines()
.map(|l| l.trim_end_matches('\r'))