Public
Edited
Feb 11
Insert cell
Insert cell
Insert cell
Insert cell
ticker = {
while (true) {
yield await Promises.tick(1000).then(() => {
mutableTxt[0] = update();
display.innerText = mutableTxt[0];
});
}
}
Insert cell
numberOfPossibleReplacements = 1
Insert cell
update = function() {
let words = RiTa.tokenize(mutableTxt[0]); // split into words

// loop a random number of times: 1 - numberOfPossibleReplacements
let l = Math.ceil(Math.random() * numberOfPossibleReplacements);
for (let j = 0; j < l; j++) {
// loop from a random spot
let r = Math.floor(Math.random() * words.length);
for (let i = r; i < words.length + r; i++) {
let idx = i % words.length;
let word = words[idx].toLowerCase();
if (word.length < 3) continue; // len >= 3

// find related words
console.log(`word: ${word}`);
let pos = RiTa.tagger.allTags(word)[0];
console.log(`word: ${word} pos: ${pos}`);
let rhymes = RiTa.rhymes(word, { pos });
let sounds = RiTa.soundsLike(word, { pos });
let spells = RiTa.spellsLike(word, { pos });
let similars = [...rhymes, ...sounds, ...spells];

// only words with 2 or more similars
if (similars.length < 2) {
console.log("No sims for " + word);
continue;
}

// pick a random similar
let next = RiTa.random(similars);

if (next.includes(word) || word.includes(next)) {
continue; // skip substrings
}

if (/[A-Z]/.test(words[idx][0])) {
next = RiTa.capitalize(next); // keep capitals
}

console.log("replace(" + idx + "): " + word + " -> " + next);
words[idx] = next; // do replacement
break;
}
}
return RiTa.untokenize(words);
// mutable txt = RiTa.untokenize(words); // actually even with await Promises ... this does not work
// mutableTxt[0] = RiTa.untokenize(words); // this would also work
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
RiTa.VERSION
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