diff options
| author | tslil clingman <tslil@posteo.de> | 2022-09-29 22:55:01 +0200 |
|---|---|---|
| committer | tslil clingman <tslil@posteo.de> | 2022-09-29 22:55:01 +0200 |
| commit | ab985ecb4cb6bcc8a65ff79aa746a1f80fa4b2f4 (patch) | |
| tree | 876352f82e51723ac1cbadeda272c3adbef00ae5 /srchr/src/corpus.rs | |
| parent | 785fa20b4e4a5372857f6f033b9d478685460cea (diff) | |
Refactor code
Diffstat (limited to 'srchr/src/corpus.rs')
| -rw-r--r-- | srchr/src/corpus.rs | 123 |
1 files changed, 14 insertions, 109 deletions
diff --git a/srchr/src/corpus.rs b/srchr/src/corpus.rs index 8e4b437..eecb122 100644 --- a/srchr/src/corpus.rs +++ b/srchr/src/corpus.rs @@ -1,7 +1,7 @@ use std::fmt; use std::fs; -use crate::layout::*; +use crate::config::*; const fn build_lookup_table() -> [usize; 128] { let mut result = [0; 128]; @@ -34,6 +34,18 @@ impl Corpus { self.character_count[c as usize] } + pub fn get_bigram_count(&self, x: char, y: char) -> u32 { + self.bigram_count[pair_to_index(x, y)] + } + + pub fn get_index_threshold(&self) -> u32 { + self.index_threshold + } + + pub fn get_total_count(&self) -> u32 { + self.total_count + } + pub fn load(path: &str) -> Result<Corpus, std::io::Error> { let contents = fs::read_to_string(path)?; @@ -59,7 +71,7 @@ impl Corpus { } } - let index_threshold = (total_count as f32 * 0.15) as u32; + let index_threshold = (total_count as f32 * DESIRED_INDEX_USAGE_PERCENT) as u32; return Ok(Corpus { bigram_count, @@ -68,113 +80,6 @@ impl Corpus { index_threshold, }); } - - pub fn prelayout_fitness(&self, layout: &Prelayout) -> u32 { - let mut score: u32 = 0; - - for i in 0..6 { - let keys = layout.get_standard_column(i); - let k1 = keys[0]; - let k2 = keys[1]; - let k3 = keys[2]; - - score += self.bigram_count[pair_to_index(k1, k2)] - + self.bigram_count[pair_to_index(k1, k3)] - + self.bigram_count[pair_to_index(k2, k3)]; - } - - for i in 0..2 { - let keys = layout.get_index_column(i); - let k1 = keys[0]; - let k2 = keys[1]; - let k3 = keys[2]; - let k4 = keys[3]; - let k5 = keys[4]; - let k6 = keys[5]; - - // We want index usage! - let index_count: u32 = keys.iter().map(|&k| self.character_count[k as usize]).sum(); - score += (self.index_threshold as i64 - index_count as i64).abs() as u32 / 64; - - score += self.bigram_count[pair_to_index(k1, k2)] - + self.bigram_count[pair_to_index(k1, k3)] - + self.bigram_count[pair_to_index(k2, k3)] - + self.bigram_count[pair_to_index(k4, k5)] - + self.bigram_count[pair_to_index(k4, k6)] - + self.bigram_count[pair_to_index(k5, k6)] - + self.bigram_count[pair_to_index(k1, k4)] - + self.bigram_count[pair_to_index(k1, k5)] - + self.bigram_count[pair_to_index(k1, k6)] - + self.bigram_count[pair_to_index(k2, k4)] - + self.bigram_count[pair_to_index(k2, k5)] - + self.bigram_count[pair_to_index(k2, k6)] - + self.bigram_count[pair_to_index(k3, k4)] - + self.bigram_count[pair_to_index(k3, k5)] - + self.bigram_count[pair_to_index(k3, k6)]; - } - - score - } - - pub fn evaluate_layout(&self, layout: &Layout) -> Evaluation { - let mut keypress: [u32; NUM_KEYS] = [0; NUM_KEYS]; - let mut sfb: [u32; 8] = [0; 8]; - - for (i, &c) in self.character_count.iter().enumerate() { - if c > 0 { - keypress[layout.get_index((i as u8) as char)] = c; - } - } - - // TODO: We make assumptions about NUM_KEYS here - for i in 0..8 { - let ind = if i < 4 { i } else { i + 2 }; - - let k1 = layout.get_key(ind + 10 * 0); - let k2 = layout.get_key(ind + 10 * 1); - let k3 = layout.get_key(ind + 10 * 2); - - sfb[i] = self.bigram_count[pair_to_index(k1, k2)] - + self.bigram_count[pair_to_index(k1, k3)] - + self.bigram_count[pair_to_index(k2, k3)]; - - if i == 3 { - let k4 = layout.get_key(4 + 10 * 0); - let k5 = layout.get_key(4 + 10 * 1); - let k6 = layout.get_key(4 + 10 * 2); - sfb[i] += self.bigram_count[pair_to_index(k4, k5)] - + self.bigram_count[pair_to_index(k4, k6)] - + self.bigram_count[pair_to_index(k5, k6)] - + self.bigram_count[pair_to_index(k1, k4)] - + self.bigram_count[pair_to_index(k1, k5)] - + self.bigram_count[pair_to_index(k1, k6)] - + self.bigram_count[pair_to_index(k2, k4)] - + self.bigram_count[pair_to_index(k2, k5)] - + self.bigram_count[pair_to_index(k2, k6)] - + self.bigram_count[pair_to_index(k3, k4)] - + self.bigram_count[pair_to_index(k3, k5)] - + self.bigram_count[pair_to_index(k3, k6)]; - } else if i == 4 { - let k4 = layout.get_key(5 + 10 * 0); - let k5 = layout.get_key(5 + 10 * 1); - let k6 = layout.get_key(5 + 10 * 2); - sfb[i] += self.bigram_count[pair_to_index(k4, k5)] - + self.bigram_count[pair_to_index(k5, k6)] - + self.bigram_count[pair_to_index(k4, k6)] - + self.bigram_count[pair_to_index(k1, k4)] - + self.bigram_count[pair_to_index(k1, k5)] - + self.bigram_count[pair_to_index(k1, k6)] - + self.bigram_count[pair_to_index(k2, k4)] - + self.bigram_count[pair_to_index(k2, k5)] - + self.bigram_count[pair_to_index(k2, k6)] - + self.bigram_count[pair_to_index(k3, k4)] - + self.bigram_count[pair_to_index(k3, k5)] - + self.bigram_count[pair_to_index(k3, k6)]; - } - } - - return Evaluation::new(keypress, self.total_count, sfb); - } } fn dump_bigrams(corpus: &Corpus) -> Vec<(String, u32)> { |
