-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauto_incorrect.py
More file actions
104 lines (90 loc) · 3.03 KB
/
Copy pathauto_incorrect.py
File metadata and controls
104 lines (90 loc) · 3.03 KB
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
# Author: lazho
# This program will take a string or a file's contents as input and make spelling errors. It is by no way polished and is prone to errors due to lack of exception handling. I made this for fun, don't do a whole code review on it.
import sys, random
def auto_incorrect(args):
# usage:
# argument 1: -s flag means input is string, -f means filename
# 2: string, could be filename if specified -f
# 3: (optional) non-negative int to specify how many times
# in 100 that a word should have a spelling mistake added
usage = "\
Usage: \
python auto-incorrect.py [-s|-f] [string|filename] [percentchance]"
chance = 20
# If ID-10-T user, tell them check usage
if len(args) < 3:
print(usage)
sys.exit(-1)
# If specified, set chance of mistakes
if len(args) == 4:
chance = int(args[3])
# Do we open a file?
openFile = False
if args[1] == "-f":
openFile = True
elif args[1] != "-s":
print(usage)
sys.exit(-1)
inputStr = args[2]
if openFile:
inputStr = open(inputStr, "r").read()
inputStr = inputStr.split()
# Probably shouldn't hardcode this in and instead read from a file,
# but oh well.
specialCases = {
"your": "you're",
"you're": "your",
"its": "it's",
"it's": "its",
"their": "they're",
"they're": "there",
"there": "their",
"lose": "loose",
"loose": "lose",
"effect": "affect",
"affect": "effect",
"definitely": "definately",
"weather": "whether",
"whether": "weather",
"then": "than",
"an": "a",
"the": "teh",
"like": "liek"
}
outputStr = ""
# Given string s and index i, it swaps s[i] and s[i+1] and returns
def swap(s, i):
si = s[i]
sj = s[i+1]
s = s[:i] + sj + si + s[i+2:]
return s
# Given string s and index i, it omits the i-th character.
def omit(s, i):
return s[:i]+s[i+1:]
for w in inputStr:
# Idiots don't use shift or capslock
w = w.lower()
# If jackpot
if random.randrange(100) < chance:
# Special cases get priority
if w in specialCases:
outputStr = outputStr + specialCases[w]
# Words less than 3 letters can't stand omit or swap
elif len(w) >= 3:
# Decide a random index
i = random.randrange(len(w)-1)
# 50/50 chance between omission and swap
if random.randrange(2) == 1:
outputStr = outputStr + swap(w, i)
else:
outputStr = outputStr + omit(w, i)
# Leave short words be
else:
outputStr = outputStr + w
else:
outputStr = outputStr + w
outputStr = outputStr + " "
return outputStr
if __name__ == "__main__":
output = auto_incorrect(sys.argv)
print(output)