1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use std::collections::HashSet;

static KEYBOARDS: [&str; 1] = [
   "abcdefghijklmnopqrstuvwxyz",
];
static KEYBOARD_VOWELS: [&str; 1] = [
   "aeiouy",
];
static KEYBOARD_CONSONANTS: [&str; 1] = [
   "bcdfghjklmnpqrstvwxz",
];

pub fn detect_keyboard(_s: &str) -> usize {
   0
}
pub fn detect_keyboard_layout(s: &str) -> &str {
   KEYBOARDS[detect_keyboard(s)]
}
pub fn detect_keyboard_vowels(s: &str) -> &str {
   KEYBOARD_VOWELS[detect_keyboard(s)]
}
pub fn detect_keyboard_consonants(s: &str) -> &str {
   KEYBOARD_CONSONANTS[detect_keyboard(s)]
}

pub fn consonant_mistakes(s: &str) -> Vec<String> {
   let kb = detect_keyboard_consonants(s);
   let mut ts = Vec::new();
   for oi in 0..s.len() {
      //replace consonant, add, or omit
      if !kb.contains(&s[oi..oi+1]) { continue; }
      ts.push(format!("{}{}", &s[..oi], &s[oi+1..]));
      for k in kb.chars() {
         ts.push(format!("{}{}{}", &s[..oi], k, &s[oi+1..]));
         ts.push(format!("{}{}{}", &s[..oi], k, &s[oi..]));
         ts.push(format!("{}{}{}", &s[..oi+1], k, &s[oi+1..]));
      }
   }
   ts
}

pub fn vowel_mistakes(s: &str) -> Vec<String> {
   let kb = detect_keyboard_vowels(s);
   let mut ts = Vec::new();
   for oi in 0..s.len() {
      //replace vowel, add, or omit
      if !kb.contains(&s[oi..oi+1]) { continue; }
      ts.push(format!("{}{}", &s[..oi], &s[oi+1..]));
      for k1 in kb.chars() {
         ts.push(format!("{}{}{}", &s[..oi], k1, &s[oi+1..]));
         ts.push(format!("{}{}{}", &s[..oi], k1, &s[oi..]));
         ts.push(format!("{}{}{}", &s[..oi+1], k1, &s[oi+1..]));
         for k2 in kb.chars() {
            ts.push(format!("{}{}{}{}", &s[..oi], k1, k2, &s[oi..]));
         }
      }
   }
   ts
}

pub fn typos(s: &str) -> Vec<String> {
   let kb = detect_keyboard_layout(s);
   let mut ts = Vec::new();
   for oi in 0..s.len() {
      //omit key
      ts.push(format!("{}{}", &s[..oi], &s[oi+1..]));
   }
   for oi in 0..s.len() {
      //replace key
      for k in kb.chars() {
         ts.push(format!("{}{}{}", &s[..oi], k, &s[oi+1..]));
      }
   }
   for oi in 0..s.len() {
      //add key
      for k in kb.chars() {
         ts.push(format!("{}{}{}", &s[..oi], k, &s[oi..]));
      }
   }
   ts
}

pub fn misspell(s: &str) -> HashSet<String> {
   let mut ms = HashSet::new();
   for w in typos(s).into_iter() {
      ms.insert(w);
   }
   for w in vowel_mistakes(s).into_iter() {
      ms.insert(w);
   }
   for w in consonant_mistakes(s).into_iter() {
      ms.insert(w);
   }
   if ms.contains(s) {
      ms.remove(s);
   }
   ms
}