aboutsummaryrefslogtreecommitdiff
path: root/src/freq.rs
blob: dc692c6a18b03356c50cff72a9352dadfad058ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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 strip_nick(s: &str) -> &str {
    if s.is_empty() { return s }
    match s.as_bytes()[0] {
        b'~' | b'&' | b'@' | b'%' | b'+' => &s[1..],
        _ => s
    }
}

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, .. } => {
                let nick = strip_nick(from);
                if stats.contains_key(nick) {
                    let p: &mut Person = stats.get_mut(nick).unwrap();
                    p.lines += 1;
                    p.words += words(content);
                } else {
                    stats.insert(nick.to_owned(), 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)
    }
}