mcut 0.1.1

Zero-copy, low-memory multilingual word segmenter using pointwise linear classification and minimal FST
Documentation
use crate::{
  cut::{LangChain, LangCut, LangPack, NoLang},
  cutter::Cutter,
  format::{Model, UserDict},
  i18n::Lang,
};

/// Builder for [`Cutter`] with custom zero-copy models, [`UserDict`], and [`LangCut`] extensions.
#[derive(Clone, Copy, Debug)]
pub struct CutterBuilder<'a, E = NoLang> {
  pub(crate) zh: Option<Model<'a>>,
  pub(crate) ja: Option<Model<'a>>,
  pub(crate) th: Option<Model<'a>>,
  pub(crate) user_dict: Option<UserDict<'a>>,
  pub(crate) ext: E,
}

impl Default for CutterBuilder<'_, NoLang> {
  fn default() -> Self {
    Self::new()
  }
}

impl<'a> CutterBuilder<'a, NoLang> {
  /// Creates an empty builder defaulting to embedded built-in models.
  pub const fn new() -> Self {
    Self {
      zh: None,
      ja: None,
      th: None,
      user_dict: None,
      ext: NoLang,
    }
  }

  /// Installs a slice, array, or `Vec` of [`LangCut`] extensions in one call.
  pub fn with_lang_pack<P: LangPack>(self, ext: P) -> CutterBuilder<'a, P> {
    CutterBuilder {
      zh: self.zh,
      ja: self.ja,
      th: self.th,
      user_dict: self.user_dict,
      ext,
    }
  }
}

impl<'a, E> CutterBuilder<'a, E> {
  /// Overrides the built-in model matching `model.lang()` with a zero-copy external [`Model`].
  ///
  /// When an external model is set for a language, its embedded `.rodata` bytes are never touched.
  pub const fn with_model(mut self, model: Model<'a>) -> Self {
    match model.lang() {
      Lang::Zh => self.zh = Some(model),
      Lang::Ja => self.ja = Some(model),
      Lang::Th => self.th = Some(model),
      Lang::Ko | Lang::En | Lang::Ru | Lang::Ar => {}
    }
    self
  }

  /// Attaches a zero-copy hard-boundary [`UserDict`] overlay.
  pub const fn with_user_dict(mut self, user_dict: UserDict<'a>) -> Self {
    self.user_dict = Some(user_dict);
    self
  }

  /// Registers a custom [`LangCut`] language handler using zero-cost static dispatch.
  pub fn with_lang<L: LangCut>(self, lang: L) -> CutterBuilder<'a, LangChain<L, E>> {
    CutterBuilder {
      zh: self.zh,
      ja: self.ja,
      th: self.th,
      user_dict: self.user_dict,
      ext: LangChain::new(lang, self.ext),
    }
  }

  /// Finalizes the [`Cutter`].
  pub fn build(self) -> Cutter<'a, E> {
    Cutter {
      zh: self.zh,
      ja: self.ja,
      th: self.th,
      user_dict: self.user_dict,
      ext: self.ext,
    }
  }
}