/
main.py
102 lines (79 loc) · 3.26 KB
/
main.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
99
100
101
102
__author__ = 'Esther'
import random
def open_file(path):
infile = open(path, "r", encoding="utf8")
return infile.read()
def write_file(path, content):
outpath = switch_letters(path.split('.',1)[0]) + '.' + path.split('.',1)[1]
print(content, file=open(outpath, "w", encoding="utf8"))
def switch_letters(text):
# swap two random adjacent letters
if len(text) >1:
first = random.randint(0,len(text)-2) # first of switched letters may not be last letter of text
second = first + 1
out_text = text[0:first] + text[second] + text[first] + text[second+1:]
return out_text
def miss_shift(text):
out_text = ''
for letter in text:
if letter.isupper():
choice = random.randint(1,6) # 1 in 5 upper-case letters will be turned to lower-case
if choice == 1:
out_text += letter.lower()
else:
out_text += letter
else:
out_text += letter
return out_text
def get_nearby_char(char):
# german keyboard layout
layout=['^', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'ß', '`'], \
['q', 'w', 'e', 'r', 't', 'z', 'u', 'i', 'o', 'p', 'ü', '*'], \
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'ö', 'ä', '#'], \
['<', 'y', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '-'], \
[ '', '', '', ' ', ' ', ' ', ' ', ' ', '', '', '']
if ((not char) or (not any(char in row for row in layout))): # no char or not found in layout
return char
for ix, row in enumerate(layout): # get index of char we want to manipulate
for iy, i in enumerate(row):
if i == char: # introduce the "typo", shift the index randomly by +-1
if i == ' ': # spacebar ...
iy += random.randint(-1, +5) # ... is a bit bigger
else:
iy += random.randint(-1, +1)
ix += random.randint(-1, +1)
ix = max(min(len(layout)-1, ix), 0) # prevent out of bounds
iy = max(min(len(layout[ix])-1, iy), 0)
return (layout[ix][iy])
def wrong_key(text):
out_text = ''
for letter in text:
choice = random.randint(1,61)
if choice <= 2: # 2 in 60 characters will be replaced with an adjacent letter (only lower-case)
out_text += get_nearby_char(letter)
elif choice == 4: # 1 in 60 characters will be followed by an adjacent letter
out_text += letter + get_nearby_char(letter)
else:
out_text += letter
return out_text
def wrong_spelling(text):
out_text = text
common_mistakes = {' seid ':' seit ',
' seit ':' seid ',
'wider':' wieder',
'wieder':'wider',
', das':', dass',
', dass':', das',
'standard':'standart',
'Standard':'Standart'}
for written_word in common_mistakes.keys():
out_text = out_text.replace(written_word, common_mistakes[written_word])
return out_text
def obfucsate(text):
# insert typos of all types
text = wrong_spelling(text)
text = miss_shift(text)
text = wrong_key(text)
return text
test = "Kafka - Die Verwandlung.txt"
write_file(test, obfucsate(open_file(test)))