#classifier #bayesian #spam

bin+lib bayespam

A simple bayesian spam classifier

13 releases (5 stable)

1.1.0 Dec 24, 2020
1.0.3 Dec 13, 2019
1.0.0 Nov 9, 2019
0.3.0 Nov 6, 2019
0.1.4 Oct 27, 2019

#118 in Science

Download history 177/week @ 2023-12-18 7/week @ 2023-12-25 153/week @ 2024-01-01 184/week @ 2024-01-08 235/week @ 2024-01-15 233/week @ 2024-01-22 919/week @ 2024-01-29 414/week @ 2024-02-05 316/week @ 2024-02-12 322/week @ 2024-02-19 580/week @ 2024-02-26 525/week @ 2024-03-04 398/week @ 2024-03-11 380/week @ 2024-03-18 348/week @ 2024-03-25 248/week @ 2024-04-01

1,380 downloads per month

MIT license

1MB
162 lines

bayespam

Build Status Crates.io Docs License: MIT

A simple bayesian spam classifier.

About

Bayespam is inspired by Naive Bayes classifiers, a popular statistical technique of e-mail filtering.

Here, the message to be identified is cut into simple words, also called tokens. That are compared to all the corpus of messages (spam or not), to determine the frequency of different tokens in both categories.

A probabilistic formula is used to calculate the probability that the message is a spam. When the probability is high enough, the classifier categorizes the message as likely a spam, otherwise as likely a ham. The probability threshold is fixed at 0.8 by default.

Documentation

Learn more about Bayespam here: https://docs.rs/bayespam.

Usage

Add to your Cargo.toml manifest:

[dependencies]
bayespam = "1.1.0"

Use a pre-trained model

Add a model.json file to your package root. Then, you can use it to score and identify messages:

extern crate bayespam;

use bayespam::classifier;

fn main() -> Result<(), std::io::Error> {
    // Identify a typical spam message
    let spam = "Lose up to 19% weight. Special promotion on our new weightloss.";
    let score = classifier::score(spam)?;
    let is_spam = classifier::identify(spam)?;
    println!("{:.4?}", score);
    println!("{:?}", is_spam);

    // Identify a typical ham message
    let ham = "Hi Bob, can you send me your machine learning homework?";
    let score = classifier::score(ham)?;
    let is_spam = classifier::identify(ham)?;
    println!("{:.4?}", score);
    println!("{:?}", is_spam);

    Ok(())
}
$> cargo run
0.9993
true
0.6311
false

Train your own model and save it as JSON into a file

You can train a new model from scratch, save it as JSON to reload it later:

extern crate bayespam;

use bayespam::classifier::Classifier;
use std::fs::File;

fn main() -> Result<(), std::io::Error> {
    // Create a new classifier with an empty model
    let mut classifier = Classifier::new();

    // Train the classifier with a new spam example
    let spam = "Don't forget our special promotion: -30% on men shoes, only today!";
    classifier.train_spam(spam);

    // Train the classifier with a new ham example
    let ham = "Hi Bob, don't forget our meeting today at 4pm.";
    classifier.train_ham(ham);

    // Identify a typical spam message
    let spam = "Lose up to 19% weight. Special promotion on our new weightloss.";
    let score = classifier.score(spam);
    let is_spam = classifier.identify(spam);
    println!("{:.4}", score);
    println!("{}", is_spam);

    // Identify a typical ham message
    let ham = "Hi Bob, can you send me your machine learning homework?";
    let score = classifier.score(ham);
    let is_spam = classifier.identify(ham);
    println!("{:.4}", score);
    println!("{}", is_spam);

    // Serialize the model and save it as JSON into a file
    let mut file = File::create("my_super_model.json")?;
    classifier.save(&mut file, false)?;

    Ok(())
}
$> cargo run
0.9999
true
0.0001
false
$> cat my_super_model.json
{"token_table":{"forget":{"ham":1,"spam":1},"only":{"ham":0,"spam":1},"meeting":{"ham":1,"spam":0},"our":{"ham":1,"spam":1},"dont":{"ham":1,"spam":1},"bob":{"ham":1,"spam":0},"men":{"ham":0,"spam":1},"today":{"ham":1,"spam":1},"shoes":{"ham":0,"spam":1},"special":{"ham":0,"spam":1},"promotion:":{"ham":0,"spam":1}}}

Contribution

Contributions via issues or pull requests are appreciated.

License

Bayespam is distributed under the terms of the MIT License.

Dependencies