forked from AlgorithmsMeetup/HuffmanCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.html
318 lines (279 loc) · 7.87 KB
/
Demo.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
<html>
<head>
<title>Huffman Coding Demo</title>
<script src="lib/jquery.js"></script>
<script src="src/priorityQueue.js"></script>
<script src="src/misc.js"></script>
</head>
<body>
<div class='column c1'>
<h2>Corpus:</h2>
<textarea class='corpus'></textarea>
<div class='buttons'>
<button class='generate'>Generate</button>
<button class='lorem'>Use Lorem Ipsum</button>
<button class='declaration'>Use Declaration of Independence</button>
</div>
<h2>Plain text:</h2>
<textarea type='text' class='encode'></textarea>
<div class='buttons'>
<button class='encode'>Encode</button>
<button class='encodeCorpus'>Use Corpus</button>
</div>
<h2>Coded text:</h2>
<textarea type='text' class='decode'></textarea>
<div class='buttons'>
<button class='decode'>Decode</button>
</div>
<div class='error'></div>
</div>
<div class='column c2'>
<h2>Your encoding scheme:</h2>
<ul class='coding'>
</ul>
<div class='efficiency'>
<h2>Efficiency:</h2>
<div class='standard'></div>
<div class='huffman'></div>
<div class='reduction'></div>
</div>
</div>
<style>
body {
font-family: courier;
}
.column {
margin: 0;
padding: 0 10px;
float: left;
}
.c1 {
width: 55%;
}
.c2 {
width: 40%;
}
.buttons {
margin-top: 5px;
margin-bottom: 10px;
}
.error {
color: #a00;
}
.efficiency {
margin-top: 20px;
float: left;
display: none;
}
body {
margin: 5px;
padding: 0 10px 20px;
}
h2 {
margin-top: 0;
margin-bottom: 5px;
}
textarea {
min-width: 80%;
min-height: 130px;
}
input {
min-width: 80%;
}
ul {
margin: 0;
padding: 0;
}
li {
display: block;
width: 48%;
margin: 0;
float: left;
white-space: pre-wrap;
}
</style>
<script>
var countChars = function(input) {
var counts = {};
for (var i = 0; i < input.length; i++) {
var c = input[i];
counts[c] = counts[c] || 0;
counts[c]++;
}
return counts;
};
var makeHuffmanTree = function(input) {
var charCounts = countChars(input);
var pq = new PriorityQueue();
for (var c in charCounts) {
var n = charCounts[c];
var tree = new Tree([c]);
pq.insert(n, tree);
}
while (pq.size() > 1) {
var first = pq.extract();
var second = pq.extract();
var tree1 = first.val;
var tree2 = second.val;
var key1 = first.key;
var key2 = second.key;
var newTree = new Tree(tree1.val.concat(tree2.val));
newTree.left = tree1;
newTree.right = tree2;
pq.insert(key1+key2, newTree);
}
return pq.extract().val;
};
var encodeChar = function(c, huffman) {
var output = "";
while (huffman.val.length > 1) {
if (huffman.left.val.indexOf(c) !== -1) {
huffman = huffman.left;
output += "0";
} else if (huffman.right.val.indexOf(c) !== -1) {
huffman = huffman.right;
output += "1";
} else {
throw new Error("Character " + c + " is not in this Huffman tree.");
}
}
return output;
};
var encodeString = function(input, huffman) {
var output = "";
for (var i = 0; i < input.length; i++) {
output += encodeChar(input[i], huffman);
}
return output;
};
var decodeString = function(input, huffman) {
var output = "";
var currNode = huffman;
for (var currIdx = 0; currIdx < input.length; currIdx++) {
var currBit = input[currIdx];
if (currBit === "0") {
currNode = currNode.left;
} else if (currBit === "1") {
currNode = currNode.right;
} else {
throw new Error("String to decode must consist of only 0s and 1s.");
}
if (currNode.val.length === 1) {
output += currNode.val[0];
currNode = huffman;
}
}
if (currNode !== huffman) {
throw new Error("String cannot be decoded - make sure you aren't missing any bits!");
}
return output;
};
var getDictionaryFromTree = function(tree) {
var result = {};
var traverse = function(node, current) {
if (node.val.length === 1) {
result[node.val] = current;
}
if (node.left) {
traverse(node.left, current + "0");
}
if (node.right) {
traverse(node.right, current + "1");
}
};
traverse(tree, "");
return result;
};
var calcEfficiency = function(uncodedLen, codedLen, charSetSize) {
var stdWordSize = Math.ceil(Math.log(charSetSize)/Math.LN2);
var stdCodedLen = stdWordSize*uncodedLen;
var reduction = ((stdCodedLen - codedLen)*100/stdCodedLen).toFixed(2) + "%";
$('.efficiency').show();
$('.efficiency .standard').text("With standard coding: " + stdCodedLen + " bits");
$('.efficiency .huffman').text("With Huffman coding: " + codedLen + " bits");
$('.efficiency .reduction').text("Reduction: " + reduction);
};
var huffman = null;
var triggerGenerateHuffman = function() {
$(".efficiency").hide();
var corpus = $("textarea.corpus").val();
huffman = makeHuffmanTree(corpus);
var dictionary = getDictionaryFromTree(huffman);
var ul = $('.coding');
ul.html('');
var pairs = Object
.keys(dictionary)
.sort(function(a, b) {
return a < b ? -1 : 1;
})
.forEach(function(key) {
var li = $("<li></li>");
li.text(key + " - " + dictionary[key]);
ul.append(li);
});
};
var triggerLorem = function() {
var corpus = $("textarea.corpus").val(loremIpsum);
triggerGenerateHuffman();
};
var triggerDeclaration = function() {
var corpus = $("textarea.corpus").val(declaration);
triggerGenerateHuffman();
};
var triggerEncode = function() {
$("textarea.decode").val('');
if (!huffman) {
$(".error").text("Please enter a corpus and create an encoding scheme first.")
return;
};
var input = $('textarea.encode').val();
if (!input) {
$(".error").text("Please enter some text to encode.")
return;
}
var output;
try {
output = encodeString(input, huffman);
} catch (ex) {
$('.error').text(ex.message);
return;
}
$('.error').text('');
$("textarea.decode").val(output);
calcEfficiency(input.length, output.length, huffman.val.length)
};
var triggerEncodeCorpus = function() {
$('textarea.encode').val($('textarea.corpus').val());
triggerEncode();
};
var triggerDecode = function() {
$(".efficiency").hide();
$("textarea.encode").val('');
if (!huffman) {
$(".error").text("Please enter a corpus and create an encoding scheme first.")
return;
};
var input = $('textarea.decode').val();
if (!input) {
$(".error").text("Please enter some text to decode.")
return;
}
var output;
try {
output = decodeString(input, huffman);
} catch (ex) {
$('.error').text(ex.message);
return;
}
$('.error').text('');
$("textarea.encode").val(output);
};
$(document).on('click', 'button.generate', triggerGenerateHuffman);
$(document).on('click', 'button.lorem', triggerLorem);
$(document).on('click', 'button.declaration', triggerDeclaration);
$(document).on('click', 'button.encode', triggerEncode);
$(document).on('click', 'button.encodeCorpus', triggerEncodeCorpus);
$(document).on('click', 'button.decode', triggerDecode);
</script>
</body>
</html>