Skip to content

Instantly share code, notes, and snippets.

@krdln
Created June 10, 2014 23:54
Show Gist options
  • Save krdln/a0d3496b607b39a3be05 to your computer and use it in GitHub Desktop.
Save krdln/a0d3496b607b39a3be05 to your computer and use it in GitHub Desktop.
Zliczanie wystąpień słów (Rust master @ 2014-06-10)
use std::collections::HashMap;
use std::path::Path;
use std::io::fs::File;
use std::io::BufferedReader;
fn main() {
let path = Path::new("potop2.txt");
let reader = File::open(&path).unwrap();
let mut reader = BufferedReader::new(reader);
let mut counter : HashMap<String, int> = HashMap::new();
for line in reader.lines() {
for word in line.unwrap().as_slice().words() {
let trims = &['\n', '\t', '.', ',', '?', '!', '-', ';', ':', '\"', '(', ')', '_', '=', '\\', '\''];
let word = word.trim_chars(trims);
let word = word.chars().map(|c| c.to_lowercase()).collect();
*counter.find_or_insert(word, 0) += 1;
}
}
let mut v = Vec::new();
for (ref w, n) in counter.iter() {
v.push( (n, w.as_slice()) );
}
v.sort();
// counter.insert("dupa".to_string(), 42);
// ↑ nie skompiluje się, bo groziłoby to dyndającymi wskaźnikami w wektorze v
// yay Rust!
for &(w, n) in v.iter() {
println!("{} {}", w, n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment