-
Notifications
You must be signed in to change notification settings - Fork 20
/
mispeller.py
98 lines (88 loc) · 2.17 KB
/
mispeller.py
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
import random
kbloc = [
[x for x in "1234567890-="],
[x for x in "qwertyuiop[]"],
[x for x in "asdfghjkl:;'"],
[x for x in "zxcvbnm,.>/?"],
]
kbdict = {}
for i, l in enumerate(kbloc):
for j, k in enumerate(l):
kbdict[k] = (i, j)
sounddict = {
"a": "e",
"b": "d",
"c": "k",
"d": "g",
"e": "eh",
"f": "ph",
"g": "j",
"h": "h",
"i": "ai",
"j": "ge",
"k": "c",
"l": "ll",
"m": "n",
"n": "m",
"o": "oa",
"p": "b",
"q": "kw",
"r": "ar",
"s": "ss",
"t": "d",
"u": "you",
"v": "w",
"w": "wn",
"x": "cks",
"y": "uy",
"z": "s",
}
def mispeller(word):
if len(word) <= 6:
num = 1
else:
num = random.choice([1, 2])
wordseq = list(range(0, len(word)))
random.shuffle(wordseq)
letters = wordseq[0:num]
def mistype(string, i):
l = string[i]
if l not in kbdict:
return string
lpos = kbdict[l]
newpos = lpos
while newpos == lpos:
newpos = (
(lpos[0] + random.choice([-1, 0, 1])) % len(kbloc),
(lpos[1] + random.choice([-1, 0, 1])) % len(kbloc[0]),
)
string = string[0:i] + kbloc[newpos[0]][newpos[1]] + string[i + 1 :]
return string
def transpose(string, i):
j = (i + random.choice([-1, 1])) % len(string)
l = [c for c in string]
l[i], l[j] = l[j], l[i]
return "".join(l)
def randomletter(string, i):
string = (
string[0 : i + 1]
+ random.choice("abcdefghijklmnopqrstuvwxyz")
+ string[i + 1 :]
)
return string
def randomreplace(string, i):
string = (
string[0:i] + random.choice("abcdefghijklmnopqrstuvwxyz") + string[i + 1 :]
)
return string
def soundalike(string, i):
try:
c = sounddict[string[i]]
except:
return string
string = string[0:i] + c + string[i + 1 :]
return string
func = random.choice([mistype, transpose, randomletter, randomreplace, soundalike])
for i in letters:
word = func(word, i)
return word