[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,234 @@
|
||||
//! Czech: spelling is nearly phonemic. Rules: digraphs (ch, dž, ou, au, eu, ej), palatals (ď ť ň, and d t n before
|
||||
//! i/í/ě), ě after labials (bě → bje, mě → mɲe), ř and its voiceless form, n before k/g, regressive voicing
|
||||
//! assimilation and final devoicing, syllabic r and l, stress on the first syllable. Symbols follow espeak-ng `cs`.
|
||||
|
||||
/// One phoneme and whether it is a vowel nucleus.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct Ph {
|
||||
s: String,
|
||||
vowel: bool,
|
||||
}
|
||||
|
||||
fn v(s: &str) -> Ph {
|
||||
Ph { s: s.to_owned(), vowel: true }
|
||||
}
|
||||
fn c(s: &str) -> Ph {
|
||||
Ph { s: s.to_owned(), vowel: false }
|
||||
}
|
||||
|
||||
const R_HACEK: &str = "r\u{31D}";
|
||||
const R_HACEK_VOICELESS: &str = "r\u{31D}\u{30A}";
|
||||
|
||||
/// Short words read without stress (prepositions and the like lean on the next word).
|
||||
const CLITICS: [&str; 16] = ["a", "i", "v", "ve", "s", "se", "z", "ze", "k", "ke", "o", "u", "si", "jsem", "jsi", "že"];
|
||||
|
||||
fn letters(w: &str) -> Vec<Ph> {
|
||||
let ch: Vec<char> = w.chars().collect();
|
||||
let mut out = Vec::new();
|
||||
let mut i = 0;
|
||||
let at = |k: usize| ch.get(k).copied();
|
||||
while i < ch.len() {
|
||||
let x = ch[i];
|
||||
let next = at(i + 1);
|
||||
let after = at(i + 2);
|
||||
// Two-letter units.
|
||||
match (x, next) {
|
||||
('c', Some('h')) => {
|
||||
out.push(c("x"));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
('d', Some('ž')) => {
|
||||
out.push(c("dʒ"));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
('o', Some('u')) => {
|
||||
out.push(v("oʊ"));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
('a', Some('u')) => {
|
||||
out.push(v("aʊ"));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
('e', Some('u')) => {
|
||||
out.push(v("eʊ"));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
('e', Some('j')) if after.is_none_or(|a| !"aáeéěiíoóuúůyý".contains(a)) => {
|
||||
out.push(v("eɪ"));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
// d t n before i í ě: ď ť ň.
|
||||
('d' | 't' | 'n', Some('i' | 'í' | 'ě')) => {
|
||||
out.push(c(match x {
|
||||
'd' => "ɟ",
|
||||
't' => "c",
|
||||
_ => "ɲ",
|
||||
}));
|
||||
out.push(match next {
|
||||
Some('í') => v("iː"),
|
||||
Some('ě') => v("e"),
|
||||
_ => v("i"),
|
||||
});
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
// bě pě vě fě: b je; mě: m ɲe.
|
||||
('b' | 'p' | 'v' | 'f', Some('ě')) => {
|
||||
out.push(c(&x.to_string()));
|
||||
out.push(c("j"));
|
||||
out.push(v("e"));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
('m', Some('ě')) => {
|
||||
out.push(c("m"));
|
||||
out.push(c("ɲ"));
|
||||
out.push(v("e"));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let p = match x {
|
||||
'a' => v("a"),
|
||||
'á' => v("aː"),
|
||||
'e' | 'ě' => v("e"),
|
||||
'é' => v("eː"),
|
||||
'i' | 'y' => v("i"),
|
||||
'í' | 'ý' => v("iː"),
|
||||
'o' => v("o"),
|
||||
'ó' => v("oː"),
|
||||
'u' => v("u"),
|
||||
'ú' | 'ů' => v("uː"),
|
||||
'c' => c("ts"),
|
||||
'č' => c("tʃ"),
|
||||
'ř' => c(R_HACEK),
|
||||
'š' => c("ʃ"),
|
||||
'ž' => c("ʒ"),
|
||||
'ť' => c("c"),
|
||||
'ď' => c("ɟ"),
|
||||
'ň' => c("ɲ"),
|
||||
'g' => c("ɡ"),
|
||||
'q' => {
|
||||
out.push(c("k"));
|
||||
c("v")
|
||||
}
|
||||
'w' => c("v"),
|
||||
'x' => {
|
||||
out.push(c("k"));
|
||||
c("s")
|
||||
}
|
||||
'n' if matches!(next, Some('k' | 'g')) => c("ŋ"),
|
||||
x if x.is_alphabetic() => c(&x.to_string()),
|
||||
_ => {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
out.push(p);
|
||||
i += 1;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Voiced ↔ voiceless obstruents.
|
||||
const PAIRS: [(&str, &str); 11] = [
|
||||
("b", "p"),
|
||||
("d", "t"),
|
||||
("ɟ", "c"),
|
||||
("ɡ", "k"),
|
||||
("v", "f"),
|
||||
("z", "s"),
|
||||
("ʒ", "ʃ"),
|
||||
("h", "x"),
|
||||
("dʒ", "tʃ"),
|
||||
("dz", "ts"),
|
||||
(R_HACEK, R_HACEK_VOICELESS),
|
||||
];
|
||||
|
||||
fn voiced(s: &str) -> Option<bool> {
|
||||
PAIRS.iter().find_map(|(a, b)| {
|
||||
if s == *a {
|
||||
Some(true)
|
||||
} else if s == *b {
|
||||
Some(false)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn with_voice(s: &str, on: bool) -> String {
|
||||
PAIRS
|
||||
.iter()
|
||||
.find_map(|(a, b)| if s == *a || s == *b { Some(if on { *a } else { *b }) } else { None })
|
||||
.unwrap_or(s)
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
fn assimilate(p: &mut [Ph]) {
|
||||
// Right to left: the end of the word is voiceless; an obstruent takes the voicing of the obstruent after it
|
||||
// (v and ř never pass their voicing on). ř is voiceless after a voiceless consonant.
|
||||
let n = p.len();
|
||||
for i in (0..n).rev() {
|
||||
if p[i].vowel || voiced(&p[i].s).is_none() {
|
||||
continue;
|
||||
}
|
||||
let next = p.get(i + 1).filter(|q| !q.vowel);
|
||||
let target = match next {
|
||||
// A one-consonant preposition (v, z, k, s) takes the next word's voicing, which one word cannot know.
|
||||
None if i + 1 == n && n == 1 => None,
|
||||
None if i + 1 == n => Some(false),
|
||||
Some(q) if q.s != "v" && q.s != R_HACEK && q.s != R_HACEK_VOICELESS => voiced(&q.s),
|
||||
_ => None,
|
||||
};
|
||||
if p[i].s == R_HACEK || p[i].s == R_HACEK_VOICELESS {
|
||||
let after_voiceless = i > 0 && voiced(&p[i - 1].s) == Some(false);
|
||||
if after_voiceless || target == Some(false) {
|
||||
p[i].s = R_HACEK_VOICELESS.to_owned();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(t) = target {
|
||||
p[i].s = with_voice(&p[i].s, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn syllabic(p: &mut [Ph]) {
|
||||
// r or l with no vowel on either side is the nucleus of its syllable (vlk, krk, Brno).
|
||||
let n = p.len();
|
||||
for i in 0..n {
|
||||
if (p[i].s == "r" || p[i].s == "l") && !p[i].vowel {
|
||||
let left = i == 0 || !p[i - 1].vowel;
|
||||
let right = i + 1 == n || !p[i + 1].vowel;
|
||||
if left && right && i > 0 {
|
||||
p[i].s.push('\u{329}');
|
||||
p[i].vowel = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn word(w: &str) -> String {
|
||||
let mut p = letters(w);
|
||||
assimilate(&mut p);
|
||||
syllabic(&mut p);
|
||||
let stressed = !CLITICS.contains(&w);
|
||||
let mut out = String::new();
|
||||
let mut done = false;
|
||||
for ph in &p {
|
||||
if ph.vowel && !done && stressed {
|
||||
out.push(super::STRESS);
|
||||
done = true;
|
||||
}
|
||||
out.push_str(&ph.s);
|
||||
}
|
||||
out
|
||||
}
|
||||
Reference in New Issue
Block a user