::vowel:: = ɐ|aː|ɛ|æː|e|eː|ɪ|iː|oː|ɔ|ʊ|uː|iɛ|uɔ
::consonant_hard:: = b|t͡s|t͡ʃ|d|f|ɡ|ɦ|k|ɫ|m|n|p|r|s|ʃ|t|v|z|ʒ|x|d͡z|d͡ʒ
::consonant_soft:: = ((b|t͡s|t͡ʃ|d|f|ɡ|ɦ|k|l|m|n|p|r|s|ʃ|t|v|z|ʒ|x|d͡z|d͡ʒ)ʲ|ɕ|ʑ|t͡ɕ|d͡ʑ)
%::consonant_soft:: = ((b|t͡s|d|f|ɡ|ɦ|k|l|m|n|p|r|s|t|v|z|x|d͡z)ʲ)|ɕ|ʑ|t͡ɕ|d͡ʑ
::consonant:: = (::consonant_hard::)|(::consonant_soft::)|j

% "According to their relations with hard and soft (palatalized) consonants, the vowels may be classified into two types:"
::vowel_u:: = uː|ʊ|uɔ|oː|ɔ|aː|ɐ
::vowel_i:: = iː|ɪ|iɛ|eː|e|æː|ɛ

::pause_punc:: = \.|,|:|;|\?|\!
::punc:: = \.|,|:|;|\'|\"|«|»|\u00BB|\?|\!|\(|\)\[|\]

% Affrication (heuristic: may not cover all cases; covered cases may not all apply)
ts -> t͡s / ((::vowel::)|y) _ ((::vowel::)|y)

% Degemination (conservative: only if exact same, and hardcoded only some consonants)
mm -> m / _
nn -> n / _
ss -> s / _
tt -> t / _
ʃʃ -> ʃ / _
rr -> r / _
ʒʒ -> ʒ / _
zz -> z / _

% Aspiration of the word final [t k] before a pause (sometimes also p, ignored here)
0 -> ʰ / [t|k] _ (::pause_punc::)

% Handle stressed final and non-final {a, e}
% NOTE: We naively assume that all stresses are explicitly marked with a tilde or grave or acute diacritic
ɐ(/|~|`) -> aː / _ ((::consonant::)+(::vowel::)+(::consonant::)?)
ɛ(/|~|`) -> æː / _ ((::consonant::)+(::vowel::)+(::consonant::)?)
ɐ(/|~|`) -> ɐ / _ (::consonant::)?(#|(::punc::))
ɛ(/|~|`) -> ɛ / _ (::consonant::)?(#|(::punc::))
% Not sure if correct:
ɐ(/|~|`) -> aː / _ (::consonant::){2,}|(::consonant::)ʰ
ɛ(/|~|`) -> æː / _ (::consonant::){2,}|(::consonant::)ʰ

% Velarization of /n/ before backlingual consonants
n -> ŋ / _ ɡ|k

% Vocalization of /j v/ at ends of words and before consonants
j -> ɪ / _ ((::consonant::)|#)
v -> w / _ ((::consonant::)|#)

% Consonant Palatalization
% =============================
% V^i vowel phonemes occur only after soft consonants and /j/
0 -> ʲ / (::consonant_hard::) _ (::consonant_soft::)*((::vowel_i::)|j)
0 -> ʲ / (::consonant_hard::) _ (::consonant_soft::)*((::vowel_i::)|j)
0 -> ʲ / (::consonant_hard::) _ (::consonant_soft::)*((::vowel_i::)|j)
% Naive repetition to handle multiple consonants...

% "After soft (palatalized) consonants [...] and /j/, all the back vowels are realized by their fronted (advanced) variants"
% TODO: handle iu -> u
(::vowel_i::)uɔ -> uɔ˖ / ((::consonant_soft::)|j) _
(::vowel_i::)uː -> uː˖ / ((::consonant_soft::)|j) _
(::vowel_i::)ʊ -> ʊ˖ / ((::consonant_soft::)|j) _
(::vowel_i::)oː -> oː˖ / ((::consonant_soft::)|j) _
% "The vowels /aː/ and /ɐ/ in this position [...] coincide with /æː/ and /ɛ/ respectively"
(::vowel_i::)aː -> æː˖ / ((::consonant_soft::)|j) _
(::vowel_i::)ɐ -> ɛ / ((::consonant_soft::)|j) _


% Reformatting soft consonants
ʃʲ -> ɕ / _
zʲ -> ʑ / _
t͡ʃʲ -> t͡ɕ / _
d͡zʲ -> d͡ʑ / _
ɫʲ -> lʲ / _

% Change /m mʲ/ into labiodentals before /v vʲ/ and /f fʲ/
mʲ -> ɱʲ / _ (v|f)
m -> ɱ / _ (v|f)
