Public
Edited
May 22, 2021
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
replace = function (para) {
let words = RiTa.tokenize(para); // split into words
let indices = RiTa.randomOrdering(words.length); // shuffle indices

for (let i = 0; i < indices.length; i++) {
let idx = indices[i];
let word = words[idx].toLowerCase();
if (word.length < 3) continue; // len >= 3

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

// only keep 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, +new Date());
words[idx] = next; // do replacement
break;
}

return RiTa.untokenize(words);
}
Insert cell
Insert cell
Insert cell
Insert cell
RiTa.VERSION

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