lindera-crf: Conditional Random Fields implemented in pure Rust
lindera-crf is forked from rucrf and contains a trainer and an estimator for Conditional Random Fields (CRFs). This library supports:
- lattices with variable length edges,
- L1, L2, and Elastic Net regularization,
- multi-threading during training, and
- zero-copy deserialization with rkyv.
Changes from rucrf
- Serialization backend replaced: Switched from
bincodetorkyvfor zero-copy deserialization, enabling faster model loading - Elastic Net regularization: Added
Regularization::ElasticNetthat combines L1 and L2 penalties with a configurablel1_ratioparameter - Rust 2024 edition: Updated to Rust 2024 edition
- Dependency updates: Updated
argmin(0.10 -> 0.11),argmin-math(0.4 -> 0.5),argmin-observer-slog(0.1 -> 0.2),hashbrown(0.15 -> 0.16)
Examples
use NonZeroU32;
use ;
// Train:
// 京(kyo) 都(to)
// 東(to) 京(kyo)
// 京(kei) 浜(hin)
// 京(kyo) の(no) 都(miyako)
//
// Test:
// 水(mizu) の(no) 都(miyako)
//
// 1-gram features:
// 京: 1, 都: 2, 東: 3, 浜: 4, の: 5, 水: 6
// 2-gram features:
// kyo: 1, to: 2, kei: 3, hin: 4, no: 5, miyako: 6, mizu: 7
let mut provider = new;
let label_京kyo = provider.add_feature_set?;
let label_都to = provider.add_feature_set?;
let label_東to = provider.add_feature_set?;
let label_京kei = provider.add_feature_set?;
let label_浜hin = provider.add_feature_set?;
let label_のno = provider.add_feature_set?;
let label_都miyako = provider.add_feature_set?;
let label_水mizu = provider.add_feature_set?;
let mut lattices = vec!;
// 京都 (kyo to)
let mut lattice = new?;
lattice.add_edge?;
lattice.add_edge?;
lattice.add_edge?;
lattice.add_edge?;
lattices.push;
// 東京 (to kyo)
let mut lattice = new?;
lattice.add_edge?;
lattice.add_edge?;
lattice.add_edge?;
lattices.push;
// 京浜 (kei hin)
let mut lattice = new?;
lattice.add_edge?;
lattice.add_edge?;
lattice.add_edge?;
lattices.push;
// 京の都 (kyo no miyako)
let mut lattice = new?;
lattice.add_edge?;
lattice.add_edge?;
lattice.add_edge?;
lattice.add_edge?;
lattice.add_edge?;
lattices.push;
// Generates a model
let trainer = new;
let model = trainer.train;
// 水の都 (mizu no miyako)
let mut lattice = new?;
lattice.add_edge?;
lattice.add_edge?;
lattice.add_edge?;
lattice.add_edge?;
let = model.search_best_path;
assert_eq!;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
See the guidelines.