diff options
author | Till Hoeppner | 2015-06-09 00:18:31 +0200 |
---|---|---|
committer | Till Hoeppner | 2015-06-09 00:18:31 +0200 |
commit | 9c63c4b89ea0fd889f7f0fd1da71684511e6620e (patch) | |
tree | b50c0598f3aae17f2ab6e8ffad9ef0ba21d6838f | |
parent | cd182daf35eae3739511f3751843ad01f0d489b3 (diff) | |
download | ilc-9c63c4b89ea0fd889f7f0fd1da71684511e6620e.tar.gz ilc-9c63c4b89ea0fd889f7f0fd1da71684511e6620e.tar.xz ilc-9c63c4b89ea0fd889f7f0fd1da71684511e6620e.zip |
Add basic frequency counter binary
-rw-r--r-- | Cargo.toml | 13 | ||||
-rw-r--r-- | src/format/binary.rs | 13 | ||||
-rw-r--r-- | src/format/mod.rs | 2 | ||||
-rw-r--r-- | src/format/weechat3.rs | 11 | ||||
-rw-r--r-- | src/freq.rs | 53 | ||||
-rw-r--r-- | src/lib.rs | 1 |
6 files changed, 85 insertions, 8 deletions
@@ -8,6 +8,11 @@ authors = ["Till Hoeppner <till@hoeppner.ws>"] name = "ilc" doc = false +[[bin]] +name = "freq" +path = "src/freq.rs" +doc = false + [dependencies] regex = "*" regex_macros = "*" @@ -16,6 +21,14 @@ docopt = "*" rustc-serialize = "*" log = "*" env_logger = "*" +bincode = "*" + +[dependencies.clippy] +version = "*" +optional = true + +[features] +lints = ["clippy"] [profile.release] opt-level = 3 diff --git a/src/format/binary.rs b/src/format/binary.rs index aae760c..f7efd7d 100644 --- a/src/format/binary.rs +++ b/src/format/binary.rs @@ -1,6 +1,5 @@ -use std::io::{ self, BufRead, Write }; -use std::borrow::ToOwned; -use std::iter::{ Iterator }; +use std::io::{ BufRead, Write }; +use std::iter::Iterator; use log::Event; use format::{ Encode, Decode }; @@ -10,19 +9,21 @@ use bincode::{ self, SizeLimit }; pub struct Binary; pub struct Iter<R> where R: BufRead { - input: R, + input: R } impl<R> Iterator for Iter<R> where R: BufRead { type Item = ::Result<Event>; fn next(&mut self) -> Option<::Result<Event>> { - Some(bincode::decode_from(&mut self.input, SizeLimit::Infinite).map_err(|_| ::IlcError::BincodeDecode)) + Some(bincode::decode_from::<R, Event>(&mut self.input, SizeLimit::Infinite) + .map_err(|_| ::IlcError::BincodeDecode)) } } impl<W> Encode<W> for Binary where W: Write { fn encode(&self, mut output: W, event: &Event) -> ::Result<()> { - bincode::encode_into(event, &mut output, SizeLimit::Infinite).map_err(|_| ::IlcError::BincodeEncode) + bincode::encode_into(event, &mut output, SizeLimit::Infinite) + .map_err(|_| ::IlcError::BincodeEncode) } } diff --git a/src/format/mod.rs b/src/format/mod.rs index 2c271bd..0716dba 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -2,7 +2,7 @@ //! As the source format may not provide the same information as the //! target format, all formats must allow for omittable information. -use std::io::{ self, BufRead, Write }; +use std::io::{ BufRead, Write }; use log::Event; diff --git a/src/format/weechat3.rs b/src/format/weechat3.rs index 59bb072..f319ace 100644 --- a/src/format/weechat3.rs +++ b/src/format/weechat3.rs @@ -1,4 +1,4 @@ -use std::io::{ self, BufRead, Write }; +use std::io::{ BufRead, Write }; use std::borrow::ToOwned; use std::iter::{ Iterator }; @@ -58,6 +58,10 @@ impl<R> Iterator for Iter<R> where R: BufRead { nick: nick.to_owned(), channel: channel.to_owned(), mask: mask(host), reason: mask(&join(reason, &split_tokens[8..])), time: timestamp(date, time) })), + [date, time, "<--", nick, host, "has", "quit", reason..] => return Some(Ok(Event::Quit { + nick: nick.to_owned(), mask: mask(host), + reason: mask(&join(reason, &split_tokens[7..])), time: timestamp(date, time) + })), [date, time, "--", notice, content..] if notice.starts_with("Notice(") => return Some(Ok(Event::Notice { @@ -68,6 +72,11 @@ impl<R> Iterator for Iter<R> where R: BufRead { [date, time, "--", "irc:", "disconnected", "from", "server", _..] => return Some(Ok(Event::Disconnect { time: timestamp(date, time) })), + [date, time, "--", nick, verb, "now", "known", "as", new_nick] + if verb == "is" || verb == "are" + => return Some(Ok(Event::Nick { + old: nick.to_owned(), new: new_nick.to_owned(), time: timestamp(date, time) + })), [date, time, sp, "*", nick, msg..] if sp.is_empty() => return Some(Ok(Event::Action { 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) + } +} @@ -1,5 +1,6 @@ #![feature(plugin, slice_patterns, core, custom_derive)] #![plugin(regex_macros)] +#![cfg_attr(feature = "lints", plugin(clippy))] extern crate regex; extern crate chrono; #[macro_use] |