-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
367 lines (337 loc) · 16.4 KB
/
index.html
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LZ77-LZ78</title>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<py-script>
# This function is used in the Lz78 algorithm.
def longest_common_substring(s1, s2):
# go along the first string and search for the longest match
maxLongest = 0
offset = 0
for i in range(0, len(s1)):
longest = 0
if ((i == len(s1) - len(s2) - 2)):
break
for j in range(0, len(s2)):
if (i+j < len(s1)):
if s1[i+j] == s2[j]:
longest = longest + 1
if (maxLongest < longest):
maxLongest = longest
offset = i
else:
break
else:
break
return maxLongest, offset
def compress_lz77(text, ws):
'''
Parameters:
text
ws: window size
Returns:
ouput: tuple list the compression
'''
ws = int(ws)
matches = 0
output = []
for i in range(0, len(text)):
# Skip many iterations as matches
if matches > 0:
matches = matches - 1
continue
# Search window
if len(text[:i]) > ws:
window = text[i - ws:i]
else:
window = text[:i]
# Largest prefix
prefix = text[i]
while True:
if prefix in window:
matches = matches + 1
aux = prefix
# check next prefix
prefix = text[i:i + matches + 1]
# special case last char
if aux == prefix:
prefix = prefix + '_'
break
else:
break
# Output
if matches == 0:
offset = 0
length = 0
symbol = prefix
else:
length = len(prefix) - 1 # prefix is the last checked that didn't match
symbol = prefix[length]
prefix = prefix[:-1] # correct prefix
offset = i - window.rfind(prefix) - (i - len(window))
output.append((offset, length, symbol))
return output
def decompress_lz77(tuples):
'''
Parameters:
tuples: compress output
Returns:
decoded: original text
'''
decoded = ""
for i in range(0, len(tuples)):
if tuples[i][0] == 0:
# Add new patterns
decoded += tuples[i][2]
else:
offset = tuples[i][0]
length = tuples[i][1]
symbol = tuples[i][2]
j = len(decoded)
# Add repeated patterns
rep = decoded[j - offset:j - offset + length]
decoded += rep
# Final text special case
if symbol != "_":
decoded += symbol
return decoded
def compress_lz78(text):
dictionary = dict()
i = 0
index = 1
encodedNumbers = []
encodedLetters = []
while i < len(text):
stringToBeSaved = text[i]
indexInDictionary = 0
while stringToBeSaved in dictionary:
indexInDictionary = dictionary[stringToBeSaved]
if (i == len(text) - 1):
stringToBeSaved = " "
break
i = i + 1
stringToBeSaved = stringToBeSaved + text[i]
#print ("<{0}, {1}>".format(indexInDictionary, stringToBeSaved[len(stringToBeSaved) - 1]))
encodedNumbers.append(indexInDictionary)
encodedLetters.append(stringToBeSaved[len(stringToBeSaved) - 1])
if (stringToBeSaved not in dictionary):
dictionary[stringToBeSaved] = index
index = index + 1
i = i + 1
return encodedNumbers, encodedLetters, dictionary
def decompress_lz78(encodedNumbers, encodedLetters, dictionary):
decoded = ""
i = 0
while i < len(encodedNumbers):
if (encodedNumbers[i] != 0):
decoded += str(list(dictionary.keys())[list(dictionary.values()).index(encodedNumbers[i])])
decoded+=(encodedLetters[i])
i = i+1
return decoded
# Initialize global variable for lz77 algorithm
index_compress_lz77 = 0
index_decompress_lz77 = 0
data_original_lz77 = "tipp tap tipp tap tippe tippe tipp tap"
data_compressed_lz77 = compress_lz77(data_original_lz77,len(data_original_lz77))
data_decompressed_lz77 = decompress_lz77(data_compressed_lz77)
# Initialize global variable for lz78 algorithm
index_compress_lz78 = 0
index_decompress_lz78 = 0
data_original_lz78 = "tipp tap tipp tap tippe tippe tipp tap"
data_compressed_lz78=[]
[encodedNumbers, encodedLetters, dictionary]=compress_lz78(data_original_lz78)
for i in range(len(encodedNumbers)):
data_compressed_lz78.append([encodedNumbers[i], encodedLetters[i]])
data_decompressed_lz78 = decompress_lz78(encodedNumbers, encodedLetters, dictionary)
# Functions for the Lz77 compress
def get_compress_input_lz77(*args, **kwargs):
global index_compress_lz77, data_original_lz77, data_compressed_lz77
data_original_lz77 = Element("data_original_input_lz77").element.value
data_compressed_lz77 = compress_lz77(data_original_lz77,len(data_original_lz77))
index_compress_lz77 = 0
pyscript.write("data_compressed_lz77", data_compressed_lz77[:index_compress_lz77])
def button_compress_previous_lz77(condition):
global index_compress_lz77
index_compress_lz77 -= 1
index_compress_lz77 = index_compress_lz77 % len(data_compressed_lz77)
pyscript.write("data_compressed_lz77", data_compressed_lz77[:index_compress_lz77+1])
def button_compress_next_lz77(condition):
global index_compress_lz77
index_compress_lz77 += 1
index_compress_lz77 = index_compress_lz77 % len(data_compressed_lz77)
pyscript.write("data_compressed_lz77", data_compressed_lz77[:index_compress_lz77+1])
# Functions for the Lz78 compress
def get_compress_input_lz78(*args, **kwargs):
global index_compress_lz78, data_original_lz78, data_compressed_lz78,encodedNumbers,encodedLetters, dictionary
data_original_lz78 = Element("data_original_input_lz78").element.value
data_compressed_lz78=[]
[encodedNumbers, encodedLetters, dictionary]=compress_lz78(data_original_lz78)
for i in range(len(encodedNumbers)):
data_compressed_lz78.append([encodedNumbers[i], encodedLetters[i]])
index_compress_lz78 = 0
pyscript.write("data_compressed_lz78", data_compressed_lz78[:index_compress_lz78])
def button_compress_previous_lz78(condition):
global index_compress_lz78,encodedNumbers
index_compress_lz78 -= 1
index_compress_lz78 = index_compress_lz78 % len(encodedNumbers)
pyscript.write("data_compressed_lz78", data_compressed_lz78[:index_compress_lz78+1])
def button_compress_next_lz78(condition):
global index_compress_lz78
index_compress_lz78 += 1
index_compress_lz78 = index_compress_lz78 % len(data_compressed_lz78)
pyscript.write("data_compressed_lz78", data_compressed_lz78[:index_compress_lz78+1])
# Functions for the Lz77 decompress
def get_decompress_input_lz77(*args, **kwargs):
global index_decompress_lz77, data_compressed_lz77, data_decompressed_lz77
data_compressed_lz77 = eval(Element("data_compressed_input_lz77").element.value)
index_decompress_lz77 = -1
data_decompressed_lz77 = " "
pyscript.write("data_decompressed_lz77", " ")
def button_decompress_previous_lz77(condition):
global index_decompress_lz77, data_decompressed_lz77
index_decompress_lz77 -= 1
index_decompress_lz77 = index_decompress_lz77 % len(data_compressed_lz77)
data_decompressed_lz77 = decompress_lz77(data_compressed_lz77[:index_decompress_lz77 + 1])
pyscript.write("data_decompressed_lz77", data_decompressed_lz77)
def button_decompress_next_lz77(condition):
global index_decompress_lz77, data_decompressed_lz77
index_decompress_lz77 += 1
index_decompress_lz77 = index_decompress_lz77 % len(data_compressed_lz77)
data_decompressed_lz77 = decompress_lz77(data_compressed_lz77[:index_decompress_lz77 + 1])
pyscript.write("data_decompressed_lz77", data_decompressed_lz77)
# Functions for the Lz78 decompress
def get_decompress_input_lz78(*args, **kwargs):
global index_decompress_lz78, data_compressed_lz78, data_decompressed_lz78
data_compressed_lz78 = eval(Element("data_compressed_input_lz78").element.value)
index_decompress_lz78 = 0
data_decompressed_lz78 = " "
pyscript.write("data_decompressed_lz78", " ")
def button_decompress_previous_lz78(condition):
global index_decompress_lz78, data_decompressed_lz78,encodedNumbers, encodedLetters, dictionary
index_decompress_lz78 -= 1
index_decompress_lz78 = index_decompress_lz78 % len(encodedNumbers)
data_decompressed_lz78 = decompress_lz78(encodedNumbers[:index_decompress_lz78 + 1], encodedLetters[:index_decompress_lz78 + 1], dictionary)
pyscript.write("data_decompressed_lz78", data_decompressed_lz78)
def button_decompress_next_lz78(condition):
global index_decompress_lz78, data_decompressed_lz78,encodedNumbers, encodedLetters, dictionary
index_decompress_lz78 += 1
index_decompress_lz78 = index_decompress_lz78 % len(encodedNumbers)
data_decompressed_lz78 = decompress_lz78(encodedNumbers[:index_decompress_lz78 + 1], encodedLetters[:index_decompress_lz78 + 1], dictionary)
pyscript.write("data_decompressed_lz78", data_decompressed_lz78)
Element("data_original_input_lz77").element.value = data_original_lz77
Element("data_compressed_input_lz77").element.value = [(0, 0, 't'), (0, 0, 'i'), (0, 0, 'p'), (1, 1, ' '), (5, 1, 'a'), (4, 3, 'i'), (9, 9, 'p'), (1, 1, 'e'), (6, 6, ' '), (21, 8, '_')]
Element("data_original_input_lz78").element.value = data_original_lz78
Element("data_compressed_input_lz78").element.value = [[0, 't'], [0, 'i'], [0, 'p'], [3, ' '], [1, 'a'], [4, 't'], [2, 'p'], [6, 'a'], [6, 'i'], [3, 'p'], [0, 'e'], [0, ' '], [1, 'i'], [10, 'e'], [12, 't'], [7, 'p'], [15, 'a'], [3, ' ']]
</py-script>
<div class="row">
<div class="col-lg-6 mb-4">
<div class="card">
<div class="card-header">
<h3>LZ77 Compression Algorithm</h3>
</div>
<div class="card-body">
<h5 class="card-title">Compress with LZ77</h5>
<p class="card-text">
<input type="text" id="data_original_input_lz77"
class="p-2 text-white bg-blue-600 border border-blue-600 rounded" style="width: 40vw;"/>
<button id="get_compress_input_lz77" class="p-2 text-white bg-blue-600 border border-blue-600 rounded"
type="submit"
pys-onClick="get_compress_input_lz77">
Enter
</button>
<div id="data_compressed_lz77" class="alert alert-primary" style="width: 40vw;"></div>
<button id="button_compress_previous_lz77" class="p-2 text-white bg-blue-600 border border-blue-600 rounded"
type="submit"
pys-onClick="button_compress_previous_lz77">
Previous
</button>
<button id="button_compress_next_lz77" class="p-2 text-white bg-blue-600 border border-blue-600 rounded"
type="submit"
pys-onClick="button_compress_next_lz77">
Next
</button>
</p>
<h5 class="card-title">Decompress with LZ77 </h5>
<p class="card-text">
<input type="text" id="data_compressed_input_lz77"
class="p-2 text-white bg-blue-600 border border-blue-600 rounded" style="width: 40vw;"/>
<button id="get_decompress_input_lz77" class="p-2 text-white bg-blue-600 border border-blue-600 rounded"
type="submit"
pys-onClick="get_decompress_input_lz77">
Enter
</button>
<div id="data_decompressed_lz77" class="alert alert-primary" style="width: 40vw;"></div>
<button id="button_decompress_previous_lz77"
class="p-2 text-white bg-blue-600 border border-blue-600 rounded" type="submit"
pys-onClick="button_decompress_previous_lz77">
Previous
</button>
<button id="button_decompress_next_lz77" class="p-2 text-white bg-blue-600 border border-blue-600 rounded"
type="submit"
pys-onClick="button_decompress_next_lz77">
Next
</button>
</p>
</div>
</div>
</div>
<div class="col-lg-6 mb-4">
<div class="card">
<div class="card-header">
<h3>LZ78 Compression Algorithm</h3>
</div>
<div class="card-body">
<h5 class="card-title">Compress with LZ78 </h5>
<p class="card-text">
<input type="text" id="data_original_input_lz78"
class="p-2 text-white bg-blue-600 border border-blue-600 rounded" style="width: 40vw;"/>
<button id="get_compress_input_lz78" class="p-2 text-white bg-blue-600 border border-blue-600 rounded"
type="submit"
pys-onClick="get_compress_input_lz78">
Enter
</button>
<div id="data_compressed_lz78" class="alert alert-primary" style="width: 40vw;"></div>
<button id="button_compress_previous_lz78" class="p-2 text-white bg-blue-600 border border-blue-600 rounded"
type="submit"
pys-onClick="button_compress_previous_lz78">
Previous
</button>
<button id="button_compress_next_lz78" class="p-2 text-white bg-blue-600 border border-blue-600 rounded"
type="submit"
pys-onClick="button_compress_next_lz78">
Next
</button>
</p>
<h5 class="card-title">Decompress with LZ78</h5>
<p class="card-text">
<input type="text" id="data_compressed_input_lz78"
class="p-2 text-white bg-blue-600 border border-blue-600 rounded" style="width: 40vw;"/>
<button id="get_decompress_input_lz78" class="p-2 text-white bg-blue-600 border border-blue-600 rounded"
type="submit"
pys-onClick="get_decompress_input_lz78">
Enter
</button>
<div id="data_decompressed_lz78" class="alert alert-primary" style="width: 40vw;"></div>
<button id="button_decompress_previous_lz78"
class="p-2 text-white bg-blue-600 border border-blue-600 rounded" type="submit"
pys-onClick="button_decompress_previous_lz78">
Previous
</button>
<button id="button_decompress_next_lz78" class="p-2 text-white bg-blue-600 border border-blue-600 rounded"
type="submit"
pys-onClick="button_decompress_next_lz78">
Next
</button>
</p>
</div>
</div>
</div>
</div>
</body>
</html>