Skip to content

Instantly share code, notes, and snippets.

@archer884
Created December 1, 2014 22:10
Show Gist options
  • Save archer884/0961fe708f43718efd1a to your computer and use it in GitHub Desktop.
Save archer884/0961fe708f43718efd1a to your computer and use it in GitHub Desktop.
Rust word usage counter
#![feature(phase)]
#[phase(plugin)]
extern crate regex_macros;
extern crate regex;
use regex::Regex;
use std::ascii::AsciiExt;
use std::collections::{HashMap};
use std::collections::hash_map::Entry::{Vacant, Occupied};
use std::io::{BufferedReader, File};
static WORD_PATTERN: Regex = regex!(r"(\w)+('\w+)?");
struct Item {
word: String,
count: int,
}
fn main() {
let content = get_content();
let mut map: HashMap<String, int> = HashMap::new();
for word in content.into_iter() {
match map.entry(word) {
Occupied(mut entry) => *entry.get_mut() += 1,
Vacant(entry) => { entry.set(1); },
};
}
let mut items = Vec::new();
for (key, val) in map.iter() {
items.push(Item { word: key.to_string(), count: *val });
}
items.sort_by(|a, b| a.word.cmp(&b.word));
items.sort_by(|a, b| b.count.cmp(&a.count));
for i in items.iter() {
println!("{}: {}", i.word, i.count);
}
}
fn get_content() -> Vec<String> {
let args = std::os::args();
let text = File::open(&Path::new(&args[1])).read_to_string().unwrap();
WORD_PATTERN.captures_iter(text.as_slice())
.map(|cap| cap.at(0).to_ascii_upper())
.collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment