Public
Edited
Apr 6, 2023
Insert cell
Insert cell
char_codes = Array.from({ length: 256 }, (_, i) => i)
Insert cell
char_map = new Map(char_codes.map((i) => [i, String.fromCharCode(i)]))
Insert cell
viewof text = Inputs.text({ value: "TOBEORNOTTOBEORTOBEORNOT#" })
Insert cell
Insert cell
function* words(curr) {
// yield* ["hello"];
// return;
if (curr.terminal || curr.children.size == 0) {
yield "";
// return;
}
for (let [c, node] of curr.children.entries()) {
for (let suffix of words(node)) {
yield `${c}${suffix}`;
}
// yield* words(node).map((suffix) => `{c}{suffix}`);
// .map((suffix) => `{c}{suffix}`)
}
}
Insert cell
trie = {
const trie = new TrieNode();
trie.add("TOBEORNOTTOBEORTOBEORNOT#");
trie.set("TOBO", 22);
return trie;
}
Insert cell
trie.get("TOBO")
Insert cell
trie.children.size
Insert cell
Array.from(words(trie))
Insert cell
{
const trie = new TrieNode();
// trie.add("h");
trie.add("hello");
trie.add("shello");
trie.add("hero");
trie.add("heroics");
trie.set("heron", 22);
return Array.from(words(trie));
}
Insert cell
prefix = trie.search(text)
Insert cell
new Object({
...prefix,
prefix: text.slice(0, prefix.i),
terminal: prefix.curr.terminal
})
Insert cell
function compress(text) {
let result = null;
for (let out of compress_generator(text)) {
result = out;
}
return result;
}
Insert cell
function* compress_generator(text) {
const trie = new TrieNode();
// trie.set("\0", trie.size);
let new_text = [];
const encoding_dict = new Map();
const first_position = new Map();
let i = 0;
while (i < text.length) {
let text_slice = text.slice(i, text.length);
console.log(`i=${i}, size=${trie.size} and ${text_slice}`);
const prefix = trie.search(text_slice);
// break;
// trie.add(text_slice);
const prefix_slice = text.slice(i, i + prefix.i + 1);
if (prefix.i < text_slice.length) {
// Some new elements in prefix slice
trie.set(prefix_slice, trie.size);
first_position.set(trie.size, i);
}
encoding_dict.set(trie.get(prefix_slice), prefix_slice);
new_text.push(trie.get(prefix_slice));
console.log(prefix_slice, new_text, encoding_dict);
i += prefix.i + 1; // Consumed i length, move to next
yield { new_text, trie, encoding_dict, i, first_position };
}

// yield { new_text, trie, encoding_dict, i, first_position };
}
Insert cell
htl.html`${viewof text}`
// TOBEORNOTTOBEORTOBEORNOT#
Insert cell
{
let prev_i = 0;
let codes = [];
for (let out of compress_generator(text)) {
await Promises.delay(2000);
const new_slice = text.slice(prev_i, out.i);
let new_slice_code = 0;
let prefix = new_slice.slice(0, new_slice.length - 1);
try {
// new_slice_code = trie.get(text.slice(prev_i, out.i - 1));
new_slice_code = out.trie.get(prefix);

new_slice_code =
new_slice_code === null
? new_slice[new_slice.length - 1]
: `${new_slice_code}${new_slice[new_slice.length - 1]}`;
} catch (error) {
// yield htl.html`${error}`;
new_slice_code = error;
// await Promises.delay(2000);
}

codes.push(new_slice_code);

yield htl.html`<div style="word-wrap: break-word;"><p><span style="color: green">${text.slice(
0,
prev_i
)}</span><span style="color: red">${text.slice(
prev_i,
out.i
)}</span>${text.slice(out.i)}</p>
<pre>new_slice = ${new_slice} prefix=${prefix}
prev_i = ${prev_i}
out.i = ${out.i}
new_slice_code = ${new_slice_code}
codes = ${codes.join(", ")}
words = ${Array.from(words(out.trie)).join(", ")}
</pre></div>`;

// if (new_slice_code === null) return;
prev_i = out.i;
}

// new_slice_code = ${new_slice_code}-${text[out.i]}
// // new_slice_code = ${trie.get(text.slice(prev_i, out.i))}${text[out.i]}
}
Insert cell
text.length
Insert cell
compressed_text = compress(text)
// compressed_text = compress("TOBEORNOT")
Insert cell
Array.from(words(compressed_text.trie))
Insert cell
compressed_text.new_text
.map((x) => compressed_text.encoding_dict.get(x))
.join("")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more