-
Notifications
You must be signed in to change notification settings - Fork 0
/
compression.py
268 lines (208 loc) · 9.66 KB
/
compression.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from bitstring import BitArray
import os
import json
#NodeTree class to create Huffman Tree.
class NodeTree(object):
def __init__(self, left=None, right=None):
self.left = left
self.right = right
def children(self):
return (self.left, self.right)
def nodes(self):
return (self.left, self.right)
# For Compressed Files
def open_decode(filename: str):
with open(filename, 'rb') as f:
b = BitArray(f.read())
binary = b.bin
#binary = binary.strip('0')
#print('b =', binary)
return binary
def write_to_file(dictionary: dict[chr,str], txt_lines: list[str], filename: str):
binary_string = ''
for line in txt_lines:
for character in line:
binary_string = binary_string+dictionary[character]
a = BitArray(bin=binary_string)
with open(filename, 'wb') as f:
a.tofile(f)
# For Text Files
def open_file(filename: str):
txt_lines = []
txt_lines = open(filename).readlines()
return txt_lines
def write_txt(filename: str, text: str):
with open(filename, 'w') as f:
f.write(text)
# Find the number of occurrences (frequency) for each character in the text.
# Generates a dictionary of how many times a charecter is used.
def get_character_freq(txt_lines: list[str])-> dict[chr,int]:
letter_freq = {}
for line in txt_lines:
for character in line:
if character in letter_freq:
letter_freq[character] += 1
else:
letter_freq[character] = 1
return letter_freq
# Creates the huffman tree
def huffman_code_tree(node, left: bool = True, binString: str =""):
if type(node) is str:
return {node: binString}
(l, r) = node.children()
d = dict()
d.update(huffman_code_tree(l, True, binString + '0'))
d.update(huffman_code_tree(r, False, binString + '1'))
return d
# Uses the Huffman Code dictionary to decode the encoded file.
def huffman_decode(dictionary: dict, text: str):
reversed_dict = {}
for value, key in dictionary.items():
reversed_dict[key] = value
start_index = 0
end_index = 1
max_index = len(text)-1
s = ''
#print(reversed_dict)
looper = True
while looper == True:
if text[start_index : end_index] in reversed_dict:
s += reversed_dict[text[start_index : end_index]]
start_index = end_index
end_index += 1
if start_index > max_index:
looper = False
elif end_index > max_index + 8:
looper = False
#print("start_index = " + str(start_index) + " end_index = " + str(end_index) + " max_index = " + str(max_index))
return s[:-1]
# write the huffman tree to file
def write_tree(filename: str, dictionary: dict):
with open(filename, 'w') as file:
file.write(json.dumps(dictionary))
# read the huffman tree from file
def read_tree(filename: str):
return json.load(open(filename))
# when option 1 is selected
def encode():
print("Please enter the name of your .txt file.")
print("For example, if you wanted to encode 'test.txt' you would enter: 'test'.")
print("")
#take user input
filename = input()
lines = []
lines = open_file(filename+".txt")
print("File '" + filename + ".txt' has been succesfully found.")
letter_freq = {}
letter_freq = get_character_freq(lines)
sorted_letter_freq = {}
sorted_letter_freq = sorted(letter_freq.items(), key=lambda x: x[1], reverse=True)
print("Letter frequency of text file '" + filename + ".txt' calculated.")
nodes = sorted_letter_freq
while len(nodes) > 1:
(key1, c1) = nodes[-1]
(key2, c2) = nodes[-2]
nodes = nodes[:-2]
node = NodeTree(key1, key2)
nodes.append((node, c1 + c2))
nodes = sorted(nodes, key=lambda x: x[1], reverse=True)
print("Huffman Tree has been created.")
huffman_code = huffman_code_tree(nodes[0][0])
print("Compressing file.")
write_to_file(huffman_code, lines, filename+".bin")
write_tree(filename+".hufftree", huffman_code)
return ("File '" + filename + ".txt' has been succesfully compressed to " + filename + ".bin. In addition, the huffman tree has been saved as " + filename + ".hufftree.")
# when option 2 is selected
def decode():
print("Please enter the name of your compressed .bin file.")
print("For example, if you wanted to uncompress 'test.bin' you would enter: 'test'.")
print("")
#take user input
filename = input()
hufffilename = filename
encoded = open_decode(filename+".bin")
print("If the name of the huffman tree you wish to use is not the same as the name of the file, please enter '1', any other input will use " + filename + ".hufftree.")
user_input = input()
if user_input == "1":
print("Please enter the name of the .hufftree file.")
hufffilename = input()
huffman_code = read_tree(hufffilename+".hufftree")
decoded = huffman_decode(huffman_code, encoded)
write_txt(filename+"_decoded.txt", decoded)
return ("File '" + filename + "_decoded.txt' has been succesfully decompressed from " + filename + ".bin using the huffman tree " + hufffilename + ".hufftree.")
def diff_hufftree():
print("THIS FEATURE IS FOR EXPERIMENTING WITH USING DIFFERENT HUFFMAN TREES WITH .txt FILES.")
print("THIS FEATURE MAY NOT WORK IF THE TREE DOESN'T CONTAIN THE SAME CHARACTERS.")
print("")
print("Please enter the name of your .txt file.")
print("For example, if you wanted to encode 'test.txt' you would enter: 'test'.")
print("")
#take user input
filename = input()
lines = []
lines = open_file(filename+".txt")
print("File '" + filename + ".txt' has been succesfully found.")
print("Please enter the name of the .hufftree file.")
hufffilename = input()
huffman_code = read_tree(hufffilename+".hufftree")
print(huffman_code)
write_to_file(huffman_code, lines, filename+"_"+hufffilename+".bin")
return ("File '" + filename + ".txt' has been succesfully compressed to " + filename + "_" + hufffilename + ".bin using the huffman tree " + hufffilename + ".hufftree.")
cmd = 'mode 157,40'
os.system(cmd)
clear = lambda: os.system('cls')
quitter = False
error_message = ""
while quitter == False:
print("""
_____ _ _ _____ _____ _
/ ____(_) ( ) / ____| / ____| | |
| (___ _ _ __ ___ ___ _ __ |/ ___ | (___ _ _ _ __ ___ _ __ | | ___ ___ | |
\___ \| | '_ ` _ \ / _ \| '_ \ / __| \___ \| | | | '_ \ / _ | '__| | | / _ \ / _ \| |
____) | | | | | | | (_) | | | | \__ \ ____) | |_| | |_) | __| | | |___| (_) | (_) | |
|_____/|_|_| |_| |_|\___/|_| |_| |___/ |_____/ \__,_| .__/ \___|_| \_____\___/ \___/|_|
_ _ __ __ _____| | _ _____
| | | | / _|/ _| / ____|_| (_) | __ \
| |__| |_ _| |_| |_ _ __ ___ __ _ _ __ | | ___ _ __ ___ _ __ _ __ ___ ___ ___ _ ___ _ __ | |__) _ __ ___ __ _ _ __ __ _ _ __ ___
| __ | | | | _| _| '_ ` _ \ / _` | '_ \ | | / _ \| '_ ` _ \| '_ \| '__/ _ / __/ __| |/ _ \| '_ \ | ___| '__/ _ \ / _` | '__/ _` | '_ ` _ \
| | | | |_| | | | | | | | | | | (_| | | | | | |___| (_) | | | | | | |_) | | | __\__ \__ | | (_) | | | | | | | | | (_) | (_| | | | (_| | | | | | |
|_| |_|\__,_|_| |_| |_| |_| |_|\__,_|_| |_| \_____\___/|_| |_| |_| .__/|_| \___|___|___|_|\___/|_| |_| |_| |_| \___/ \__, |_| \__,_|_| |_| |_|
| | __/ |
|_| |___/
""")
print(error_message)
print("")
print("What would you like to do?")
print("1. Encode a .txt file.")
print("2. Decode an encoded .bin file.")
print("3. Encode a .txt using a different .hufftree file.")
print("q. Quit.")
user_input = input()
if user_input == "1":
error_message = ""
clear()
try:
error_message = encode()
except Exception as e:
error_message = "ERROR: " + str(e)
elif user_input == "2":
error_message = ""
clear()
try:
error_message = decode()
except Exception as e:
error_message = "ERROR: " + str(e)
elif user_input == "3":
error_message = ""
clear()
try:
error_message = diff_hufftree()
except KeyError as e:
error_message = "ERROR: HUFFMAN TREE DOES NOT CONTAIN " + str(e) + "."
except Exception as e:
error_message = "ERROR: " + str(e)
elif user_input == "q":
quitter = True
else:
error_message = "Invalid input, please try again..."
clear()