Last active
December 14, 2019 22:26
-
-
Save gibizer/c28aa131da17ecfb3e79aa7978bddd44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::fs::File; | |
use std::io::Read; | |
fn main() { | |
let mut counter = HashMap::new(); | |
let mut f = File::open("sherlock.txt") | |
.expect("cannot open file"); | |
let mut text = String::new(); | |
f.read_to_string(&mut text).expect("non UTF8 content"); | |
for s in text.split(|c: char| ! c.is_alphabetic()).filter(|&c| c != "") { | |
let word = s.to_lowercase(); | |
let count = counter.entry(word).or_insert(0); | |
*count += 1; | |
} | |
let mut entries: Vec<_> = counter.into_iter().collect(); | |
entries.sort_by(|a, b| b.1.cmp(&a.1)); | |
for e in entries.iter().take(20) { | |
println!("{} {}", e.0, e.1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment