Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Library API Overview

The litsea crate provides a Rust API for word segmentation, model training, and feature extraction.

Installation

[dependencies]
litsea = "0.16.0"

Loading models from local files with load_model_from_path or load_model_from_reader is synchronous and needs no async runtime. The load_model methods (for example TwoStageLearner::load_model) and model_io::read_model_bytes are async for every URI form, local paths included, so calling them needs an async runtime such as tokio.

Loading models over HTTP/HTTPS also needs the remote_model feature, which is off by default; without it, http:// and https:// URIs fail with LitseaError::Unsupported (see Remote Model Loading):

[dependencies]
litsea = { version = "0.16.0", features = ["remote_model"] }

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"]
    W["litsea::user_dictionary"] --- X["UserDictionary"]
    Y["litsea::model_io"] --- Z["read_model_bytes"]
ModulePrimary TypesPurpose
litsea::segmenterSegmenter, SegmentBufferWord segmentation (owned or allocation-free output), two-stage segmentation with POS tagging
litsea::user_dictionaryUserDictionaryUser dictionary: words emitted as single tokens, optionally with a UPOS tag for segment_with_pos (see User Dictionary)
litsea::adaboostAdaBoostBinary classification, model I/O
litsea::perceptronAveragedPerceptronMulticlass classification (two-stage training), model I/O
litsea::uposUpos, SegmentLabelUPOS POS tags, segment labels
litsea::languageLanguageLanguage definitions, character classification
litsea::extractorExtractorFeature extraction from corpus
litsea::trainerTrainer, PerceptronTrainer, TwoStageTrainer, TwoStageMetricsTraining orchestration
litsea::errorLitseaError, ResultError type and result alias
litsea::metricsBinaryMetrics, MulticlassMetricsEvaluation metrics (in-sample)
litsea::evaluationPosMetrics, SegmentationMetricsHeld-out evaluation against a gold corpus
litsea::two_stageModelKind, TwoStageFeatureSet, TwoStageLearnerTwo-stage model container and model-kind detection
litsea::model_ioread_model_bytesResolves a model URI (path, file://, or http(s):// with the remote_model feature) to raw bytes (async)

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 crate root also provides litsea::version(), which returns the crate version (for example "0.16.0").

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/japanese.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.