[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:
@@ -0,0 +1,201 @@
|
||||
//! English: CMUdict's ARPABET → espeak-ng `en-us` IPA (stress before the vowel, ɜː/ɚ for stressed/unstressed ER,
|
||||
//! t flapped to ɾ between a vowel and an unstressed vowel), and letter-to-sound rules for words not in the
|
||||
//! dictionary (good enough for names and rare words, never the main path).
|
||||
|
||||
const VOWELS: [(&str, &str, &str); 15] = [
|
||||
// ARPABET, stressed, unstressed
|
||||
("AA", "ɑː", "ɑː"),
|
||||
("AE", "æ", "æ"),
|
||||
("AH", "ʌ", "ə"),
|
||||
("AO", "ɔː", "ɔː"),
|
||||
("AW", "aʊ", "aʊ"),
|
||||
("AY", "aɪ", "aɪ"),
|
||||
("EH", "ɛ", "ɛ"),
|
||||
("ER", "ɜː", "ɚ"),
|
||||
("EY", "eɪ", "eɪ"),
|
||||
("IH", "ɪ", "ɪ"),
|
||||
("IY", "iː", "i"),
|
||||
("OW", "oʊ", "oʊ"),
|
||||
("OY", "ɔɪ", "ɔɪ"),
|
||||
("UH", "ʊ", "ʊ"),
|
||||
("UW", "uː", "uː"),
|
||||
];
|
||||
|
||||
const CONSONANTS: [(&str, &str); 24] = [
|
||||
("B", "b"),
|
||||
("CH", "tʃ"),
|
||||
("D", "d"),
|
||||
("DH", "ð"),
|
||||
("F", "f"),
|
||||
("G", "ɡ"),
|
||||
("HH", "h"),
|
||||
("JH", "dʒ"),
|
||||
("K", "k"),
|
||||
("L", "l"),
|
||||
("M", "m"),
|
||||
("N", "n"),
|
||||
("NG", "ŋ"),
|
||||
("P", "p"),
|
||||
("R", "ɹ"),
|
||||
("S", "s"),
|
||||
("SH", "ʃ"),
|
||||
("T", "t"),
|
||||
("TH", "θ"),
|
||||
("V", "v"),
|
||||
("W", "w"),
|
||||
("Y", "j"),
|
||||
("Z", "z"),
|
||||
("ZH", "ʒ"),
|
||||
];
|
||||
|
||||
/// One ARPABET pronunciation in IPA; `None` for an unknown symbol.
|
||||
pub fn from_arpabet(arpa: &[&str]) -> Option<String> {
|
||||
// (ipa, is vowel, stress 0/1/2)
|
||||
let mut ph: Vec<(String, bool, u8)> = Vec::with_capacity(arpa.len());
|
||||
for a in arpa {
|
||||
let (base, stress) = match a.chars().last() {
|
||||
Some(d @ '0'..='2') => (&a[..a.len() - 1], d as u8 - b'0'),
|
||||
_ => (*a, 0),
|
||||
};
|
||||
if let Some((_, s, u)) = VOWELS.iter().find(|(k, _, _)| *k == base) {
|
||||
ph.push(((if stress > 0 { *s } else { *u }).to_owned(), true, stress));
|
||||
} else if let Some((_, c)) = CONSONANTS.iter().find(|(k, _)| *k == base) {
|
||||
ph.push(((*c).to_owned(), false, 0));
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
// Flapping: t (or d) after a vowel or r, before an unstressed vowel → ɾ (better, water, ladder).
|
||||
for i in 1..ph.len().saturating_sub(1) {
|
||||
if (ph[i].0 == "t" || ph[i].0 == "d") && (ph[i - 1].1 || ph[i - 1].0 == "ɹ") && ph[i + 1].1 && ph[i + 1].2 == 0
|
||||
{
|
||||
ph[i].0 = "ɾ".to_owned();
|
||||
}
|
||||
}
|
||||
let mut out = String::new();
|
||||
for (s, vowel, stress) in &ph {
|
||||
if *vowel {
|
||||
match stress {
|
||||
1 => out.push(super::STRESS),
|
||||
2 => out.push(super::STRESS2),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
out.push_str(s);
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// Letter-to-sound for words the dictionary does not have: common English spellings, stress on the first vowel.
|
||||
pub fn rules(w: &str) -> String {
|
||||
const UNITS: [(&str, &str); 38] = [
|
||||
("tion", "ʃən"),
|
||||
("sion", "ʒən"),
|
||||
("ough", "ʌf"),
|
||||
("igh", "aɪ"),
|
||||
("tch", "tʃ"),
|
||||
("sch", "sk"),
|
||||
("ee", "iː"),
|
||||
("ea", "iː"),
|
||||
("oo", "uː"),
|
||||
("ou", "aʊ"),
|
||||
("ow", "oʊ"),
|
||||
("oa", "oʊ"),
|
||||
("ai", "eɪ"),
|
||||
("ay", "eɪ"),
|
||||
("oi", "ɔɪ"),
|
||||
("oy", "ɔɪ"),
|
||||
("au", "ɔː"),
|
||||
("aw", "ɔː"),
|
||||
("ar", "ɑːɹ"),
|
||||
("or", "ɔːɹ"),
|
||||
("er", "ɚ"),
|
||||
("ir", "ɜː"),
|
||||
("ur", "ɜː"),
|
||||
("ch", "tʃ"),
|
||||
("sh", "ʃ"),
|
||||
("th", "θ"),
|
||||
("ph", "f"),
|
||||
("wh", "w"),
|
||||
("ck", "k"),
|
||||
("ng", "ŋ"),
|
||||
("qu", "kw"),
|
||||
("kn", "n"),
|
||||
("wr", "ɹ"),
|
||||
("gh", ""),
|
||||
("ll", "l"),
|
||||
("ss", "s"),
|
||||
("tt", "t"),
|
||||
("nn", "n"),
|
||||
];
|
||||
let ch: Vec<char> = w.chars().filter(|c| c.is_alphabetic() || *c == '\'').collect();
|
||||
let s: String = ch.iter().collect();
|
||||
let mut out = String::new();
|
||||
let mut stressed = false;
|
||||
let mut i = 0;
|
||||
let vowel_ipa = |c: char, next: Option<char>, after: Option<char>| -> Option<&'static str> {
|
||||
// A vowel before one consonant and a final e is long (make, time, home, cute).
|
||||
let magic = next.is_some_and(|n| !"aeiou".contains(n)) && after == Some('e');
|
||||
Some(match (c, magic) {
|
||||
('a', true) => "eɪ",
|
||||
('a', false) => "æ",
|
||||
('e', _) => "ɛ",
|
||||
('i', true) => "aɪ",
|
||||
('i', false) => "ɪ",
|
||||
('o', true) => "oʊ",
|
||||
('o', false) => "ɑː",
|
||||
('u', true) => "juː",
|
||||
('u', false) => "ʌ",
|
||||
('y', _) => "i",
|
||||
_ => return None,
|
||||
})
|
||||
};
|
||||
while i < ch.len() {
|
||||
let rest = &s[s.char_indices().nth(i).map_or(s.len(), |(b, _)| b)..];
|
||||
if let Some((k, v)) = UNITS.iter().find(|(k, _)| rest.starts_with(k)) {
|
||||
let is_vowel = v.starts_with(|c: char| "iːuːaʊoʊeɪɔɪɑɚɜʌæɛɪəʊ".contains(c));
|
||||
if is_vowel && !stressed {
|
||||
out.push(super::STRESS);
|
||||
stressed = true;
|
||||
}
|
||||
out.push_str(v);
|
||||
i += k.chars().count();
|
||||
continue;
|
||||
}
|
||||
let c = ch[i];
|
||||
let next = ch.get(i + 1).copied();
|
||||
let after = ch.get(i + 2).copied();
|
||||
// A final e after a consonant is silent.
|
||||
if c == 'e' && next.is_none() && i > 0 {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if "aeiouy".contains(c) && !(c == 'y' && i == 0) {
|
||||
if let Some(v) = vowel_ipa(c, next, after) {
|
||||
if !stressed {
|
||||
out.push(super::STRESS);
|
||||
stressed = true;
|
||||
}
|
||||
out.push_str(v);
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
let sound: String = match c {
|
||||
'c' if next.is_some_and(|n| "eiy".contains(n)) => "s".into(),
|
||||
'c' => "k".into(),
|
||||
'g' if next.is_some_and(|n| "eiy".contains(n)) => "dʒ".into(),
|
||||
'g' => "ɡ".into(),
|
||||
'j' => "dʒ".into(),
|
||||
'q' => "k".into(),
|
||||
'r' => "ɹ".into(),
|
||||
'x' => "ks".into(),
|
||||
'y' => "j".into(),
|
||||
c if c.is_ascii_alphabetic() => c.to_string(),
|
||||
_ => String::new(),
|
||||
};
|
||||
out.push_str(&sound);
|
||||
i += 1;
|
||||
}
|
||||
out
|
||||
}
|
||||
Reference in New Issue
Block a user