aboutsummaryrefslogtreecommitdiff
path: root/src/freq.rs
diff options
context:
space:
mode:
authorTill Hoeppner2015-06-09 00:18:31 +0200
committerTill Hoeppner2015-06-09 00:18:31 +0200
commit9c63c4b89ea0fd889f7f0fd1da71684511e6620e (patch)
treeb50c0598f3aae17f2ab6e8ffad9ef0ba21d6838f /src/freq.rs
parentcd182daf35eae3739511f3751843ad01f0d489b3 (diff)
downloadilc-9c63c4b89ea0fd889f7f0fd1da71684511e6620e.tar.gz
ilc-9c63c4b89ea0fd889f7f0fd1da71684511e6620e.tar.xz
ilc-9c63c4b89ea0fd889f7f0fd1da71684511e6620e.zip
Add basic frequency counter binary
Diffstat (limited to 'src/freq.rs')
-rw-r--r--src/freq.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/freq.rs b/src/freq.rs
new file mode 100644
index 0000000..21c4e62
--- /dev/null
+++ b/src/freq.rs
@@ -0,0 +1,53 @@
+extern crate ilc;
+
+use std::io;
+use std::collections::hash_map::*;
+
+use ilc::log::Event::*;
+use ilc::format::{ self, Decode };
+
+struct Person {
+ lines: u32,
+ words: u32
+}
+
+fn words(s: &str) -> u32 {
+ s.split_whitespace().filter(|s| !s.is_empty()).count() as u32
+}
+
+fn main() {
+ let stdin = io::stdin();
+
+ let mut stats: HashMap<String, Person> = HashMap::new();
+
+ let mut parser = format::weechat3::Weechat3;
+ for e in parser.decode(stdin.lock()) {
+ let m = match e {
+ Ok(m) => m,
+ Err(err) => panic!(err)
+ };
+
+ match m {
+ Msg { ref from, ref content, .. } => {
+ if stats.contains_key(from) {
+ let p: &mut Person = stats.get_mut(from).unwrap();
+ p.lines += 1;
+ p.words += words(content);
+ } else {
+ stats.insert(from.clone(), Person {
+ lines: 1,
+ words: words(content)
+ });
+ }
+ },
+ _ => ()
+ }
+ }
+
+ let mut stats: Vec<(String, Person)> = stats.into_iter().collect();
+ stats.sort_by(|&(_, ref a), &(_, ref b)| b.words.cmp(&a.words));
+
+ for &(ref name, ref stat) in stats.iter().take(10) {
+ println!("{}:\n\tLines: {}\n\tWords: {}", name, stat.lines, stat.words)
+ }
+}