-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonoSub.py
More file actions
161 lines (152 loc) · 5.65 KB
/
MonoSub.py
File metadata and controls
161 lines (152 loc) · 5.65 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import argparse
import pickle
atoz = "abcdefghijklmnopqrstuvwxyz".upper()
dictionary = {}
for char in atoz: dictionary[char] = '.'
parser = argparse.ArgumentParser()
parser.add_argument("inputfile")
args = parser.parse_args()
codefile = open(args.inputfile, "r")
codelines = [line.strip().upper() for line in codefile.readlines()]
def substitute(text):
res = ""
for char in text:
if char in dictionary.keys(): res += dictionary[char]
else: res += char
return res
def print_message():
os.system("clear")
for line in codelines:
print()
print(" " + line)
print(" " + substitute(line))
print()
autofreq = True
commandlist = "add remove clear reset frequency autofreq summary save load quit exit"
while True:
print_message()
if autofreq:
letter_count = {}
for key in dictionary.keys(): letter_count[key] = 0
for line in codelines:
for char in line:
if char in letter_count.keys(): letter_count[char] += 1
letter_count_tuple = [(k, v) for k, v in letter_count.items()]
letter_count_tuple.sort(key = lambda x: -x[1])
count = 0
for k, v in letter_count_tuple:
print("{}: {}".format(k, v), end = '\t')
count += 1
if count == 6:
print()
count = 0
print("\n")
print("Commands: " + commandlist + '\n')
command = ""
while command == "":
try:
command = input("> ")
if command in ["quit", "exit"]:
os.system("clear")
exit()
elif command == "":
continue
elif command.split(' ')[0] == "add":
if len(command.split(' ')) != 3:
print("Usage: add <source-word> <dest-word>")
command = ""
continue
source_word = command.split(' ')[1]
dest_word = command.split(' ')[2]
if len(source_word) != len(dest_word):
print("add: Length mismatch.")
command = ""
continue
for s, d in zip(source_word.upper(), dest_word.upper()):
if s in dictionary.keys(): dictionary[s] = d
elif command.split(' ')[0] == "remove":
if len(command.split(' ')) != 2:
print("Usage: remove <char>")
command = ""
continue
elif len(command.split(' ')[1]) != 1:
print("remove: Remove one character at a time!")
command = ""
continue
dictionary[command.split(' ')[1].upper()] = '.'
elif command == "clear":
continue
elif command == "reset":
for key in dictionary.keys():
dictionary[key] = '.'
elif command.split(' ')[0] == "autofreq":
if len(command.split(' ')) != 2:
print("Usage: autofreq (on/off)")
command = ""
continue
if command.split(' ')[1] == "on":
print("Auto frequency printing is ON.")
autofreq = True
command = ""
continue
elif command.split(' ')[1] == "off":
print("Auto frequency printing is OFF.")
autofreq = False
command = ""
continue
elif command == "frequency":
letter_count = {}
for key in dictionary.keys(): letter_count[key] = 0
for line in codelines:
for char in line:
if char in letter_count.keys(): letter_count[char] += 1
letter_count_tuple = [(k, v) for k, v in letter_count.items()]
letter_count_tuple.sort(key = lambda x: -x[1])
print()
count = 0
for k, v in letter_count_tuple:
print("{}: {}".format(k, v), end = '\t')
count += 1
if count == 6:
print()
count = 0
command = ""
print("\n")
continue
elif command == "summary":
print()
count = 0
for key in dictionary.keys():
print("{}: {}".format(key, dictionary[key]), end = '\t')
count += 1
if count == 6:
print()
count = 0
print("\n")
command = ""
continue
elif command == "save":
savefile = open(args.inputfile + '.p', 'wb')
pickle.dump(dictionary, savefile)
savefile.close()
print("Saved to {}.p".format(args.inputfile))
command = ""
elif command == "load":
try:
savefile = open(args.inputfile + '.p', 'rb')
dictionary = pickle.load(savefile)
savefile.close()
except Exception as e:
print("Error: " + str(e))
command = ""
else:
print("Command unknown.")
print("Commands: \n" + commandlist)
command = ""
continue
except KeyboardInterrupt:
print()
print("Type quit or exit to exit.")
command = ""
continue