Last active
July 5, 2020 18:34
-
-
Save lamalex/f40fc41472e2431df05e6f915c4f96dc to your computer and use it in GitHub Desktop.
The civil war was definitely about slavery.
This file contains hidden or 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 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Written to end a facebook comment argument about southern secession. TL;DR "slave", and "slavery" are very common words in the articles of secession.