Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ Naive Bayes classifier
keywords = ["naive-bayes", "bayesian", "classifier", "machine-learning"]

[dependencies]
rustc-serialize = "0.3.12"
regex = "0.1.27"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
regex = "1.0"
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//! ```
//! meat examples from [baconipsum](http://baconipsum.com/), veggie examples from: [veggieipsum](http://veggieipsum.com/)

extern crate rustc_serialize;
extern crate serde;
extern crate regex;

mod naive_bayes;
Expand Down
10 changes: 5 additions & 5 deletions src/naive_bayes.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::collections::{HashMap, HashSet};
use std::f64;
use regex::Regex;
use rustc_serialize::json;
use serde::{Serialize, Deserialize};

static DEFAULT_SMOOTHING: f64 = 1.0f64;

/// Naive Bayes classifier
#[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Classifier {
vocab: HashSet<String>,
num_examples: u32,
smoothing: f64,
classifications: HashMap<String, Classification>
}

#[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Classification {
label: String,
num_examples: u32,
Expand Down Expand Up @@ -151,12 +151,12 @@ impl Classifier {

/// Encodes the classifier as a JSON string.
pub fn to_json(&self) -> String {
json::encode(self).ok().expect("encoding JSON failed")
serde_json::to_string(self).expect("encoding JSON failed")
}

/// Builds a new classifier from a JSON string
pub fn from_json(encoded: &str) -> Classifier {
let classifier: Classifier = json::decode(encoded).ok().expect("decoding JSON failed");
let classifier: Classifier = serde_json::from_str(encoded).expect("decoding JSON failed");
classifier
}

Expand Down