-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmisspell.py
66 lines (61 loc) · 1.54 KB
/
misspell.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
import re, random
class Misspell:
#Give list of words to mispell
def __init__(self, wordList):
self.wList = wordList
def genWord(self):
return self.misspelled(self.wList[random.randint(0,len(self.wList)-1)])
# Mispelling a word based on these types of incorrections while typing
# -deletion (deletes)
# -swapping adajent letters (transposes)
# -alteration (replaces)
# -inserting a letter (inserts)
#Returns a word mispelled
def misspelled(self, word):
if len(word) == 1:
return word
vowels = 'aeiouy'
consonants = 'bcdfghjklmnpqrstvwxyz'
if len(word) < 9:
mistakes = 1
elif len(word) < 12:
mistakes = 2
elif len(word) < 17:
mistakes = 3
else:
mistakes = 4
newWord = word[0]
prev = word[0]
for i in word[1:]:
if mistakes != 0:
rNum = random.randint(1,10)
else:
rNum = 5
# if rNum == 1:
#Do nothing...'Deletion'
if rNum == 2:
#Swapping adjacent letters
newWord = newWord[:len(newWord)-2] + i + prev
elif rNum == 3:
#Alteration
if i in vowels:
c = vowels[random.randint(0, len(vowels)-1)]
while i == c:
c = vowels[random.randint(0, len(vowels)-1)]
else:
c = i
newWord += c
elif rNum == 4:
newWord += i + i
else:
newWord += i
prev = i
mistakes -= 1
if newWord == word:
newWord += prev
return newWord
# def words(text):
# return re.findall('[a-z]+', text.lower())
# lWords = words(file('/usr/share/dict/words').read())
# miss = Misspelled(lWords)
# print miss.genWord()