Public
Edited
Apr 21, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
findDisplay = function() {
if (buttonWithValue == true) {
//if the button has been pressed...
return html`${
/*"I need something to hang onto."*/ (newpulse,
rs`I need (something | someone) to hang onto.`)
}`;
} //display entire poem
else {
//if the button hasn't been pressed...
return html`${""}`;
} //display up to "never"
}
Insert cell
newpulse = pulse(2000)
Insert cell
ticker = {
while (true) {
yield await Promises.tick(9000 / x[0]).then(() => {
mutableTxt[0] = update();
display.innerText = mutableTxt[0].replace(/<br>/g, " \n ");
x[0] = x[0] + 1;
//.replace(/<br>/g, " \n ");
});
}
}
Insert cell
x = [1]
Insert cell
x[0]
Insert cell
numberOfPossibleReplacements = 1
Insert cell
update = function() {
let words = mutableTxt[0].split(" ");
//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 || word == "<br>") continue; // len >= 3 or line break

// 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;
}
}
words = words.join(" ");
//words = RiTa.untokenize(words);
return words;
//.replace(/<br>/g, " <br> ");
// 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
mutableTxt = [
"<br> I'm writing this in hopes <br> my message reaches you. <br> Today I walked to work <br> and had carrots for lunch. <br> <br> My mother's doing well, <br> how about yours? <br> And how are you? <br> Tell me even though <br> I'll never completely understand. <br> Tell me before the words fall through. <br> <br> Hello? Is anybody out there? <br> Has my message reached you?"
]
Insert cell
mutableTxt[0]
Insert cell
RiTa.tokenize(mutableTxt[0])
Insert cell
Insert cell
Insert cell
Insert cell
RiTa.VERSION
Insert cell
import { Button, Text, Checkbox, Select } from "@observablehq/inputs"
Insert cell
import { rs, pulse, verseTag } from "@dla/imports"
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