This commit is contained in:
Christoph J. Scherr 2024-03-22 16:03:52 +01:00
commit 8afede35b6
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
3 changed files with 11 additions and 5 deletions

View File

@ -7,7 +7,7 @@ use libpt::log::*;
use wordle_analyzer::game::response::GuessResponse; use wordle_analyzer::game::response::GuessResponse;
use wordle_analyzer::solve::{stupid, BuiltinSolverNames, NaiveSolver, Solver, StupidSolver}; use wordle_analyzer::solve::{BuiltinSolverNames, Solver};
use wordle_analyzer::wlist::builtin::BuiltinWList; use wordle_analyzer::wlist::builtin::BuiltinWList;
use wordle_analyzer::wlist::word::Word; use wordle_analyzer::wlist::word::Word;
use wordle_analyzer::{self, game}; use wordle_analyzer::{self, game};

View File

@ -47,7 +47,7 @@ pub trait WordList: Clone + std::fmt::Debug + Default {
WordMap::from(hm) WordMap::from(hm)
} }
fn get_word(&self, word: &Word) -> Option<WordData>; fn get_word(&self, word: &Word) -> Option<WordData>;
fn letter_frequency(&self) -> WordMap { fn letter_frequency(&self) -> HashMap<char, Frequency> {
// PERF: this function has complexity O(n²)! // PERF: this function has complexity O(n²)!
let mut cmap: HashMap<char, usize> = HashMap::new(); let mut cmap: HashMap<char, usize> = HashMap::new();
// count the chars in each word // count the chars in each word
@ -58,8 +58,8 @@ pub trait WordList: Clone + std::fmt::Debug + Default {
} }
} }
} }
// make all chars to strings // convert to relative frequency
let cmap: HashMap<Word, usize> = cmap.into_iter().map(|p| (p.0.to_string(), p.1)).collect(); let n: f64 = cmap.keys().len() as f64;
WordMap::from_absolute(cmap) cmap.into_iter().map(|p| (p.0, p.1 as f64 / n)).collect()
} }
} }

View File

@ -103,3 +103,9 @@ impl From<HashMap<Word, Frequency>> for WordMap {
Self { inner: value } Self { inner: value }
} }
} }
impl From<WordMap > for HashMap<Word, Frequency>{
fn from(value: WordMap) -> Self {
value.inner
}
}