pesterchum/mispeller.py

99 lines
2.2 KiB
Python
Raw Normal View History

2011-02-13 20:32:02 -05:00
import random
kbloc = [
[x for x in "1234567890-="],
[x for x in "qwertyuiop[]"],
[x for x in "asdfghjkl:;'"],
[x for x in "zxcvbnm,.>/?"],
]
2011-02-13 20:32:02 -05:00
kbdict = {}
2023-02-09 14:52:26 -05:00
for i, l in enumerate(kbloc):
for j, k in enumerate(l):
2011-02-13 20:32:02 -05:00
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",
}
2011-02-13 20:32:02 -05:00
def mispeller(word):
if len(word) <= 6:
num = 1
else:
num = random.choice([1, 2])
2021-03-23 17:36:43 -04:00
wordseq = list(range(0, len(word)))
2011-02-13 20:32:02 -05:00
random.shuffle(wordseq)
letters = wordseq[0:num]
2011-02-13 20:32:02 -05:00
def mistype(string, i):
l = string[i]
2021-03-23 17:36:43 -04:00
if l not in kbdict:
2011-02-13 20:32:02 -05:00
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 :]
2011-02-13 20:32:02 -05:00
return string
2011-02-13 20:32:02 -05:00
def transpose(string, i):
j = (i + random.choice([-1, 1])) % len(string)
2011-02-13 20:32:02 -05:00
l = [c for c in string]
l[i], l[j] = l[j], l[i]
return "".join(l)
2011-02-13 20:32:02 -05:00
def randomletter(string, i):
string = (
string[0 : i + 1]
+ random.choice("abcdefghijklmnopqrstuvwxyz")
+ string[i + 1 :]
)
2011-02-13 20:32:02 -05:00
return string
2011-02-13 20:32:02 -05:00
def randomreplace(string, i):
string = (
string[0:i] + random.choice("abcdefghijklmnopqrstuvwxyz") + string[i + 1 :]
)
2011-02-13 20:32:02 -05:00
return string
2011-02-13 20:32:02 -05:00
def soundalike(string, i):
try:
c = sounddict[string[i]]
except:
return string
string = string[0:i] + c + string[i + 1 :]
2011-02-13 20:32:02 -05:00
return string
func = random.choice([mistype, transpose, randomletter, randomreplace, soundalike])
2011-02-13 20:32:02 -05:00
for i in letters:
word = func(word, i)
return word