HSModel

Struct HSModel 

Source
pub struct HSModel {
    pub ham_bow: BagOfWords,
    pub spam_bow: BagOfWords,
}
Expand description

A model which contains 2 BagOfWords, one containing known spam, and the other known ham.

let ham_bow = BagOfWords::from("hello there how are you");
let spam_bow = BagOfWords::from("I have an offer you won't be able to pass up!!!");
let model = HSModel::new().add_spam_bow(spam_bow).add_ham_bow(ham_bow);

Fields§

§ham_bow: BagOfWords

BagOfWords that are known to be found in non-spam (ham) text.

§spam_bow: BagOfWords

BagOfWords that are know to be found in spam text.

Implementations§

Source§

impl HSModel

Source

pub fn new() -> Self

Create a new empty model, with no training data.

let model = HSModel::new(); //returns an empty model.
Source

pub fn add_spam_bow(self, spam_bow: BagOfWords) -> Self

Builder pattern for adding a spam_bow with the combine method.

let model = HSModel::new().add_spam_bow(spam_bow).add_ham_bow(ham_bow); //builder pattern
Source

pub fn add_ham_bow(self, ham_bow: BagOfWords) -> Self

Builder pattern for adding a ham_bow with the combine method.

let model = HSModel::new().add_ham_bow(ham_bow).add_spam_bow(spam_bow); //builder pattern
Source

pub fn from_bows(ham_bow: BagOfWords, spam_bow: BagOfWords) -> Self

Create a HSModel from a ham_bow and a spam_bow.

let model = HSModel::from_bows(ham_bow, spam_bow);
Source

pub fn text_spam_probability(&self, text: &str) -> Probability

Returns the probability that a slice of text is spam, based on the model. Read about how this is calulated here on the Naive Bayes Spam Filtering Wikipedia Page

let spam_probability = model.text_spam_probability("Respond fast! I have an offer of a lifetime!"); // return value between [0.0, 1.0]
Source

pub fn write_to_json(&self, file_path: &str) -> Option<()>

Serializse HSModel to a compact json string and write it to file_path. This write is destructive.

model.write_to_json("test_resources/test_models/model.json").expect("Write unsucessful");
Source

pub fn read_from_json(file_path: &str) -> Option<Self>

read a json string from file_path and deserialize it to HSModel.

let model = HSModel::read_from_json("test_resources/test_models/model.json").unwrap();

Trait Implementations§

Source§

impl Clone for HSModel

Source§

fn clone(&self) -> HSModel

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for HSModel

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for HSModel

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for HSModel

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,