-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffman.py
More file actions
198 lines (156 loc) · 5.88 KB
/
Huffman.py
File metadata and controls
198 lines (156 loc) · 5.88 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
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
import pickle
import struct
# Node tree for the Huffman algorithm
class Nodes:
def __init__(self, probability, symbol, left=None, right=None):
# probability of the symbol
self.probability = probability
# the symbol
self.symbol = symbol
# the left node
self.left = left
# the right node
self.right = right
# the tree direction (0 or 1)
self.code = ''
# A supporting function in order to calculate the probabilities of symbols
# in specified data
def CalculateProbability(the_data):
the_symbols = dict()
for item in the_data:
if the_symbols.get(item) is None:
the_symbols[item] = 1
else:
the_symbols[item] += 1
return the_symbols
# A supporting function in order to print the codes of symbols by
# travelling a Huffman Tree
the_codes = dict()
def CalculateCodes(node, value=''):
# a huffman code for current node
newValue = value + str(node.code)
if (node.left):
CalculateCodes(node.left, newValue)
if (node.right):
CalculateCodes(node.right, newValue)
if (not node.left and not node.right):
the_codes[node.symbol] = newValue
return the_codes
# A supporting function in order to get the encoded result
def OutputEncoded(the_data, coding):
encodingOutput = []
for element in the_data:
encodingOutput.append(coding[element])
the_string = ''.join([str(item) for item in encodingOutput])
return the_string
# A supporting function in order to calculate the space difference between
# compressed and non-compressed data
def TotalGain(the_data, coding):
# total bit space to store the data before compression
beforeCompression = len(the_data) * 8
afterCompression = 0
the_symbols = coding.keys()
for symbol in the_symbols:
the_count = the_data.count(symbol)
# calculating how many bit is required for that symbol in total
afterCompression += the_count * len(coding[symbol])
# Function for encoding data using the Huffman algorithm that returns a
# node tree and the encoded output
def HuffmanEncoding(the_data):
symbolWithProbs = CalculateProbability(the_data)
the_symbols = symbolWithProbs.keys()
the_probabilities = symbolWithProbs.values()
the_nodes = []
# converting symbols and probabilities into huffman tree nodes
for symbol in the_symbols:
the_nodes.append(Nodes(symbolWithProbs.get(symbol), symbol))
while len(the_nodes) > 1:
# sorting all the nodes in ascending order based on their probability
the_nodes = sorted(the_nodes, key=lambda x: x.probability)
# for node in nodes:
# print(node.symbol, node.prob)
# picking two smallest nodes
right = the_nodes[0]
left = the_nodes[1]
left.code = 0
right.code = 1
# combining the 2 smallest nodes to create new node
newNode = Nodes(
left.probability +
right.probability,
left.symbol +
right.symbol,
left,
right)
the_nodes.remove(left)
the_nodes.remove(right)
the_nodes.append(newNode)
huffmanEncoding = CalculateCodes(the_nodes[0])
TotalGain(the_data, huffmanEncoding)
encodedOutput = OutputEncoded(the_data, huffmanEncoding)
return encodedOutput, the_nodes[0]
# Function for decoding encoded data using the Huffman node tree
def HuffmanDecoding(encodedData, huffmanTree):
treeHead = huffmanTree
decodedOutput = []
for x in encodedData:
if x == '1':
huffmanTree = huffmanTree.right
elif x == '0':
huffmanTree = huffmanTree.left
try:
if huffmanTree.left.symbol is None and huffmanTree.right.symbol is None:
pass
except AttributeError:
decodedOutput.append(huffmanTree.symbol)
huffmanTree = treeHead
bytess = bytes()
for i in decodedOutput:
bytess += bytes([i])
return bytess
# Class for Huffman file archivation
class Huffman():
# Function for reading data from a Huffman archive
def read_data(self, file):
with open(file, "rb") as f:
f.read(7)
a = struct.unpack("<l", f.read(4))[0]
length = struct.unpack("<l", f.read(4))[0]
binary_data_read = f.read(length)
binary_data_read = ''.join(format(byte, '08b')
for byte in binary_data_read)
if a != 0:
binary_data_read = binary_data_read[0:-
8] + binary_data_read[-1 * a::]
else:
binary_data_read = binary_data_read
while True:
try:
chunk = pickle.load(f)
# Check if the chunk is a dictionary
if isinstance(chunk, Nodes):
# Use the dictionary found
my_tree = chunk
break
except EOFError:
# End of file reached, no dictionary found
break
huff = my_tree
return binary_data_read, huff
# Function for archiving files using the Huffman algorithm
def archive(self, data):
encoding, the_tree = HuffmanEncoding(data)
binary_string = encoding
binary_data = bytes([int(binary_string[i:i + 8], 2)
for i in range(0, len(binary_string), 8)])
a = len(binary_string) % 8
data = b''
data += struct.pack("<l", a)
data += struct.pack("<l", len(binary_data))
data += binary_data
data += pickle.dumps(the_tree)
return data
# Function for unarchiving Huffman archives
def unarchive(self, archive_name):
data = self.read_data(archive_name)
return HuffmanDecoding(data[0], data[1])