From 785fa20b4e4a5372857f6f033b9d478685460cea Mon Sep 17 00:00:00 2001 From: tslil clingman Date: Wed, 28 Sep 2022 00:12:11 +0200 Subject: Throwing in kicad stuff --- srchr/src/layout.rs | 363 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 363 insertions(+) create mode 100644 srchr/src/layout.rs (limited to 'srchr/src/layout.rs') diff --git a/srchr/src/layout.rs b/srchr/src/layout.rs new file mode 100644 index 0000000..9662b84 --- /dev/null +++ b/srchr/src/layout.rs @@ -0,0 +1,363 @@ +use crate::corpus::*; + +use rand::prelude::*; +use std::fmt; + +pub const NUM_KEYS: usize = 30; +pub const ROW_LENGTH: usize = 10; + +pub const KEY_CHARS: [char; NUM_KEYS] = [ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', + 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '/', '.', ',', '\'', +]; + +pub fn canonicalise(inp: char) -> Option { + if inp.is_ascii_alphabetic() { + return Some(inp.to_ascii_uppercase()); + } else { + match inp { + '.' => Some('.'), + '>' => Some('.'), + ',' => Some(','), + '<' => Some(','), + '/' => Some('/'), + '?' => Some('/'), + '\'' => Some('\''), + '"' => Some('\''), + _ => None, + } + } +} + +#[derive(Copy, Clone)] +pub struct Prelayout { + standard_columns: [[char; 3]; 6], + index_columns: [[char; 6]; 2], +} + +impl Prelayout { + pub fn get_standard_column(&self, index: usize) -> &[char; 3] { + &self.standard_columns[index] + } + + pub fn get_index_column(&self, index: usize) -> &[char; 6] { + &self.index_columns[index] + } + + pub fn new_random_from(pl: &Prelayout, rng: &mut R) -> Prelayout { + let mut standard_columns = pl.standard_columns.clone(); + let mut index_columns = pl.index_columns.clone(); + + let mut count = rng.gen_range(1..NUM_KEYS); + while count > 0 { + let source_index: bool = rng.gen(); + let target_index: bool = rng.gen(); + + let saved; + let target_col: usize; + let target_idx: usize; + if target_index { + target_col = rng.gen_range(0..2); + target_idx = rng.gen_range(0..6); + saved = index_columns[target_col][target_idx]; + } else { + target_col = rng.gen_range(0..6); + target_idx = rng.gen_range(0..3); + saved = standard_columns[target_col][target_idx]; + } + + let source_col: usize; + let source_idx: usize; + if source_index { + source_col = rng.gen_range(0..2); + source_idx = rng.gen_range(0..6); + if target_index { + index_columns[target_col][target_idx] = index_columns[source_col][source_idx]; + } else { + standard_columns[target_col][target_idx] = + index_columns[source_col][source_idx]; + } + index_columns[source_col][source_idx] = saved; + } else { + source_col = rng.gen_range(0..6); + source_idx = rng.gen_range(0..3); + if target_index { + index_columns[target_col][target_idx] = + standard_columns[source_col][source_idx]; + } else { + standard_columns[target_col][target_idx] = + standard_columns[source_col][source_idx]; + } + standard_columns[source_col][source_idx] = saved; + } + count -= 1; + } + Prelayout { + standard_columns, + index_columns, + } + } + + fn from_char_array(ca: &[char; NUM_KEYS]) -> Prelayout { + let mut standard_columns = [['x'; 3]; 6]; + let mut index_columns = [['x'; 6]; 2]; + + for i in 0..6 { + let ind = if i < 3 { i } else { i + 4 }; + for j in 0..3 { + standard_columns[i][j] = ca[ind + j * ROW_LENGTH]; + } + } + + for j in 0..3 { + index_columns[0][j] = ca[3 + j * ROW_LENGTH]; + index_columns[1][j] = ca[6 + j * ROW_LENGTH]; + index_columns[0][j + 3] = ca[4 + j * ROW_LENGTH]; + index_columns[1][j + 3] = ca[5 + j * ROW_LENGTH]; + } + + Prelayout { + standard_columns, + index_columns, + } + } +} + +const KEY_TO_FINGER: [usize; NUM_KEYS] = [ + 0, 1, 2, 3, 3, 4, 4, 5, 6, 7, 0, 1, 2, 3, 3, 4, 4, 5, 6, 7, 0, 1, 2, 3, 3, 4, 4, 5, 6, 7, +]; + +#[derive(Copy, Clone)] +pub struct Layout { + keys: [char; NUM_KEYS], +} + +impl Layout { + pub fn as_prelayout(&self) -> Prelayout { + Prelayout::from_char_array(&self.keys) + } + + pub fn from_prelayout(pl: &Prelayout, corpus: &Corpus) -> Layout { + fn weight_function( + columns: &[[char; N]; M], + corpus: &Corpus, + ) -> Vec<(Vec, u32)> { + let mut result = columns + .iter() + .map(|col| { + let mut weight = 0; + let mut wcol: Vec<(char, u32)> = col + .iter() + .map(|&c| { + let w = corpus.get_character_count(c); + weight += w; + (c, w) + }) + .collect(); + wcol.sort_by(|(_, l), (_, r)| r.cmp(l)); + wcol.swap(0, 1); + if N == 6 { + wcol.swap(3, 4); + } + (wcol.into_iter().map(|(k, _)| k).collect(), weight) + }) + .collect::, u32)>>(); + result.sort_by(|(_, l), (_, r)| r.cmp(l)); + result + } + + let mut balance: i64 = 0; + let mut left_col: usize = 0; + let mut right_col: usize = 9; + let mut keys = ['x'; NUM_KEYS]; + + let mut w_standard_columns = weight_function(&pl.standard_columns, corpus); + while let Some((col, weight)) = w_standard_columns.pop() { + let left: bool = ((balance >= 0) && (left_col <= 2)) || (right_col <= 6); + let ind = if left { left_col } else { right_col }; + for i in 0..3 { + keys[ind + i * ROW_LENGTH] = col[i]; + } + if left { + left_col += 1; + } else { + right_col -= 1; + } + balance += if left { + -(weight as i64) + } else { + weight as i64 + }; + } + + let w_index_columns = weight_function(&pl.index_columns, corpus); + let (left_ind, right_ind) = if balance >= 0 { (0, 1) } else { (1, 0) }; + for j in 0..3 { + keys[3 + j * ROW_LENGTH] = w_index_columns[left_ind].0[j]; + keys[6 + j * ROW_LENGTH] = w_index_columns[right_ind].0[j]; + keys[4 + j * ROW_LENGTH] = w_index_columns[left_ind].0[j + 3]; + keys[5 + j * ROW_LENGTH] = w_index_columns[right_ind].0[j + 3]; + } + + Layout { keys } + } + + pub fn get_index(&self, c: char) -> usize { + let mut found_key = 0; + while self.keys[found_key] != c { + found_key += 1 + } + found_key + } + + fn char_array_to_layout(keys: [char; NUM_KEYS]) -> Layout { + return Layout { keys }; + } + + pub fn get_key(&self, index: usize) -> char { + self.keys[index] + } + + pub fn from_verbose(inp: &str) -> Option { + let mut layout: [char; NUM_KEYS] = ['x'; NUM_KEYS]; + + let mut k: usize = 0; + for c in inp.chars() { + let valid = (c.is_uppercase() && c.is_alphabetic()) + || c == '.' + || c == '/' + || c == ',' + || c == '\''; + if valid { + layout[k] = c; + k += 1; + } + if k > NUM_KEYS { + return None; + } + } + + return Some(Layout::char_array_to_layout(layout)); + } +} + +impl fmt::Display for Layout { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str(&format_block_output(self.keys.into_iter())) + } +} + +#[derive(Copy, Clone)] +pub struct Evaluation { + keypress: [u32; NUM_KEYS], + total_keypress: u32, + sfb: [u32; 8], +} + +impl Evaluation { + pub fn new(keypress: [u32; NUM_KEYS], total_keypress: u32, sfb: [u32; 8]) -> Evaluation { + Evaluation { + keypress, + sfb, + total_keypress, + } + } + + fn output_eval(&self) -> String { + let mut result = String::new(); + let tot = self.total_keypress as f32; + + result += &"Percent per key:\n"; + result += &format_block_output(self.keypress.into_iter().map(|k| 100.0 * k as f32 / tot)); + + let mut finger_usages = [0; 8]; + for (i, &c) in self.keypress.iter().enumerate() { + finger_usages[KEY_TO_FINGER[i]] += c; + } + + result += "\nFinger usage: "; + let mut lh: f32 = 0.0; + let mut rh: f32 = 0.0; + for (i, &u) in finger_usages.iter().enumerate() { + let f = u as f32 / tot * 100.0; + result += &format!("{:>5.2}%{}", f, if i < 7 { ", " } else { "" }); + if i % 10 < 4 { + lh += f; + } else { + rh += f; + } + } + + result += &format!("\nHand usage: {:.2}% vs {:.2}%", lh, rh); + + result += "\nSame finger bigrams: "; + for (i, &u) in self.sfb.iter().enumerate() { + result += &format!( + "{:>6.3}%{}", + u as f32 / tot * 100.0, + if i < 7 { ", " } else { "" } + ); + } + + let sfb = self.sfb.iter().sum::(); + result += &format!("\nTotal sfb: {:.2}% ({})", sfb as f32 / tot * 100.0, sfb); + + return result; + } +} + +impl fmt::Display for Evaluation { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str(&self.output_eval()) + } +} + +// This is a silly amount of work to genericise the below... +trait ToMyString { + const BLANK_STRING: &'static str; + fn to_my_string(&self) -> String; +} + +impl ToMyString for char { + const BLANK_STRING: &'static str = " "; + fn to_my_string(&self) -> String { + self.to_string() + } +} + +impl ToMyString for u32 { + const BLANK_STRING: &'static str = " "; + fn to_my_string(&self) -> String { + format!("{:5}", *self) + } +} + +impl ToMyString for f32 { + const BLANK_STRING: &'static str = " "; + fn to_my_string(&self) -> String { + format!("{:>4.1}", *self) + } +} + +fn format_block_output, S: ToMyString>(things: T) -> String { + let mut result = String::new(); + for (i, t) in things.enumerate() { + result += &t.to_my_string(); + if i < NUM_KEYS - 1 { + result.push(' '); + } + if (i + 1) % 10 == 0 { + result.push('\n'); + if NUM_KEYS < 30 && i == 19 { + result += S::BLANK_STRING; + result.push(' ') + } + } else if (i < 20 && (i + 1) % 5 == 0) || (i == (NUM_KEYS - 20) / 2 + 19) { + result.push(' '); + } + if i + 1 >= NUM_KEYS { + break; + } + } + return result; +} -- cgit v1.3.1