diff --git a/Cargo.toml b/Cargo.toml index 93137f1..8eb68dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,17 @@ [package] -name = "mood-cgi" +name = "mood" version = "0.1.0" edition = "2021" +[[bin]] +name = "mood.cgi" +path = "src/cgi.rs" + +[[bin]] +name = "mood-counter" +path = "src/counter.rs" + + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/src/main.rs b/src/cgi.rs similarity index 100% rename from src/main.rs rename to src/cgi.rs diff --git a/src/counter.rs b/src/counter.rs new file mode 100644 index 0000000..b3479c2 --- /dev/null +++ b/src/counter.rs @@ -0,0 +1,26 @@ +use std::collections::HashMap; + +fn main() { + let file = std::env::var("MOOD_FILE").expect("missing MOOD_FILE env variable"); + let content = std::fs::read_to_string(file).expect("could not read MOOD_FILE contents"); + + let lines : Vec<(u64, &str)> = content.lines() + .map(|l| l.split_once(' ').unwrap()) + .map(|(n, l)| (n.parse::().unwrap(), l)) + .collect(); + + let sum = lines.iter().map(|(x,_)| x).sum::() as f32; + let mut probabilities = HashMap::new(); + + for (count, line) in &lines { + probabilities.insert(line, *count as f32 / sum); + } + + println!("{{"); + for (line, chance) in probabilities { + println!(" \"{}\": {:.6},", line.replace('"', "\\\""), chance); + } + println!(" \"count\": {},", lines.len()); + println!(" \"total\": {}", sum); + println!("}}"); +}