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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::collections::HashSet;
use std::iter::FromIterator;

static KEYBOARDS: [&str; 2] = [
   "abcdefghijklmnopqrstuvwxyz",
   "abcdefghijklmnopqrstuvwxyzñõàèìòùäëïöüÿâêîôû",
];
static KEYBOARD_VOWELS: [&str; 2] = [
   "aeiouy",
   "aeiouyõàèìòùäëïöüÿâêîôû",
];
static KEYBOARD_CONSONANTS: [&str; 2] = [
   "bcdfghjklmnpqrstvwxz",
   "bcdfghjklmnpqrstvwxzñ",
];

pub fn detect_keyboard(s: &str) -> usize {
   let mut best = (0, 0);
   for ki in 0..KEYBOARDS.len() {
      let count_contains = s.chars().filter(|c| KEYBOARDS[ki].contains(*c)).count();
      if count_contains==s.len() {
         return ki;
      }
      if best.1 < count_contains {
         best = (ki, count_contains);
      }
   }
   best.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 ateji_mistakes(s: &str) -> Vec<String> {
   let s = s.chars().collect::<Vec<char>>();
   let mut ts = Vec::new();
   for oi in 0..s.len() {
      let c = s[oi];
      if let Some(cs) = cjk::JOUYOU_ATEJI_INDEX.get(&c) {
         for ct in cs.iter() {
            ts.push(format!("{}{}{}", String::from_iter(&s[..oi]), ct, String::from_iter(&s[oi+1..])));
         }
      }
   }
   ts
}

pub fn contraction_mistakes(s: &str) -> Vec<String> {
   let s = s.chars().collect::<Vec<char>>();
   let mut ts = Vec::new();
   if let Some(ai) = s.iter().position(|&c| c=='\'') {
      if ai > 0 {
         ts.push(format!("{}{}", String::from_iter(&s[..ai-1]), String::from_iter(&s[ai..]))); //missing preceding letter
         ts.push(format!("{}{}{}", String::from_iter(&s[..ai]), &s[ai-1], String::from_iter(&s[ai..]))); //duplicate preceding letter
      }
      if ai+2 <= s.len() {
         ts.push(format!("{}{}", String::from_iter(&s[..ai+1]), String::from_iter(&s[ai+2..]))); //missing following letter
         ts.push(format!("{}{}{}", String::from_iter(&s[..ai+1]), &s[ai+1], String::from_iter(&s[ai+1..]))); //duplicate following letter
         ts.push(format!("{}{}{}{}", String::from_iter(&s[..ai]), &s[ai+1], "'", String::from_iter(&s[ai+2..]))); //late apostrophe
      }
      if ai > 0 && ai+1 <= s.len() {
         ts.push(format!("{}{}{}{}", String::from_iter(&s[..ai-1]), "'", &s[ai-1], String::from_iter(&s[ai+1..]))); //early apostrophe
      }
   }
   ts
}

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

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

pub fn typos(s: &str) -> Vec<String> {
   let kb = detect_keyboard_layout(s);
   let s = s.chars().collect::<Vec<char>>();
   let mut ts = Vec::new();
   for oi in 0..s.len() {
      //omit key
      ts.push(format!("{}{}", String::from_iter(&s[..oi]), String::from_iter(&s[oi+1..])));
   }
   for oi in 0..s.len() {
      //replace key
      for k in kb.chars() {
         ts.push(format!("{}{}{}", String::from_iter(&s[..oi]), k, String::from_iter(&s[oi+1..])));
      }
   }
   for oi in 0..s.len() {
      //add key
      for k in kb.chars() {
         ts.push(format!("{}{}{}", String::from_iter(&s[..oi]), k, String::from_iter(&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);
   }
   for w in contraction_mistakes(s).into_iter() {
      ms.insert(w);
   }
   for w in ateji_mistakes(s).into_iter() {
      ms.insert(w);
   }
   if ms.contains(s) {
      ms.remove(s);
   }
   ms
}