-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuntypo.ps1
255 lines (218 loc) · 6.78 KB
/
untypo.ps1
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
param($UserInput)
$characterTypoPatterns = @(
## The user forgot to type this character
{
param($UserInput)
""
},
## The user doubled this character
{
param($UserInput)
"$UserInput$UserInput"
},
## Fat finger a letter (one off physically)
{
param($UserInput)
$typoMap = @{
'`' = "1`t"
'1' = "``2`t"
'2' = "1qw3"
'3' = "2we4"
'4' = "3er5"
'5' = "4rt6"
'6' = "5ty7"
'7' = "6yu8"
'8' = "7ui9"
'9' = "8io0"
'0' = "9op-"
'-' = "0p[="
## Backspace is also a possible typo, but that would be handled by the
## "Forgot to type a character" filter
'=' = "-[]"
'q' = "``12`twas"
'w' = "123qeasd"
'e' = "234wrsdf"
'r' = "345etdfg"
't' = "567ryfgh"
'y' = "678tughj"
'u' = "789yihjk"
'i' = "890uojkl"
'o' = "90-ipkl;"
'p' = "0-=o[l;'"
'[' = "-=p];'`n"
']' = "=[\'`n"
'\' = "]'`n"
## Shift is also a possible typo, but that would be handled by that character
## not being typed, so covered by "Forgot to type a character" filter.
'a' = "`tqwszx"
's' = "qweadzxc"
'd' = "wersfxcv"
'f' = "ertdgcvb"
'g' = "rtyfhvbn"
'h' = "tyugjbnm"
'j' = "yuihknm,"
'k' = "uiojlm,."
'l' = "iopk;,./"
';' = "op[l'./"
'''' = "[]\;`n./"
'z' = "asx"
'x' = "asdzc"
'c' = "sdfxv "
'v' = "dfgcb "
'b' = "fghvn "
'n' = "ghjbm "
'm' = "hjkn, "
',' = "jklm."
'.' = "kl;,/"
'/' = ";'`n."
}
$capitalTypoMap = @{
'~' = "!`tQ"
'!' = "~@`tQW"
'@' = "!#QWE"
'#' = "@`$WER"
'$' = "#%ERT"
'%' = "`$^ERT"
'^' = "%&RTY"
'&' = "^*TYU"
'*' = "&(YUI"
'(' = "*)IOP"
')' = "(_OP{"
'_' = ")+P{}"
## Backspace is also a possible typo, but that would be handled by the
## "Forgot to type a character" filter
'+' = "_{}|"
'{' = ")_+P}:`"`n"
'}' = "+{|`"`n"
'|' = "+}`"`n"
':' = "OP{L`">?"
'"' = "P{}:`n>?"
'<' = "JKLM>"
'>' = "KL:<?"
'?' = ":`"`n>"
}
if($typoMap.ContainsKey($UserInput.ToString()))
{
foreach($character in $typoMap[$UserInput.ToString()].ToCharArray())
{
$character
}
}
elseif($capitalTypoMap.ContainsKey($UserInput.ToString()))
{
foreach($character in $capitalTypoMap[$UserInput.ToString()].ToCharArray())
{
$character
}
}
else
{
$UserInput
}
}
)
$globalTypoPatterns = @(
## The user accidentally had CAPS engaged at some point in the input
{
param($UserInput)
for($capsPosition = 0; $capsPosition -lt $UserInput.Length; $capsPosition++)
{
$beginning = $UserInput.SubString(0, $capsPosition)
$newChars = foreach($char in $UserInput.SubString($capsPosition, $UserInput.Length - $capsPosition).ToCharArray())
{
if([Char]::IsUpper($char)) { [Char]::ToLower($char) }
elseif([Char]::IsLower($char)) { [Char]::ToUpper($char) }
else { $char }
}
$beginning + (-join $newChars)
}
},
## Was a System.SecureString, converted to text via .ToString()
{
param($UserInput)
"System.SecureString"
}
## Had spaces before or after (maybe from pasting)
{
param($UserInput)
for($prePadding = 0; $prePadding -lt 3; $prePadding++)
{
for($postPadding = 0; $postPadding -lt 3; $postPadding++)
{
(" " * $prePadding) + $UserInput + (" " * $postPadding)
}
}
}
## Had alternate keyboard engaged
{
param($UserInput)
$inputMaps = @{
'en-US' = '`1234567890-=qwertyuiop[]\asdfghjkl;''zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?'
'es-ES' = '|1234567890''¿qwertyuiop´+}asdfghjklñ{zxcvbnm,.-°!"#$%&/()=?¡QWERTYUIOP¨*]ASDFGHJKLÑ[ZXCVBNM;:_'
'Dvorak' = '`1234567890[]'',.pyfgcrl/=\aoeuidhtns-;qjkxbmwvz~!@#$%^&*(){}"<>PYFGCRL?+|AOEUIDHTNS_:QJKXBMWVZ'
}
foreach($sourceKeyboard in $inputMaps.Keys)
{
foreach($destinationKeyboard in $inputMaps.Keys)
{
if($sourceKeyboard -eq $destinationKeyboard)
{
continue
}
$chars = foreach($inputChar in $UserInput.ToCharArray()) {
$inputIndex = $inputMaps[$sourceKeyboard].IndexOf($inputChar)
if($inputIndex -ge 0)
{
$inputMaps[$destinationKeyboard][$inputIndex]
}
else
{
$inputChar
}
}
$result = -join $chars
if($result -cne $UserInput)
{
$result
}
}
}
},
## User input got truncated to some maximum length
{
param($UserInput)
for($truncation = 0; $truncation -lt $UserInput.Length; $truncation++)
{
$UserInput.Substring(0, $truncation)
}
}
)
## Emit the input as guessed by the user.
$UserInput
## Go through character typo patterns
foreach($characterTypoPattern in $characterTypoPatterns)
{
for($charPosition = 0; $charPosition -lt $UserInput.Length; $charPosition++)
{
$preInput = $UserInput.Substring(0, $charPosition)
$postInput = ""
if($charPosition -lt ($UserInput.Length - 1))
{
$postInput = $UserInput.Substring($charPosition + 1, ($UserInput.Length - $charPosition - 1));
}
$results = & $characterTypoPattern $UserInput[$charPosition]
foreach($result in $results)
{
$preInput + $result + $postInput
}
}
}
## Go through global typo patterns
foreach($globalTypoPattern in $globalTypoPatterns)
{
$results = & $globalTypoPattern $UserInput
foreach($result in $results)
{
$result
}
}