forked from Aacash-Srinath/Text-Compression-Techniques
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffmanWorks.py
156 lines (124 loc) · 3.86 KB
/
huffmanWorks.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
import heapq
from collections import defaultdict
import os
# to map each character its huffman value
codes = {}
# To store the frequency of character of the input data
freq = defaultdict(int)
# A Huffman tree node
class MinHeapNode:
def __init__(self, data, freq):
self.left = None
self.right = None
self.data = data
self.freq = freq
def __lt__(self, other):
return self.freq < other.freq
# utility function to print characters along with
# their huffman value
def printCodes(root, str):
if root is None:
return
if root.data != '$':
print(root.data, ":", str)
printCodes(root.left, str + "0")
printCodes(root.right, str + "1")
# utility function to store characters along with
# their huffman value in a hash table
def storeCodes(root, str):
if root is None:
return
if root.data != '$':
codes[root.data] = str
storeCodes(root.left, str + "0")
storeCodes(root.right, str + "1")
# function to build the Huffman tree and store it
# in minHeap
def HuffmanCodes():
global minHeap
for key in freq:
minHeap.append(MinHeapNode(key, freq[key]))
heapq.heapify(minHeap)
while len(minHeap) != 1:
left = heapq.heappop(minHeap)
right = heapq.heappop(minHeap)
top = MinHeapNode('$', left.freq + right.freq)
top.left = left
top.right = right
heapq.heappush(minHeap, top)
storeCodes(minHeap[0], "")
# utility function to store map each character with its
# frequency in input string
def calcFreq(str):
for i in str:
freq[i] += 1
# function iterates through the encoded string s
# if s[i]=='1' then move to node->right
# if s[i]=='0' then move to node->left
# if leaf node append the node->data to our output string
def decode_file(root, s):
ans = ""
curr = root
n = len(s)
for i in range(n):
if s[i] == '0':
curr = curr.left
else:
curr = curr.right
# reached leaf node
if curr.left is None and curr.right is None:
ans += curr.data
curr = root
return ans + '\0'
# def compress_file(input_file_path, output_file_path):
# with open(input_file_path, "r") as input_file:
# text = input_file.read()
# codes = compress(text)
# with open(output_file_path, "w") as output_file:
# for code in codes:
# output_file.write(str(code) + " ")
def write_file(encodedString):
codeCopy = int(encodedString, 2)
hexCode = []
while codeCopy:
hexCode.insert(0, codeCopy % 256)
codeCopy //= 256
k2 = bytes(hexCode)
with open("output.txt", "wb") as key:
key.write(k2)
def read_file():
with open("output.txt", "rb") as key:
toDecode = key.read()
huffCode = "".join([f"{int(bin(i).replace('0b', '')):08d}" for i in list(toDecode)])
i = 0
while True:
i += 1
if huffCode[i - 1] == '1':
break
return huffCode[i:]
# Driver code
if __name__ == "__main__":
minHeap = []
inputFile = "SampleTextFiles/words200/file1.txt"
with open(inputFile, 'r') as file:
str = file.read()
encodedString, decodedString = "", ""
calcFreq(str)
HuffmanCodes()
# print("Character With their Frequencies:")
# for key in sorted(codes):
# print(key, codes[key])
for i in str:
encodedString += codes[i]
# print("\nEncoded Huffman data:")
# print(encodedString)
write_file('1' + encodedString)
input_file_size = os.path.getsize(inputFile)
print("Input file size is: ", input_file_size, "bytes")
output_file_size = os.path.getsize('output.txt')
print("Output file size is: ", output_file_size, "bytes")
# Function call
huffCode = read_file()
decodedString = decode_file(minHeap[0], huffCode)
print("\nDecoded Huffman Data:")
print(decodedString)