Published
Edited
Mar 23, 2021
Insert cell
Insert cell
Insert cell
Insert cell
N = 4;
Insert cell
generate = function() {
let next, result = [];
let starts = Object.values(root.children).filter(n => /^[.?!]$/.test(n.word));
let current = root.child('<s>');
while (!current.isLeaf()) {
next = weightedChoice(Object.values(current.children));
result.push(next);
current = next;
}

while(!/^<\/s>$/.test(next.word)) {
let path = result.slice(-(N-1));
current = search(path);
next = weightedChoice(Object.values(current.children));
if (!next) break;
result.push(next);
}
return RiTa.untokenize(result.map(n => n.word)).replace(/"/g,'');
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
class Node {
constructor(word) {
this.word = word;
this.count = 0;
this.children = {};
}
child(word) {
return this.children[word];
}
isLeaf() {
return Object.keys(this.children) == 0;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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