Library API Overview
The litsea crate provides a Rust API for word segmentation, model training, and feature extraction.
Installation
[dependencies]
litsea = "0.13.0"
Loading models from local files is synchronous and needs no async runtime. An async runtime such as tokio is only required when loading models over HTTP/HTTPS with the async load_model method (for example TwoStageLearner::load_model, which always resolves the model URI through the same async path).
Module Map
graph LR
A["litsea::segmenter"] --- B["Segmenter"]
C["litsea::adaboost"] --- D["AdaBoost"]
E["litsea::language"] --- F["Language"]
G["litsea::extractor"] --- H["Extractor"]
I["litsea::trainer"] --- J["Trainer, PerceptronTrainer, TwoStageTrainer, TwoStageMetrics"]
K["litsea::error"] --- L["LitseaError, Result"]
M["litsea::perceptron"] --- N["AveragedPerceptron"]
O["litsea::upos"] --- P["Upos, SegmentLabel"]
Q["litsea::metrics"] --- R["BinaryMetrics, MulticlassMetrics"]
S["litsea::evaluation"] --- T["PosMetrics, SegmentationMetrics"]
U["litsea::two_stage"] --- V["ModelKind, TwoStageFeatureSet, TwoStageLearner"]
| Module | Primary Types | Purpose |
|---|---|---|
litsea::segmenter | Segmenter, SegmentBuffer | Word segmentation (owned or allocation-free output), two-stage segmentation with POS tagging |
litsea::adaboost | AdaBoost | Binary classification, model I/O |
litsea::perceptron | AveragedPerceptron | Multiclass classification (two-stage training), model I/O |
litsea::upos | Upos, SegmentLabel | UPOS POS tags, segment labels |
litsea::language | Language | Language definitions, character classification |
litsea::extractor | Extractor | Feature extraction from corpus |
litsea::trainer | Trainer, PerceptronTrainer, TwoStageTrainer, TwoStageMetrics | Training orchestration |
litsea::error | LitseaError, Result | Error type and result alias |
litsea::metrics | BinaryMetrics, MulticlassMetrics | Evaluation metrics (in-sample) |
litsea::evaluation | PosMetrics, SegmentationMetrics | Held-out evaluation against a gold corpus |
litsea::two_stage | ModelKind, TwoStageFeatureSet, TwoStageLearner | Two-stage model container and model-kind detection |
litsea::model_io | read_model_bytes | Resolves a model URI (path, file://, http(s)://) to raw bytes |
All primary types are also re-exported at the crate root, so use litsea::Segmenter; works as a shorthand for use litsea::segmenter::Segmenter;.
The learners resolve their own URIs, so model_io::read_model_bytes is rarely needed directly. It is public for callers that must inspect a model before choosing a learner — litsea-binding-core reads the bytes once, detects the kind with ModelKind::detect, and feeds the same bytes to load_model_from_reader, which avoids downloading a remote model twice.
Quick Example
use std::path::Path;
use litsea::adaboost::AdaBoost;
use litsea::language::Language;
use litsea::segmenter::Segmenter;
fn main() -> litsea::Result<()> {
let mut learner = AdaBoost::new(0.01, 100);
learner.load_model_from_path(Path::new("./models/RWCP.model"))?;
let segmenter = Segmenter::with_learner(Language::Japanese, learner);
let tokens = segmenter.segment("これはテストです。");
assert_eq!(tokens, vec!["これ", "は", "テスト", "です", "。"]);
Ok(())
}
Quick Example (POS Tagging)
use std::path::Path;
use litsea::language::Language;
use litsea::segmenter::Segmenter;
use litsea::two_stage::TwoStageLearner;
fn main() -> litsea::Result<()> {
let mut learner = TwoStageLearner::new();
learner.load_model_from_path(Path::new("./models/japanese_pos.model"))?;
let segmenter = Segmenter::with_two_stage_learner(Language::Japanese, learner);
let tokens = segmenter.segment_with_pos("これはテストです。")?;
for (word, pos) in &tokens {
print!("{}/{} ", word, pos);
}
println!();
Ok(())
}
Full API documentation is available on docs.rs/litsea.