Files
Liber-public/crates/liber-lang/src/g2p/de.rs
T

266 lines
10 KiB
Rust
Raw 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.
//! German: rules for the common cases. Stress on the first syllable, after an unstressed prefix (be-, ge-, ver- …)
//! or on -ieren/-tion/-ei; vowels long when doubled, before h, in an open syllable, or before a single final
//! consonant (except short function words); unstressed e → ə, final -er → ɐ, r after a vowel vocalised (ɐ);
//! ch → x after a/o/u/au, ç elsewhere; sch, st-/sp-, z, final devoicing, -ig → ɪç. Symbols follow espeak-ng `de`.
#[derive(Clone, Debug)]
struct Ph {
s: String,
vowel: bool,
}
const PREFIXES: [&str; 8] = ["be", "ge", "er", "ver", "zer", "ent", "emp", "miss"];
/// Monosyllables whose vowel is short before one final consonant.
const SHORT: [&str; 28] = [
"das", "des", "es", "ist", "mit", "in", "im", "um", "an", "am", "ab", "ob", "von", "man", "bis", "was", "hat",
"weg", "ich", "mich", "dich", "sich", "nicht", "doch", "noch", "auch", "und", "ins",
];
fn is_vowel_letter(c: char) -> bool {
"aeiouäöüy".contains(c)
}
pub fn word(w: &str) -> String {
let ch: Vec<char> = w.chars().collect();
let n = ch.len();
// Where the stressed vowel is: the index into `ch` of the first letter of the stressed nucleus.
let mut stress_from = 0;
for p in PREFIXES {
if w.starts_with(p) && n > p.len() + 3 && ch[p.chars().count()..].iter().any(|&c| is_vowel_letter(c)) {
stress_from = p.chars().count();
break;
}
}
let ends = |s: &str| w.ends_with(s);
if ends("ieren") && n > 5 {
stress_from = n - 5;
} else if (ends("tion") || ends("erei")) && n > 4 {
stress_from = n - 2;
}
let mut out: Vec<Ph> = Vec::new();
let mut stressed_at: Option<usize> = None;
let mut i = 0;
let short_word = SHORT.contains(&w);
while i < n {
let x = ch[i];
let at = |k: usize| ch.get(k).copied();
let rest: String = ch[i..].iter().collect();
let cons = |s: &str| Ph { s: s.to_owned(), vowel: false };
let vow = |s: &str| Ph { s: s.to_owned(), vowel: true };
let mark = |out: &Vec<Ph>, stressed_at: &mut Option<usize>| {
if stressed_at.is_none() && i >= stress_from {
*stressed_at = Some(out.len());
}
};
// Consonant clusters first.
let (ph, step): (Option<Vec<Ph>>, usize) = if rest.starts_with("tsch") {
(Some(vec![cons("tʃ")]), 4)
} else if rest.starts_with("sch") {
(Some(vec![cons("ʃ")]), 3)
} else if rest.starts_with("chs") {
(Some(vec![cons("k"), cons("s")]), 3)
} else if rest.starts_with("ch") {
let back = out.last().is_some_and(|p| {
p.vowel && matches!(p.s.as_str(), "a" | "aː" | "o" | "oː" | "ɔ" | "u" | "uː" | "ʊ" | "aʊ")
});
(Some(vec![cons(if back { "x" } else { "ç" })]), 2)
} else if rest.starts_with("ck") {
(Some(vec![cons("k")]), 2)
} else if rest.starts_with("tz") || rest.starts_with("ts") {
(Some(vec![cons("t"), cons("s")]), 2)
} else if rest.starts_with("ph") {
(Some(vec![cons("f")]), 2)
} else if rest.starts_with("th") {
(Some(vec![cons("t")]), 2)
} else if rest.starts_with("qu") {
(Some(vec![cons("k"), cons("v")]), 2)
} else if rest.starts_with("ng") {
(Some(vec![cons("ŋ")]), 2)
} else if rest.starts_with("nk") {
(Some(vec![cons("ŋ"), cons("k")]), 2)
} else if rest.starts_with("pf") {
(Some(vec![cons("p"), cons("f")]), 2)
} else if rest.starts_with("dt") {
(Some(vec![cons("t")]), 2)
} else if (i == 0 || i == stress_from) && (rest.starts_with("st") || rest.starts_with("sp")) {
(Some(vec![cons("ʃ"), cons(&ch[i + 1].to_string())]), 2)
} else if rest == "ig" && i > 0 {
mark(&out, &mut stressed_at);
(Some(vec![vow("ɪ"), cons("ç")]), 2)
} else {
(None, 0)
};
if let Some(ps) = ph {
out.extend(ps);
i += step;
continue;
}
// Diphthongs and long spellings.
let di = match (x, at(i + 1)) {
('e' | 'a', Some('i' | 'y')) => Some("aɪ"),
('e' | 'ä', Some('u')) => Some("ɔʏ"),
('a', Some('u')) => Some("aʊ"),
('i', Some('e')) if at(i + 2) != Some('r') || i + 3 < n => Some("iː"),
('a', Some('a')) => Some("aː"),
('e', Some('e')) => Some("eː"),
('o', Some('o')) => Some("oː"),
_ => None,
};
if let Some(d) = di {
mark(&out, &mut stressed_at);
if d == "ɔʏ" {
out.push(vow("ɔ"));
out.push(Ph { s: "ʏ".into(), vowel: false });
} else {
out.push(vow(d));
}
i += 2;
if at(i) == Some('h') && !at(i + 1).is_some_and(is_vowel_letter) {
i += 1;
}
continue;
}
if is_vowel_letter(x) {
mark(&out, &mut stressed_at);
let next = at(i + 1);
let after = at(i + 2);
let is_stressed = stressed_at == Some(out.len());
// Unstressed e in endings.
let tail: String = ch[i..].iter().collect();
if x == 'e'
&& !is_stressed
&& (tail == "e" || tail == "en" || tail == "em" || tail == "es" || tail == "el" || tail == "et")
{
out.push(vow("ə"));
i += 1;
continue;
}
if x == 'e' && !is_stressed && tail == "er" {
out.push(vow("ɐ"));
i += 2;
continue;
}
// h after a vowel is silent and makes it long (gehen, Jahr, ruhig).
let silent_h = next == Some('h');
let uch = x == 'u' && next == Some('c') && after == Some('h') && at(i + 3).is_none();
let open = next.is_some_and(|c| !is_vowel_letter(c) && c != 'h')
&& after.is_some_and(is_vowel_letter)
&& !matches!((next, after), (Some('c'), Some('h')));
let final_single = next.is_some_and(|c| !is_vowel_letter(c)) && after.is_none() && !short_word;
let at_end = next.is_none();
let long =
silent_h || uch || (open && is_stressed) || (final_single && is_stressed) || (at_end && is_stressed);
let s = match (x, long) {
('a', true) => "aː",
('a', false) => "a",
('e', true) => "eː",
('e', false) => "ɛ",
('i' | 'y', true) => "iː",
('i' | 'y', false) => "ɪ",
('o', true) => "oː",
('o', false) => "ɔ",
('u', true) => "uː",
('u', false) => "ʊ",
('ä', true) => "ɛː",
('ä', false) => "ɛ",
('ö', true) => "øː",
('ö', false) => "œ",
('ü', true) => "yː",
_ => "ʏ",
};
out.push(vow(s));
i += if silent_h { 2 } else { 1 };
continue;
}
// A doubled consonant is one sound (Mann, Wasser, können).
if i > 0 && ch[i - 1] == x && !is_vowel_letter(x) {
i += 1;
continue;
}
// Single consonants.
let prev_vowel = out.last().is_some_and(|p| p.vowel);
let next_vowel = at(i + 1).is_some_and(is_vowel_letter);
let p: Option<&str> = match x {
'r' if prev_vowel && !next_vowel => Some("ɐ"),
'r' if i == 0 => Some("r"),
'r' => Some("ɾ"),
's' if next_vowel
&& (i == 0
|| prev_vowel
|| out.last().is_some_and(|p| matches!(p.s.as_str(), "l" | "n" | "m" | "ɾ"))) =>
{
Some("z")
}
'ß' => Some("s"),
'z' => {
out.push(cons("t"));
Some("s")
}
'v' => Some("f"),
'w' => Some("v"),
'j' => Some("j"),
'g' => Some("ɡ"),
'c' if at(i + 1).is_some_and(|c| "eiäy".contains(c)) => {
out.push(cons("t"));
Some("s")
}
'c' => Some("k"),
'x' => {
out.push(cons("k"));
Some("s")
}
'h' if prev_vowel => None,
x if x.is_alphabetic() => {
out.push(cons(&x.to_string()));
None
}
_ => None,
};
if let Some(p) = p {
out.push(if p == "ɐ" { vow("ɐ") } else { cons(p) });
if p == "ɐ" {
// Vocalic r is not a syllable of its own.
out.last_mut().expect("pushed").vowel = false;
}
}
i += 1;
}
// Final devoicing: a voiced obstruent before a consonant or at the end.
let n = out.len();
for k in 0..n {
let last = k + 1 == n
|| !out[k + 1].vowel
&& out[k + 1].s != "ɾ"
&& out[k + 1].s != "l"
&& out[k + 1].s != "r"
&& out[k + 1].s != "j"
&& out[k + 1].s != "ʏ";
if last && !out[k].vowel {
let d = match out[k].s.as_str() {
"b" => Some("p"),
"d" => Some("t"),
"ɡ" => Some("k"),
"v" => Some("f"),
"z" => Some("s"),
_ => None,
};
if let Some(d) = d {
out[k].s = d.to_owned();
}
}
}
let mut s = String::new();
for (k, p) in out.iter().enumerate() {
if Some(k) == stressed_at && !short_word_unstressed(w) {
s.push(super::STRESS);
}
s.push_str(&p.s);
}
s
}
/// Articles and similar short words carry no stress mark.
fn short_word_unstressed(w: &str) -> bool {
matches!(w, "der" | "die" | "dem" | "den" | "ein" | "eine" | "einen" | "zu" | "und" | "oder" | "es" | "sie" | "er")
}