Skip to content

Instantly share code, notes, and snippets.

Created October 20, 2020 02:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
use anyhow::Result;
use std::fs::File;
use std::io::{BufRead, BufReader};
use walkdir::WalkDir;
/// Inspired by
fn main() -> Result<()> {
let mut counter = std::collections::BTreeMap::new();
for entry in WalkDir::new(".")
.filter_map(|e| e.ok())
.filter(|e| e.file_name().to_string_lossy().ends_with(".txt"))
let reader = BufReader::new(File::open(entry.into_path())?);
for line in reader.lines() {
for word in line?.split_whitespace() {
let w = word.to_lowercase().to_string();
*counter.entry(w).or_insert(0) += 1;
let mut tops: Vec<_> = counter.iter().map(|(w, c)| (c, w)).collect();
tops.sort_by_key(|(&c, w)| (std::cmp::Reverse(c), *w));
for (c, w) in tops.iter().take(10) {
println!("{} - {}", w, c);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment