mirror of
https://git.alemi.dev/mood.git
synced 2024-11-14 04:49:19 +01:00
feat: added counter bin
This commit is contained in:
parent
77620f9ea1
commit
933cf0c762
3 changed files with 36 additions and 1 deletions
11
Cargo.toml
11
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]
|
||||
|
|
26
src/counter.rs
Normal file
26
src/counter.rs
Normal file
|
@ -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::<u64>().unwrap(), l))
|
||||
.collect();
|
||||
|
||||
let sum = lines.iter().map(|(x,_)| x).sum::<u64>() 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!("}}");
|
||||
}
|
Loading…
Reference in a new issue