Public
Edited
Feb 17
ramble – development
Replaceable Quatrains
Also listed in…
quatrains and more
Insert cell
Insert cell
Insert cell
Insert cell
ticker = {
while (true) {
yield await Promises.tick(seconds * 1000).then(() => {
mutableTxt[0] = shuffleUpdate();
// display.innerText = mutableTxt[0];
return posText(mutableTxt[0]);
});
}
}
Insert cell
numberOfPossibleReplacements = 1
Insert cell
shuffleUpdate = function() {
let words = RiTa.tokenize(mutableTxt[0]); // split into words

// replace each word in shuffled orders
if (rindex == 0) {
shuffle(shuffledArray);
console.log("shuffled ...");
}
let idx = shuffledArray[rindex];
rindex[0] = rindex[0] + 1;
if (rindex[0] + 1 > words.length) {
rindex[0] = 0;
}

let word = words[idx].toLowerCase();
if (word.length < 3) return RiTa.untokenize(words); // len >= 3

// find related words
// constraining via partofs[] to the original supply text's pos's
let pos = partofs[idx];
// 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 synonyms = RiTa.synonym(word, { pos });
// similars will not have duplicates in it:
let similars = [...new Set([...rhymes, ...sounds, ...spells])]; // ...rhymes, ...sounds, ...spells
console.log(similars);

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

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

if (next.length > 11) return RiTa.untokenize(words);

if (next.includes(word) || word.includes(next)) {
return RiTa.untokenize(words); // 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

return RiTa.untokenize(words);
}
Insert cell
rindex = [0]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
preText = function(displayString) {
let str = "";
let arr = displayString.split(" ");
for (let i = 0; i < 20; i = i + 5) {
str += arr.slice(i, i + 5).join(" ") + "\n";
}
str += "\n";
str +=
"rindex[0]: " + rindex[0] + ", idx: " + shuffledArray[rindex[0]] + "\n";
return "<pre>" + str + "</pre>";
}
Insert cell
otherText = "empty islands never see humans yet hear human language echoes returning sunlight enters under boughs again gleaming green mosses canopy"
Insert cell
supplyText = "tears embers after rice sauce ink news kites of far blank loss another now knots ghost heart over stone tablets"
Insert cell
moreText = "myriad mountains bird flights end numberless pathways human traces dust lone canoe gore-tex hooded zen lonely line cold river snow"
Insert cell
posText = function(displayString) {
let str = "";
let arr = displayString.split(" ");
for (let i = 0; i < 20; i++) {
str += `<span class='text visible' style='top:${positions[i][0]}vw;left:${positions[i][1]}vw;'>${arr[i]}</span>`;
}
return str;
}
Insert cell
Insert cell
Insert cell
import { andaleMono } from "@shadoof/sifther"
Insert cell
css = html`
<style>

@font-face {
font-family: 'AndaleMono';
src: url('${await andaleMono.url()}');
}

.textcontainer {
position: relative;
min-height: 270px;
font-family: 'AndaleMono', monospace;
font-size: 2.65vw;
}

.text {
position: absolute;
color: rgba(0,0,0,0);
transition: color 1s ease-in-out;
}

.text.visible {
color: rgba(0,0,0,255);
/* starts readable */
}

</style>
`
Insert cell
RiTa.VERSION
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
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 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
import { shuffle } from "@shadoof/utils"
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