mcut 0.1.1

Zero-copy, low-memory multilingual word segmenter using pointwise linear classification and minimal FST
Documentation
use mcut::{
  Cut, CutMode, CutOption, Cutter, Lang, LangCut, MCUT, Model, OwnedUserDict, Token, UserDict,
};

struct LaoCut;

impl LangCut for LaoCut {
  fn matches(&self, ch: char) -> bool {
    matches!(ch as u32, 0x0E80..=0x0EFF)
  }

  fn cut_run<'a, F: FnMut(Token<'a>)>(&self, run: &'a str, _mode: CutMode, mut emit: F) {
    emit(Token::word(run, 0, run.len()));
  }
}

fn main() {
  // 1. Out-of-the-box global MCUT across Precise, Full and Search modes.
  let text = "南京市长江大桥";
  for mode in CutMode::ALL {
    let word_li: Vec<&str> = MCUT.cut(text, *mode).into_iter().map(|t| t.text).collect();
    println!("zh [{mode}]: {word_li:?}");
  }

  // 2. Automatic mixed-language script routing, including Traditional Chinese and alphanumeric identifiers.
  let mixed = "Qdrantのコードで単体テスト,Rust 1.85支持UTF-8与GPT-4o";
  let auto_li: Vec<&str> = MCUT
    .cut(mixed, CutMode::Precise)
    .into_iter()
    .map(|t| t.text)
    .collect();
  println!("auto: {auto_li:?}");

  let zht_mixed = "繁體中文與日本語テスト及Rust 1.85混合搜尋引擎";
  let zht_li: Vec<&str> = MCUT
    .cut(zht_mixed, CutMode::Precise)
    .into_iter()
    .map(|t| t.text)
    .collect();
  println!("zht+ja+en: {zht_li:?}");

  // 3. Streaming zero-allocation cut with separator retention.
  let opt = CutOption::precise().with_separator();
  print!("stream: ");
  MCUT.cut_each("Hello, 世界 2026!", opt, |tok| {
    print!("[{}:{}] ", tok.text, tok.start);
  });
  println!();

  // 4. Pre-built .mdict zero-copy UserDict overlay (including cross-script terms).
  let owned = OwnedUserDict::from_word_li(["Rust语言", "GPT-4o模型"]).expect("valid user dict");
  let mdict_byte = owned.as_slice();
  let user_dict = UserDict::from_slice(mdict_byte).expect("valid mdict");
  let custom = Cutter::builder().with_user_dict(user_dict).build();
  let custom_li: Vec<&str> = custom
    .cut("我喜欢用Rust语言调用GPT-4o模型", CutMode::Precise)
    .into_iter()
    .map(|t| t.text)
    .collect();
  println!("user_dict: {custom_li:?}");

  // 5. Plug in a new language via LangCut while preserving built-in mixed routing.
  let ext = Cutter::builder().with_lang(LaoCut).build();
  let ext_li: Vec<&str> = ext
    .cut("Hello ສະບາຍດີ 南京市!", CutMode::Precise)
    .into_iter()
    .map(|t| t.text)
    .collect();
  println!("ext_lang: {ext_li:?}");

  // 6. Inspect empty zero-copy model metadata API.
  let empty = Model::empty(Lang::Zh);
  println!("empty stat: {}", empty.stat());
}