use rand::{rngs::StdRng, Rng};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Keystroke {
pub ch: char,
pub hold_ms: u16,
pub gap_ms_before: u16,
pub is_correction: bool,
}
const HOT_BIGRAMS: &[(&str, u16, u16)] = &[
("th", 60, 100),
("he", 65, 105),
("in", 70, 110),
("er", 70, 115),
("an", 70, 115),
("re", 75, 120),
("on", 80, 125),
("at", 80, 125),
("en", 80, 130),
("nd", 80, 130),
("ti", 85, 130),
("es", 85, 135),
("or", 85, 135),
("te", 85, 135),
("of", 90, 140),
("ed", 90, 140),
("is", 90, 140),
("it", 90, 145),
("al", 95, 145),
("ar", 95, 145),
("st", 95, 150),
("to", 95, 150),
("nt", 100, 150),
("ng", 100, 155),
("se", 100, 155),
("ha", 100, 155),
("as", 105, 160),
("ou", 105, 160),
("io", 105, 160),
("le", 105, 165),
("ve", 110, 170),
("co", 115, 175),
("me", 115, 175),
("de", 115, 180),
("hi", 120, 180),
("ri", 120, 180),
("ro", 125, 185),
("ic", 125, 190),
("ne", 130, 195),
("ea", 130, 200),
];
pub const COLD_BIGRAM_GAP_MIN_MS: u16 = 180;
pub const COLD_BIGRAM_GAP_MAX_MS: u16 = 320;
pub const SPACE_GAP_MIN_MS: u16 = 90;
pub const SPACE_GAP_MAX_MS: u16 = 170;
pub const DIGIT_GAP_MIN_MS: u16 = 200;
pub const DIGIT_GAP_MAX_MS: u16 = 360;
pub fn bigram_gap(prev: char, next: char) -> (u16, u16) {
if next == ' ' || next == '\t' {
return (SPACE_GAP_MIN_MS, SPACE_GAP_MAX_MS);
}
if prev.is_ascii_digit() && next.is_ascii_digit() {
return (DIGIT_GAP_MIN_MS, DIGIT_GAP_MAX_MS);
}
let key = format!("{}{}", prev.to_ascii_lowercase(), next.to_ascii_lowercase());
if let Some(&(_, lo, hi)) = HOT_BIGRAMS.iter().find(|(k, _, _)| *k == key) {
return (lo, hi);
}
(COLD_BIGRAM_GAP_MIN_MS, COLD_BIGRAM_GAP_MAX_MS)
}
pub fn hold_envelope(ch: char) -> (u16, u16) {
if ch.is_ascii_uppercase() {
return (50, 100);
}
if ch.is_ascii_digit() {
return (45, 90);
}
if ch == ' ' {
return (35, 80);
}
(30, 75)
}
pub fn qwerty_neighbour(ch: char, rng_seed: u8) -> Option<char> {
let neighbours: &[char] = match ch.to_ascii_lowercase() {
'q' => &['w', 'a'],
'w' => &['q', 'e', 's'],
'e' => &['w', 'r', 'd'],
'r' => &['e', 't', 'f'],
't' => &['r', 'y', 'g'],
'y' => &['t', 'u', 'h'],
'u' => &['y', 'i', 'j'],
'i' => &['u', 'o', 'k'],
'o' => &['i', 'p', 'l'],
'p' => &['o', 'l'],
'a' => &['q', 's', 'z'],
's' => &['a', 'd', 'w', 'x'],
'd' => &['s', 'f', 'e', 'c'],
'f' => &['d', 'g', 'r', 'v'],
'g' => &['f', 'h', 't', 'b'],
'h' => &['g', 'j', 'y', 'n', 'b', ' '],
'j' => &['h', 'k', 'u', 'm'],
'k' => &['j', 'l', 'i'],
'l' => &['k', 'o', 'p'],
'z' => &['a', 'x'],
'x' => &['z', 'c', 's'],
'c' => &['x', 'v', 'd'],
'v' => &['c', 'b', 'f'],
'b' => &['v', 'n', 'g', 'h'],
'n' => &['b', 'm', 'h', 'j'],
'm' => &['n', 'j', 'k'],
_ => return None,
};
Some(neighbours[(rng_seed as usize) % neighbours.len()])
}
#[derive(Debug, Clone, Copy)]
pub struct TypingPlan {
pub typo_probability: f32,
pub thinking_pause_probability: f32,
}
impl Default for TypingPlan {
fn default() -> Self {
Self {
typo_probability: 0.015,
thinking_pause_probability: 0.10,
}
}
}
pub fn plan_keystrokes(text: &str, plan: TypingPlan, rng: &mut StdRng) -> Vec<Keystroke> {
let chars: Vec<char> = text.chars().collect();
let mut out: Vec<Keystroke> = Vec::with_capacity(chars.len() + 8);
for (i, &ch) in chars.iter().enumerate() {
let inject_typo = plan.typo_probability > 0.0
&& rng.gen::<f32>() < plan.typo_probability
&& ch.is_ascii_alphabetic();
if inject_typo {
if let Some(neighbour) = qwerty_neighbour(ch, rng.gen()) {
let (h_lo, h_hi) = hold_envelope(neighbour);
let prev = if i == 0 { ' ' } else { chars[i - 1] };
let (g_lo, g_hi) = bigram_gap(prev, neighbour);
let typo_gap = if i == 0 {
0
} else {
rng.gen_range(g_lo..=g_hi)
};
out.push(Keystroke {
ch: neighbour,
hold_ms: rng.gen_range(h_lo..=h_hi),
gap_ms_before: typo_gap,
is_correction: true,
});
out.push(Keystroke {
ch: '\u{0008}', hold_ms: rng.gen_range(35..=70),
gap_ms_before: rng.gen_range(80..=250),
is_correction: true,
});
}
}
let (h_lo, h_hi) = hold_envelope(ch);
let gap = if out.is_empty() {
0
} else if let Some(last) = out.last() {
if last.is_correction && last.ch == '\u{0008}' {
rng.gen_range(60..=180)
} else {
let prev = chars[i - 1];
let (g_lo, g_hi) = bigram_gap(prev, ch);
let mut g = rng.gen_range(g_lo..=g_hi);
if plan.thinking_pause_probability > 0.0
&& rng.gen::<f32>() < plan.thinking_pause_probability
{
g = g.saturating_add(rng.gen_range(200..=600));
}
g
}
} else {
0
};
out.push(Keystroke {
ch,
hold_ms: rng.gen_range(h_lo..=h_hi),
gap_ms_before: gap,
is_correction: false,
});
}
out
}
#[cfg(test)]
mod tests;