Pure Rust implementation of fastText
Add it to your Cargo.toml:
[dependencies]
fasttext = "0.8"use fasttext::FastText;
use fasttext::args::{Args, ModelName};
let mut args = Args::default();
args.input = "data.txt".into();
args.model = ModelName::Supervised;
args.epoch = 25;
args.lr = 1.0;
let model = FastText::train(args).unwrap();
model.save_model("model.bin").unwrap();use fasttext::FastText;
let model = FastText::load_model("model.bin").unwrap();
let predictions = model.predict("Which baking dish is best?", 3, 0.0);
for pred in &predictions {
println!("{} {}", pred.label, pred.prob);
}use fasttext::FastText;
let model = FastText::load_model("model.bin").unwrap();
let neighbors = model.get_nn("king", 10);
for (score, word) in &neighbors {
println!("{word}\t{score}");
}A command-line tool compatible with the C++ fastText CLI is available behind the cli feature:
cargo install fasttext --features cliIt supports C++-style single-dash flags (e.g. -epoch 25) as well as standard double-dash flags (--epoch 25):
fasttext supervised -input data.txt -output model -epoch 25 -lr 1.0
fasttext predict model.bin test.txt
fasttext test model.bin test.txt
fasttext nn model.binThis work is released under the MIT license. A copy of the license is provided in the LICENSE file.