lexf 0.1.0

Zero-unsafe multilingual morphological expander and query normalizer for search indexing
Documentation
//! Light10-style Arabic prefix/suffix stemmer.

use super::{has_char_n, strip_keep};

/// Minimum chars an Arabic stem must keep.
const MIN_STEM: usize = 3;
/// Definite-article and conjunction prefixes, longest first.
const PREFIX: &[&str] = &["وال", "بال", "كال", "فال", "ولل", "لل", "ال"];
/// Pronoun, plural, and feminine suffixes, longest first.
const SUFFIX: &[&str] = &[
  "ها", "ان", "ات", "ون", "ين", "يه", "هم", "هن", "كم", "كن", "تم", "تن", "وا", "ه", "ي",
];

/// Emits the stem of an already letter-folded Arabic word.
pub(crate) fn emit(fold: &str, out: &mut impl FnMut(&str)) {
  let word = PREFIX
    .iter()
    .find_map(|pre| fold.strip_prefix(pre))
    .filter(|rest| has_char_n(rest, MIN_STEM))
    .unwrap_or(fold);
  out(strip_keep(word, SUFFIX, MIN_STEM).unwrap_or(word));
}