Skip to content

Instantly share code, notes, and snippets.

@lamalex
Last active July 5, 2020 18:34
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 lamalex/f40fc41472e2431df05e6f915c4f96dc to your computer and use it in GitHub Desktop.
Save lamalex/f40fc41472e2431df05e6f915c4f96dc to your computer and use it in GitHub Desktop.
The civil war was definitely about slavery.
use regex::Regex;
use std::{env, fs};
use std::collections::HashMap;
fn main() {
let args: Vec<String> = env::args().collect();
let filename = &args[1];
let contents = fs::read_to_string(filename)
.expect("Something went wrong reading the file");
let re = Regex::new(r"\b[a-zA-z]*").unwrap();
let mut word_count = HashMap::<String, u32>::new();
for word in contents.to_lowercase().split_whitespace() {
for captures in re.captures_iter(word) {
let word = &captures[0].to_owned();
if word.len() < 5 {
continue;
}
if word_count.contains_key(word) {
word_count.insert(word.to_string(), word_count[word] + 1);
} else {
word_count.insert(word.to_string(), 1);
}
}
}
let mut count_vec: Vec<(&String, &u32)> = word_count.iter().collect();
count_vec.sort_by(|a, b| b.1.cmp(a.1));
println!("{:?}", count_vec);
}
@lamalex
Copy link
Author

lamalex commented Jul 3, 2020

Written to end a facebook comment argument about southern secession. TL;DR "slave", and "slavery" are very common words in the articles of secession.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment