use crate::transformer::{DeinflectFnType, Rule, RuleType, SuffixRule};
use fancy_regex::Regex;
use std::sync::Arc;
pub fn inflection(
inflected: &str,
deinflected: &'static str,
conditions_in: &'static [&'static str],
conditions_out: &'static [&'static str],
rule_type: RuleType,
) -> Rule {
let regx = match rule_type {
RuleType::Prefix => format!("^{inflected}"),
RuleType::Suffix => format!("{inflected}$"),
RuleType::WholeWord => format!("^{inflected}$"),
_ => panic!(
"{rule_type:?} is invalid, only RuleType Suffix, Prefix && WholeWord work with this fn"
),
};
let deinflect_fn = match rule_type {
RuleType::Suffix => DeinflectFnType::GenericSuffix,
RuleType::Prefix => DeinflectFnType::GenericPrefix,
RuleType::WholeWord => DeinflectFnType::GenericWholeWord,
_ => panic!(
"{rule_type:?} is invalid, only RuleType Suffix, Prefix && WholeWord work with this fn"
),
};
let is_inflected = Regex::new(®x).unwrap();
let deinflected = if deinflected.is_empty() {
None
} else {
Some(deinflected)
};
Rule {
rule_type,
is_inflected,
deinflected,
deinflect_fn,
inflected_str: Some(inflected.to_string()),
conditions_in,
conditions_out,
}
}
pub fn generic_stem_change_rule(
stem_from: &'static str,
stem_to: &'static str,
ending_pattern: &'static str,
ending_to: &'static str,
conditions_in: &'static [&'static str],
conditions_out: &'static [&'static str],
) -> Rule {
let is_inflected_re = format!("{stem_from}([a-z]*){ending_pattern}$");
let is_inflected = Regex::new(&is_inflected_re).unwrap();
let deinflect_fn = DeinflectFnType::GenericStemChange {
stem_from,
stem_to,
ending_re: format!("{ending_pattern}$").leak(),
ending_to,
};
Rule {
rule_type: RuleType::Other,
is_inflected,
deinflected: None,
deinflect_fn,
inflected_str: Some(is_inflected_re.strip_suffix('$').unwrap().to_string()),
conditions_in,
conditions_out,
}
}
pub fn special_cased_stem_change_rule(
inflected_stem: &'static str,
prefix: &'static str,
special_stem_from: &'static str,
special_stem_to: &'static str,
default_stem_from: &'static str,
default_stem_to: &'static str,
ending_pattern: &'static str,
ending_to: &'static str,
conditions_in: &'static [&'static str],
conditions_out: &'static [&'static str],
) -> Rule {
let is_inflected_re = format!("{inflected_stem}([a-z]*){ending_pattern}$");
let is_inflected = Regex::new(&is_inflected_re).unwrap();
let deinflect_fn = DeinflectFnType::SpecialCasedStemChange {
prefix,
special_stem_from,
special_stem_to,
default_stem_from,
default_stem_to,
ending_re: format!("{ending_pattern}$").leak(),
ending_to,
};
Rule {
rule_type: RuleType::Other,
is_inflected,
deinflected: None,
deinflect_fn,
inflected_str: Some(is_inflected_re.strip_suffix('$').unwrap().to_string()),
conditions_in,
conditions_out,
}
}