This repository has been archived by the owner on Apr 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
308 lines (277 loc) · 6.86 KB
/
index.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Making Frequenc Dictionary From String
def make_frequency_dict(rawstr):
frequency = {}
for character in rawstr:
if not character in frequency:
frequency[character] = 1
else:
frequency[character] += 1
return frequency
# Making String From File
def stringgen(file):
string=''
while True:
c=file.read(1)
if c:
string=string+c
else:
break
return string
# It is very obvious
def GetNodes(G):
Nodes=[]
for i in G.keys():
Nodes.append(i)
return Nodes
# Getting All the Nodes that have no parents
def heapTops(G):
Nodes=GetNodes(G)
tops=[]
for i in Nodes:
check=False
for j in G.values():
if i in j:
check=True
break
if not check:
tops.append(i)
return tops
def Truecount(sub,G):
if sub in G:
return G[sub]
else:
return sub
# Selection Sort to select Two minimum Elements
def min2(lis,G):
for i in range(min(2,len(lis))):
mini=i
for j in range(i+1,len(lis)):
if Truecount(lis[j],G)<Truecount(lis[mini],G):
mini=j
lis[mini],lis[i]=lis[i],lis[mini]
return lis
def Grapher(G):
BigG={}
lis=GetNodes(G)
for i in lis:
BigG[i]=[]
biglis=heapTops(BigG)
count=0
while len(biglis)>1:
min2(biglis,G)
val1,val2=biglis[0],biglis[1]
nodename='alp'+str(count)
count+=1
sumi=Truecount(val1,G)+Truecount(val2,G)
G[nodename]=sumi
BigG[nodename]=[val1,val2]
biglis.pop(0)
biglis.pop(0)
biglis=heapTops(BigG)
return BigG
def mothernode(G):
lis=heapTops(G)
return lis[0]
def assigncodes(G):
leaf=[]
codes={}
for i in G:
if not G[i]:
leaf.append(i)
for i in leaf:
codes[i]=''
path=[mothernode(G)]
visited=[mothernode(G)]
string=''
while path:
top=path[-1]
neigh=G[top]
check=False
for i in range(len(neigh)):
if neigh[i] not in visited:
path.append(neigh[i])
visited.append(neigh[i])
check=True
if i==0:
string=string+'0'
elif i==1:
string=string+'1'
break
if not check:
ntop=path.pop()
if not G[ntop]:
codes[ntop]=string
string=string[:-1]
return codes
def replacer(fil,codes):
string=''
for i in fil:
string=string+codes[i]
return string
def completer(string):
leng=len(string)
rem=leng%6
if rem==0:
return string,0
left=6-rem
string="0"*left+string
return string,left
def encoder(string):
s=''
tempstr=''
for i in string:
tempstr=tempstr+i
if len(tempstr)==6:
charcode=int('1'+tempstr,2)
if charcode==13:
print('hit')
char=chr(charcode)
s = s+char
tempstr=''
return s
def masterencoder(filename):
rawstr=stringgen(filename)
charfreq=make_frequency_dict(rawstr)
#print("char frequencies",charfreq)
graph=Grapher(charfreq)
#print("complete graph",graph)
codes=assigncodes(graph)
depth = maxleveler(codes)
while depth%6 != 0:
depth += 1
codelen=depth//6
protocol = ""
for i in codes:
j = codes[i]
length = str(len(j))
if len(length)==1:
length = "0" + length
while len(j) != depth:
j = "0"+j
j = encoder(j)
protocol += (i+j+length)
#print(protocol)
protocol += "}"
protocol += "}"
protocol += "}"
# print("assigned codes",codes,"\n")
encoded=replacer(rawstr,codes)
encoded,left=completer(encoded)
protocol += str(left)
protocol += str(codelen)
finalcode=encoder(encoded)
write_file('compressed.txt',finalcode,protocol)
def read_graph(filename):
string = ""
stop = ""
while True:
j=filename.read(1)
if j == "}":
stop += j
if j != "}" and len(stop) > 0:
string += stop
stop = ""
if len(stop) == 0:
string += j
if stop == "}}}":
break
extra=filename.read(1)
codelen=filename.read(1)
return decrypt_graph(string,int(codelen)),extra
def decrypt_graph(string,codelen):
graph = {}
for i in range(0,len(string),codelen+3):
graph[string[i]] = string[i+1:i+codelen+3]
return graph
def extractgraphcodes(rawgraph):
graph={}
for i in rawgraph:
rawcode=rawgraph[i]
completedcode=''
for j in range(len(rawcode)-2):
temp = ord(rawcode[j])
bini = bin(temp)[3:]
leng = len(bini)
if leng != 6:
left=6-leng
bini = '0'*left+bini
completedcode += bini
lengthcode=int(rawcode[-2:])
actualcode=completedcode[-lengthcode:]
graph[i]=actualcode
return graph
def inversebijection(diction):
c={}
for i in diction:
c[diction[i]]=i
return c
def stringtobinary(string):
s=''
for i in string:
value=ord(i)
bini=bin(value)[3:]
s+= bini
return s
def reversecode(strign,codes):
s=''
tempstr=''
for i in strign:
tempstr += i
if tempstr in codes:
s = s + codes[tempstr]
tempstr=''
return s
def masterdecoder(file):
rawgraph,extra=read_graph(filename)
graphcodes=extractgraphcodes(rawgraph)
reversecodes=inversebijection(graphcodes)
restofstring=stringgen(file)
rawbinary=stringtobinary(restofstring)
binarytrim=rawbinary[int(extra):]
finalstr=reversecode(binarytrim,reversecodes)
write_file("decompressed.txt",finalstr,'')
def write_file(name,text,protocol):
file1 = open(name,"w+")
for i in protocol:
file1.write(i)
for i in text:
file1.write(i)
file1.close()
def maxleveler(codes):
global maxlevel
maxl=0
for i in codes.values():
if len(i)>maxl:
maxl=len(i)
maxlevel=maxl
return maxl
bini=open('sample.txt')
sumi=0
while True:
c=bini.read(1)
if not c:
break
sumi=sumi+1
print('characters in original file', sumi)
fin=open('sample.txt')
masterencoder(fin)
print('compressed file created as compressed.txt')
fin=open('compressed.txt')
sum2=0
while True:
c=fin.read(1)
if not c:
break
sum2=sum2+1
print('characters in compressed file',sum2)
filename = open("compressed.txt",'r')
masterdecoder(filename)
print('decompressed file created as decompressed.txt')
fin=open('decompressed.txt')
sum3=0
while True:
c=fin.read(1)
if not c:
break
sum3=sum3+1
print('characters in decompressed file',sum3)