use super::{has_char_n, next_region, strip_in};
use crate::lang::LangHint;
const MAX_SHORT: usize = 3;
const MIN_R1: usize = 3;
const MIN_R1_TR: usize = 2;
const DE_1: &[&str] = &["ern", "em", "er", "es", "en", "e"];
const DE_2: &[&str] = &["est", "er", "en"];
const DE_3: &[&str] = &["isch", "keit", "heit", "lich", "ung", "ig", "ik", "end"];
const DE_ST: &str = "st";
const DE_ST_STEM: usize = 4;
const FR_STD: &[&str] = &[
"issements",
"issement",
"atrices",
"ateurs",
"ations",
"atrice",
"ateur",
"ation",
"logies",
"logie",
"usions",
"utions",
"usion",
"ution",
"ences",
"ence",
"ments",
"ment",
"euses",
"euse",
"ites",
"ite",
"ifs",
"ives",
"if",
"ive",
"eaux",
"aux",
"aises",
"aise",
];
const FR_VERB: &[&str] = &[
"issions", "issons", "issent", "iront", "irent", "eront", "erent", "era", "ez", "er",
];
const FR_END: &[&str] = &["es", "e", "s"];
const ROMANCE: &[&str] = &[
"amientos", "imientos", "amiento", "imiento", "aciones", "uciones", "adores", "adoras", "mente",
"ances", "anzas", "ables", "ibles", "istas", "ismos", "adora", "acion", "ucion", "iendo", "ance",
"anza", "able", "ible", "ista", "ismo", "ando", "endo", "ador", "ar", "er", "ir", "as", "es",
"os", "a", "e", "o", "i",
];
const PT: &[&str] = &[
"amentos", "imentos", "amento", "imento", "adores", "adoras", "acoes", "ucoes", "aveis", "iveis",
"mente", "istas", "ismos", "adora", "acao", "ucao", "avel", "ivel", "coes", "ista", "ismo",
"ando", "endo", "ador", "cao", "oes", "ar", "er", "ir", "ao", "as", "es", "os", "a", "e", "o",
];
const NL: &[&str] = &[
"heden", "heid", "eren", "ing", "ig", "lijk", "baar", "en", "e",
];
const SV: &[&str] = &[
"heten", "het", "elser", "else", "ande", "ende", "arna", "erna", "orna", "aren", "are", "ast",
"en", "er", "ar", "at", "s", "e", "a",
];
const TR_CASE: &[&str] = &[
"den", "dan", "ten", "tan", "nin", "nun", "nda", "nde", "da", "de", "ta", "te", "in", "un", "i",
"u",
];
const TR_PLURAL: &[&str] = &["ler", "lar"];
const fn is_vowel(ch: char) -> bool {
matches!(ch, 'a' | 'e' | 'i' | 'o' | 'u' | 'y')
}
const fn is_de_s_end(ch: char) -> bool {
matches!(
ch,
'b' | 'd' | 'f' | 'g' | 'h' | 'k' | 'l' | 'm' | 'n' | 'r' | 't'
)
}
const fn is_de_st_end(ch: char) -> bool {
matches!(
ch,
'b' | 'd' | 'f' | 'g' | 'h' | 'k' | 'l' | 'm' | 'n' | 't'
)
}
fn r1_r2(word: &str, min_r1: usize) -> (usize, usize) {
let min = word
.char_indices()
.nth(min_r1)
.map_or(word.len(), |(i, _)| i);
let r1 = next_region(word, 0, is_vowel).max(min);
(r1, next_region(word, r1, is_vowel))
}
pub(crate) fn stem(lang: LangHint, word: &str, out: &mut String) -> bool {
if !matches!(
lang,
LangHint::De
| LangHint::Fr
| LangHint::Es
| LangHint::Pt
| LangHint::It
| LangHint::Nl
| LangHint::Sv
| LangHint::Tr
) {
return false;
}
out.clear();
out.push_str(word);
if !has_char_n(word, MAX_SHORT + 1) {
return true;
}
let min_r1 = if lang == LangHint::Tr {
MIN_R1_TR
} else {
MIN_R1
};
let (r1, r2) = r1_r2(word, min_r1);
match lang {
LangHint::De => {
if !strip_in(out, r1, DE_1)
&& out.len() > r1
&& let Some(stem) = out.strip_suffix('s')
&& stem.ends_with(is_de_s_end)
{
out.pop();
}
if !strip_in(out, r1, DE_2)
&& out.len() >= r1 + DE_ST.len()
&& let Some(stem) = out.strip_suffix(DE_ST)
&& stem.ends_with(is_de_st_end)
&& has_char_n(stem, DE_ST_STEM)
{
out.truncate(stem.len());
}
strip_in(out, r2, DE_3);
}
LangHint::Fr => {
if !strip_in(out, r2, FR_STD) && !strip_in(out, r1, FR_VERB) {
strip_in(out, r1, FR_END);
}
}
LangHint::Es | LangHint::It => {
strip_in(out, r1, ROMANCE);
}
LangHint::Pt => {
strip_in(out, r1, PT);
}
LangHint::Nl => {
strip_in(out, r1, NL);
}
LangHint::Sv => {
strip_in(out, r1, SV);
}
LangHint::Tr => {
strip_in(out, r1, TR_CASE);
strip_in(out, r1, TR_PLURAL);
}
_ => {}
}
true
}