feat: use vec in mood-counter to be able to sort

This commit is contained in:
əlemi 2023-11-20 14:02:44 +01:00
parent 933cf0c762
commit 1dc9d8575f

View file

@ -1,5 +1,3 @@
use std::collections::HashMap;
fn main() { fn main() {
let file = std::env::var("MOOD_FILE").expect("missing MOOD_FILE env variable"); 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 content = std::fs::read_to_string(file).expect("could not read MOOD_FILE contents");
@ -10,15 +8,17 @@ fn main() {
.collect(); .collect();
let sum = lines.iter().map(|(x,_)| x).sum::<u64>() as f32; let sum = lines.iter().map(|(x,_)| x).sum::<u64>() as f32;
let mut probabilities = HashMap::new(); let mut probabilities = Vec::new();
for (count, line) in &lines { for (count, line) in &lines {
probabilities.insert(line, *count as f32 / sum); probabilities.push((line, *count, *count as f32 / sum));
} }
probabilities.sort_by_key(|x| x.1);
println!("{{"); println!("{{");
for (line, chance) in probabilities { for (line, count, chance) in probabilities {
println!(" \"{}\": {:.6},", line.replace('"', "\\\""), chance); println!(" \"{}\": [{}, {:.4}],", l, count, chance*100.);
} }
println!(" \"count\": {},", lines.len()); println!(" \"count\": {},", lines.len());
println!(" \"total\": {}", sum); println!(" \"total\": {}", sum);