use std::{ collections::HashSet, env, io::{self, Read}, process, }; use regex::Regex; const DICT: &str = include_str!("cmudict.dict"); fn main() { let mut args = env::args(); args.next().unwrap(); let min_syllables: usize = args.next().expect("one arg").parse().expect("a number"); // the vast, vast majority of discord messages are under 2000 chars, so this avoids // reallocating let mut message = String::with_capacity(2048); io::stdin() .read_to_string(&mut message) .expect("i want a message"); let word_regex = Regex::new("(\\w|-|')+").unwrap(); let mut disallowed_words: HashSet<_> = word_regex .find_iter(&message) .map(|m| m.as_str().to_ascii_lowercase()) .collect(); if disallowed_words.is_empty() { // no words is lame! process::exit(2); } for line in DICT.lines() { let Some((word, syllables)) = line.split_once(' ') else { continue; }; let syllables = syllables .split_ascii_whitespace() .filter(|s| s.ends_with(|c: char| c.is_ascii_digit())); if syllables.count() >= min_syllables { disallowed_words.remove(word); } } process::exit(i32::from(!disallowed_words.is_empty())); }