Skip to main content

badwords_ml/
lib.rs

1//! Multi-label toxicity scoring for [`badwords`], on ONNX Runtime.
2//!
3//! The rule-based engine in `badwords-core` answers "is this word in the
4//! dictionary". This crate answers "how toxic is this text, and in what way",
5//! with one probability per axis - toxicity, insult, threat, and so on, taken
6//! from the model's own config rather than hardcoded.
7//!
8//! ```no_run
9//! use badwords_ml::ToxicityModel;
10//!
11//! let model = ToxicityModel::open("model")?;
12//! let scores = model.predict("you are an idiot")?;
13//!
14//! println!("{:.2}", scores.toxicity());
15//! for (axis, value) in scores.above(0.5) {
16//!     println!("{axis}: {value:.2}");
17//! }
18//! # Ok::<(), badwords_ml::Error>(())
19//! ```
20//!
21//! [`HybridFilter`] puts the rules in front: they answer outright when they
22//! can, and the model is asked only when they cannot.
23//!
24//! The model directory is the one `ml/train.py` writes - `model.onnx`,
25//! `config.json` and `tokenizer.json` - the same directory the Python package
26//! downloads and caches.
27
28mod error;
29mod hybrid;
30mod locate;
31mod model;
32mod scores;
33
34pub use error::Error;
35pub use hybrid::{
36    Decision, HybridFilter, HybridResult, DEFAULT_CERTAIN_AT, DEFAULT_DECISION_THRESHOLD,
37};
38pub use locate::{cache_dir, is_complete, locate_model, REQUIRED_FILES};
39pub use model::{ToxicityModel, ToxicityModelBuilder, DEFAULT_MAX_LENGTH};
40pub use scores::{Scores, TOXICITY};