-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman.py
148 lines (100 loc) · 2.85 KB
/
Huffman.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
from queue import PriorityQueue
class Node:
def __init__(self,freq,ch):
self.left = None
self.right = None
self.freq = freq
self.ch = ch
def __gt__(self, other):
return (self.freq> other.freq )
def getNode(ch,freq,left,right):
node=Node(freq,ch)
node.left = left
node.right = right
return node
def encode(root,string,huffmanCode):
if (root==None):
return
if (not root.left and not root.right) :
huffmanCode[root.ch] = string
encode(root.left, string + "0", huffmanCode)
encode(root.right, string + "1", huffmanCode)
def decode(root,rr,index,text,r,c):
if root==None or index>len(text)-1:
return
if (root.left==None and root.right==None):
r=r+root.ch
index=index+1
#print("text= ",text[index:])
if (text[index:]==""):
output = open("decoded.txt","w+")
output.write("".join(r))
print("Decoded file generated as decoded.txt")
return decode(rr,rr,0,text[index:],r,0)
if (index==0 and c==0):
c=c+1
elif(index==0 and c==1):
index=index+1
else:
index=index+1
if (text[index] =='0'):
decode(root.left,rr, index, text,r,c)
else:
decode(root.right,rr, index, text,r,c)
def buildHuffmanTreeEncode(text):
dictionary = {}
for i in text:
if i in dictionary:
dictionary[i]=dictionary[i]+1
else:
dictionary[i]=1
pq= PriorityQueue()
for i in dictionary:
pq.put(getNode(i,dictionary[i],None,None))
while (pq.qsize()!=1):
left=pq.get()
right=pq.get()
sum=left.freq+right.freq
pq.put(getNode('' , sum, left, right))
root = pq.get()
huffmanCode={}
encode(root, "", huffmanCode)
string=""
for c in text:
string=string+str(huffmanCode[c])
output = open("encoded.txt","w+")
output.write("".join(string))
print("Encoded file generated as encoded.txt")
def buildHuffmanTreeDecode(text,chars,freqq):
pq=PriorityQueue()
for i in range(0,len(chars)):
pq.put(getNode(chars[i],freqq[i],None,None))
while (pq.qsize()!=1):
left=pq.get()
right=pq.get()
sum=left.freq+right.freq
pq.put(getNode('' , sum, left, right))
root = pq.get()
rr=root
index=int(0)
r=""
c=0
decode(root,rr,index,text,r,c)
with open("encode.txt", 'r') as f:
stringToEncode = f.read()
buildHuffmanTreeEncode(stringToEncode)
chars = []
freq = []
file= open("decode.txt", 'r')
data = file.read()
lines=data.splitlines()
stringToDecode=lines[0]
newList = lines[1].split(">")
for word in newList:
if word == '':
break
newword = word[1:].split(":")
chars.append(newword[0])
freq.append(int(newword[1]))
#print("jbhjkwebnjkewnfjkenfdjkwenfejkfnekj",stringToDecode)
buildHuffmanTreeDecode(stringToDecode,chars,freq)