Skip to content

Instantly share code, notes, and snippets.

@stefanoc
Created October 29, 2014 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanoc/52537ef37232d0c1d850 to your computer and use it in GitHub Desktop.
Save stefanoc/52537ef37232d0c1d850 to your computer and use it in GitHub Desktop.
Word freqs
require 'pp'
dict = Hash.new(0)
File.open(ARGV[0], 'r') do |src|
while line = src.gets
line.strip.scan(/\w+/) { |word| dict[word] += 1 }
end
end
pp dict
#![feature(phase)]
extern crate regex;
#[phase(plugin)] extern crate regex_macros;
use std::collections::HashMap;
use std::collections::hashmap::{Vacant, Occupied};
use std::io::fs::File;
use std::io::BufferedReader;
use std::os::args;
fn main() {
let mut dict = HashMap::<String, uint>::new();
let re = regex!(r"\w+");
let mut lines = 0u;
let filename = match args().len() {
1 => fail!("You must enter a filename to read line by line"),
_ => args()[1].clone()
};
let file = File::open(&Path::new(filename.as_slice()));
let mut reader = BufferedReader::new(file);
for line in reader.lines() {
match line {
Ok(line) => {
lines += 1;
for pos in re.find_iter(line.as_slice()) {
let (a, b) = pos;
let word = line.as_slice().slice_chars(a, b).to_string();
match dict.entry(word) {
Vacant(entry) => { entry.set(1u); },
Occupied(mut entry) => *entry.get_mut() += 1,
};
}
},
Err(err) => {
println!("ERR: {}", err);
break;
}
}
}
println!("Read {} lines", lines);
println!("{}", dict);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment