homophones

This commit is contained in:
mehbark 2024-08-24 18:23:05 -04:00
parent 9847d79b6d
commit 7db51ed596
2 changed files with 32 additions and 8 deletions

View file

@ -2,5 +2,3 @@
name = "anagrams" name = "anagrams"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies]

View file

@ -1,4 +1,8 @@
use std::{collections::HashMap, io}; use std::{
collections::HashMap,
fs::File,
io::{self, BufRead, BufReader},
};
type CharCount = [u8; 26]; type CharCount = [u8; 26];
@ -28,17 +32,39 @@ fn pretty_char_count(count: CharCount) -> String {
} }
fn main() { fn main() {
let mut words: HashMap<CharCount, Vec<String>> = HashMap::new(); let mut dict: HashMap<String, Vec<String>> = HashMap::new();
// https://github.com/cmusphinx/cmudict
for line in BufReader::new(File::open("cmudict.dict").unwrap()).lines() {
let line = line.unwrap();
if let Some((word, phos)) = line.split_once(' ') {
let word = match word.split_once('(') {
Some((word, _)) => word,
None => word,
};
dict.entry(word.to_string())
.or_default()
.push(phos.to_owned());
}
}
let mut words: HashMap<(CharCount, String), Vec<String>> = HashMap::new();
for word in io::stdin().lines() { for word in io::stdin().lines() {
let word = word.unwrap(); let word = word.unwrap();
let count = char_count(&word); if let Some(phos) = dict.get(&word) {
words.entry(count).or_default().push(word); let count = char_count(&word);
for phos in phos {
words
.entry((count, phos.clone()))
.or_default()
.push(word.clone());
}
}
} }
for (count, words) in words { for ((count, phos), words) in words {
if words.len() == 1 { if words.len() == 1 {
continue; continue;
} }
println!("-- {}", pretty_char_count(count)); println!("-- {} {phos}", pretty_char_count(count));
for word in words { for word in words {
println!("{word}"); println!("{word}");
} }