lexf 0.1.0

Zero-unsafe multilingual morphological expander and query normalizer for search indexing
Documentation
//! Unicode script classification shared by token expansion and language detection.

/// Script of a single character.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Script {
  Digit,
  Kana,
  Hangul,
  Han,
  Cyrillic,
  Arabic,
  Greek,
  Latin,
  OtherLetter,
  Other,
}

/// Number of [`Script`] variants.
pub(crate) const SCRIPT_N: usize = Script::Other as usize + 1;

impl Script {
  /// Classifies `ch` by Unicode block; non-alphabetic non-digits always map to `Other`.
  pub(crate) fn of(ch: char) -> Self {
    if ch.is_ascii() {
      return match ch {
        '0'..='9' => Self::Digit,
        'a'..='z' | 'A'..='Z' => Self::Latin,
        _ => Self::Other,
      };
    }
    if !ch.is_alphabetic() {
      return Self::Other;
    }
    match ch as u32 {
      // Hiragana, Katakana (excluding middle dot U+30FB), halfwidth Katakana.
      0x3040..=0x309F | 0x30A1..=0x30FA | 0x30FC..=0x30FF | 0xFF66..=0xFF9F => Self::Kana,
      0x1100..=0x11FF | 0x3130..=0x318F | 0xAC00..=0xD7AF => Self::Hangul,
      0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xF900..=0xFAFF | 0x20000..=0x3134F => Self::Han,
      0x0400..=0x052F => Self::Cyrillic,
      0x0600..=0x06FF | 0x0750..=0x077F | 0x08A0..=0x08FF => Self::Arabic,
      0x0370..=0x03FF | 0x1F00..=0x1FFF => Self::Greek,
      0x00C0..=0x024F
      | 0x1E00..=0x1EFF
      | 0x2C60..=0x2C7F
      | 0xA720..=0xA7FF
      | 0xAB30..=0xAB6F
      | 0xFF21..=0xFF3A
      | 0xFF41..=0xFF5A => Self::Latin,
      _ => Self::OtherLetter,
    }
  }

  /// Bit flag corresponding to this script inside a `u16` script mask.
  #[inline]
  pub(crate) const fn bit(self) -> u16 {
    1 << self as u8
  }
}

/// Token category selecting the expansion pipeline.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Kind {
  /// Digits and punctuation only.
  Number,
  /// Digits mixed with letters (`utf-8`, `gpt-4o`, `v1.20.3`).
  Code,
  Han,
  Ja,
  Ko,
  Ru,
  Ar,
  El,
  Latin,
  Other,
}

/// Specialized letter scripts in precedence order (Kana outranks Han so mixed Japanese stays Japanese).
const LETTER: [(Script, Kind); 7] = [
  (Script::Kana, Kind::Ja),
  (Script::Hangul, Kind::Ko),
  (Script::Han, Kind::Han),
  (Script::Cyrillic, Kind::Ru),
  (Script::Arabic, Kind::Ar),
  (Script::Greek, Kind::El),
  (Script::Latin, Kind::Latin),
];

/// Bit mask of every letter script (including unsupported scripts so `"नमस्ते42"` stays `Code`).
const LETTER_MASK: u16 = {
  let mut mask = Script::OtherLetter.bit();
  let mut i = 0;
  while i < LETTER.len() {
    mask |= LETTER[i].0.bit();
    i += 1;
  }
  mask
};

/// Classifies a normalized token from its accumulated [`Script::bit`] mask.
pub(crate) fn classify_mask(mask: u16) -> Kind {
  let letter = mask & LETTER_MASK != 0;
  if mask & Script::Digit.bit() != 0 {
    return if letter { Kind::Code } else { Kind::Number };
  }
  LETTER
    .iter()
    .find(|(s, _)| mask & s.bit() != 0)
    .map_or(Kind::Other, |&(_, kind)| kind)
}