Struct NaiveBayes

Source
pub struct NaiveBayes { /* private fields */ }

Implementations§

Source§

impl NaiveBayes

Source

pub fn new() -> NaiveBayes

creates a new instance of a NaiveBayes classifier.

Examples found in repository?
examples/example.rs (line 15)
14fn main() {
15    let mut nb = NaiveBayes::new();
16    nb.train(&to_vec("great product"), &"positive".to_string());
17    nb.train(
18        &to_vec("the protection level is poor"),
19        &"negative".to_string(),
20    );
21    nb.train(
22        &to_vec("this is a great band I love them"),
23        &"positive".to_string(),
24    );
25    nb.train(
26        &to_vec("never buy this product it is too bad"),
27        &"negative".to_string(),
28    );
29    nb.train(&to_vec("i love the shoes"), &"positive".to_string());
30    nb.train(
31        &to_vec("good product happy with the purchase"),
32        &"positive".to_string(),
33    );
34    let to_classify: Vec<String> = to_vec("I love it is great");
35    let classification = nb.classify(&to_classify);
36    print!("classification = {:?}", classification);
37}
Source

pub fn train(&mut self, data: &Vec<String>, label: &String)

trains the model with a Vec<String> of tokens, associating it with a String label.

Examples found in repository?
examples/example.rs (line 16)
14fn main() {
15    let mut nb = NaiveBayes::new();
16    nb.train(&to_vec("great product"), &"positive".to_string());
17    nb.train(
18        &to_vec("the protection level is poor"),
19        &"negative".to_string(),
20    );
21    nb.train(
22        &to_vec("this is a great band I love them"),
23        &"positive".to_string(),
24    );
25    nb.train(
26        &to_vec("never buy this product it is too bad"),
27        &"negative".to_string(),
28    );
29    nb.train(&to_vec("i love the shoes"), &"positive".to_string());
30    nb.train(
31        &to_vec("good product happy with the purchase"),
32        &"positive".to_string(),
33    );
34    let to_classify: Vec<String> = to_vec("I love it is great");
35    let classification = nb.classify(&to_classify);
36    print!("classification = {:?}", classification);
37}
Source

pub fn classify(&mut self, data: &Vec<String>) -> HashMap<String, f64>

classify a Vec<String> of tokens returning a map of tokens and probabilities as keys and values, respectively.

Examples found in repository?
examples/example.rs (line 35)
14fn main() {
15    let mut nb = NaiveBayes::new();
16    nb.train(&to_vec("great product"), &"positive".to_string());
17    nb.train(
18        &to_vec("the protection level is poor"),
19        &"negative".to_string(),
20    );
21    nb.train(
22        &to_vec("this is a great band I love them"),
23        &"positive".to_string(),
24    );
25    nb.train(
26        &to_vec("never buy this product it is too bad"),
27        &"negative".to_string(),
28    );
29    nb.train(&to_vec("i love the shoes"), &"positive".to_string());
30    nb.train(
31        &to_vec("good product happy with the purchase"),
32        &"positive".to_string(),
33    );
34    let to_classify: Vec<String> = to_vec("I love it is great");
35    let classification = nb.classify(&to_classify);
36    print!("classification = {:?}", classification);
37}
Source

pub fn log_classify(&mut self, data: &Vec<String>) -> HashMap<String, f64>

classify a Vec<String> of tokens returning a map of tokens and log-probabilities as keys and values, respectively. Using log_classify may prevent underflows.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.