Install and use nlprule in Rust
mainnlprule is a high-performance Rust library. For the best experience, use the nlprule-build crate in your build.rs to compile the necessary language binaries during the build process.
Dependency Setup
Add both nlprule and nlprule-build to your Cargo.toml. Important: The versions of nlprule and nlprule-build must be identical.
Build Script (build.rs)
Use nlprule_build::BinaryBuilder to build and validate the language binaries (e.g., for "en") and place them in the OUT_DIR.
Application Code
In your main application, use the tokenizer_filename! and rules_filename! macros to locate the compiled binaries in the OUT_DIR. You can then load them using Tokenizer::from_reader and Rules::from_reader.
// Cargo.toml
[dependencies]
nlprule = "0.6.4"
[build-dependencies]
nlprule-build = "0.6.4"
// build.rs
fn main() -> Result<(), nlprule_build::Error> {
println!("cargo:rerun-if-changed=build.rs");
nlprule_build::BinaryBuilder::new(
&["en"],
std::env::var("OUT_DIR").expect("OUT_DIR is set"),
)
.build()?
.validate()
}
// src/main.rs
use nlprule::{Rules, Tokenizer, tokenizer_filename, rules_filename};
fn main() {
let mut tokenizer_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/", tokenizer_filename!("en")));
let mut rules_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/", rules_filename!("en")));
let tokenizer = Tokenizer::from_reader(&mut tokenizer_bytes).unwrap();
let rules = Rules::from_reader(&mut rules_bytes).unwrap();
let corrected = rules.correct("She was not been here since Monday.", &tokenizer);
assert_eq!(corrected, "She was not here since Monday.");
}