crfrs 0.1.0

Generic structured-perceptron sequence labeler with Viterbi decoding
Documentation

crfrs

Generic structured-perceptron sequence labeler with Viterbi decoding, written in Rust.

Implements the averaged structured perceptron (Collins 2002) with lazy averaging (Daumé 2006) for efficient training. You supply per-token feature vectors and a label set; the crate handles training and inference.

Features

  • Averaged structured perceptron training
  • Viterbi decoding over label sequences
  • Emission and transition scoring
  • Save/load models as JSON
  • Language-agnostic — works with any feature strings and label set

Usage

[dependencies]
crfrs = "0.1"
use crfrs::{train, TrainConfig, PerceptronModel};

// Build training data: each example is (features_per_token, gold_labels)
let examples: Vec<(Vec<Vec<String>>, Vec<String>)> = vec![
    (
        vec![
            vec!["word=the".into(), "shape=lower".into()],
            vec!["word=cat".into(), "shape=lower".into()],
        ],
        vec!["DET".into(), "NOUN".into()],
    ),
];

let labels = vec!["DET".into(), "NOUN".into(), "VERB".into()];
let config = TrainConfig { epochs: 20 };

let model = train(&examples, labels, config);
model.save(std::path::Path::new("model.json")).unwrap();

// Inference
let loaded = PerceptronModel::load(std::path::Path::new("model.json")).unwrap();
let tags = loaded.tag(&[
    vec!["word=a".into(), "shape=lower".into()],
    vec!["word=dog".into(), "shape=lower".into()],
]);
println!("{:?}", tags); // ["DET", "NOUN"]

API

Item Description
train(examples, labels, config) Train a model from labeled sequences
PerceptronTrainer Low-level trainer for custom training loops
TrainConfig Training hyperparameters (default: 15 epochs)
PerceptronModel::tag(&seq_features) Viterbi decode a sequence
PerceptronModel::save(path) Persist model to JSON
PerceptronModel::load(path) Load model from JSON

License

MIT