Published
Edited
Apr 5, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
displayText = function() {
// DEBUGGING console.log("displayText");
if (unusedWords[0].length > 0) {
//random index of unusedWords
let index = getRandomInt(0, unusedWords[0].length);
//word to replace chosen from list of unused words
let wordToReplace = unusedWords[0][index];
//generate gibberish based off word to replace
let gibberish = generateGibberish(wordToReplace);
//Dictionary[wordToReplace] = gibberish;

//adding user input & gibberish to Dictionary object:
if (textInput !== "") {
let text = textInput;
Dictionary.set( wordToReplace, {gibberishWord: gibberish, userSynonym: text});
}

//For removing highlighting, if I manage to add it in becomeGibberish:
// mutableTxt[0] = mutableTxt[0].replace("<mark>", "");
// mutableTxt[0] = mutableTxt[0].replace("</mark>", "");

mutable txt = becomeGibberish(
// The following does not treat things like "<" as tokens: --why does tokenize make "<" into tokens but not split?
txt.split(" "),
// RiTa.tokenize(mutableTxt[0]),
wordToReplace,
gibberish
); //word to gibberish
unusedWords[0].splice(index, 1); //removed the replaced word from the list of unused words

display2.innerText = "‘" + gibberish + "’ means:";
} else {
display2.innerText = "Done.";

//pause a couple second
//button appears on screen with "Write a poem?" on it
//-->pressing the button causes the current display to fade out and me replaced by the new poem appearring word by shifting word on screen
}
//}
}
Insert cell
// document.addEventListener('keydown', displayText);
Insert cell
/* document.addEventListener('keydown', e => {
console.log(`${e.key} pressed`); // for DEBUGGING ONLY
e.stopPropagation();
if (e.key == "Enter") {
e.key = "";
displayText();
}
})*/
Insert cell
/*update = function(lPoem) {
let words = RiTa.tokenize(mutableTxt[0]); // split into words

// loop a random number of times: 1 - numberOfPossibleReplacements
//loop from 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 idx = r % words.length;
words[idx] = randomWord(lPoem[idx], words[idx]);
//}
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
/*viewof buttonWithValue = Button("Reset", {
reduce: functionRunOnClick
})*/
Insert cell
/*functionRunOnClick = function() {
mutableTxt[0] = unmutableTxt [0];
unusedWords[0] = [generateUnusedWords(unmutableTxt[0])];
display1.reload();
display2.reload();
}*/
Insert cell
/*functionRunOnClick = function() {
mutableTxt[0] = unmutableTxt[0];
unusedWords[0] = [generateUnusedWords(unmutableTxt[0])];
display1.innerHTML = mutableTxt[0].replace(/-g/g, " \n");
display2.innerText = 'Press Enter:';
}*/
Insert cell
Insert cell
hi = "bet.".includes(".");
Insert cell
Insert cell
//choosing random word in poem to turn into Gibberish
function becomeGibberish(poem, wordToReplace, gibberish) {
//words = string of poem
for (let ii = 0; ii < poem.length; ii++) {
let word = poem[ii].replace(" ", "");
word = poem[ii].replace(".", "");
word = poem[ii].replace(",", "");
if (word.toLowerCase() == wordToReplace) {
//if the element contains the random unused word
if (poem[ii].includes(".")) {
gibberish = gibberish + "."
}
else if(poem[ii].includes(",")) {
gibberish = gibberish + ",";
}
if (/[A-Z]/.test(word[0])) {
poem[ii] = " <mark>" + RiTa.capitalize(gibberish) +"</mark> "; // keep capitals
} else {
poem[ii] = " <mark>" + gibberish +"</mark> "; //["<mark>", gibberish, "</mark>"]; //replace the element with gibberish //md`<mark> gibberish </mark>` //supposed to highlight but won't work
}
}
}
// let newPoem = RiTa.untokenize(poem);
return poem.join(" ");
}
Insert cell
md`Test: ${becomeGibberish("very short poem".split(" "), "short", testShort)}`
Insert cell
testShort = generateGibberish("short")
Insert cell
RiTa.tokenize("very short poem")[0].toLowerCase()
Insert cell
Insert cell
//Creating new Gibberish Words based of number of syllabels in input word
function generateGibberish(oWord){
let syllables = RiTa.syllables(oWord);
let syllablesArray = syllables.split('/');
var numSyllables = syllablesArray.length;
var gibberish = "";
var notOneVowel = (oWord.length > 1 && numSyllables == 1);
//stuff about contractions
/*if has Prefix
if ending of prefix = vowel --> start root word with consonant
else --> start root word with vowel
if has suffix
if beginning of suffix = vowel --> end root word with consonant
else --> end root word with vowel
*/

let rand = Math.random();
//What word starts with
if (rand <0.3){
var startVowel = true;
} else {
var startVowel = false;
}
for (let j = 0; j < numSyllables; j++){ //loop for number of syllables
//Syllable starts w/ Vowels
if (startVowel == true) {
gibberish += createVowel(notOneVowel);
}
else {
gibberish += createConsonant();
}
if(consonants.indexOf(gibberish.slice(-1)) > 0){
startVowel = true;
}
else{
startVowel = false;
}
}
//if gibberish == real word --> create new gibberish word
return gibberish;
}
Insert cell
//Create a single syllable starting with a vowel:
function createVowel(notOneVowel){
let newVowel = vowels[getRandomInt(0, vowels.length)]; //random starting vowel
let numLetters = getRandomInt(1, 3); //generate random number for either 1 or 2 letter syllable
if (notOneVowel === true){
numLetters = 2;//if one syllable word with more than 1 letter
} //don't allow gibberish to be 1 vowel
if (numLetters > 1){ //if syllable has more than 1 letter
newVowel += consonants[getRandomInt(0, consonants.length)]; //add a random consonant
}
return newVowel;
}
Insert cell
//Create a single syllable starting with a consonant
function createConsonant(){
let newConsonant = consonants[getRandomInt(0, consonants.length)] + vowels[getRandomInt(0, vowels.length)]; //random starting consonant and random addition vowel
let numLetters = getRandomInt(2, 4); // random length 2-3
if(numLetters > 2){ //if syllable 3 letters
newConsonant += consonants[getRandomInt(0, consonants.length)]; //add consonant
}
return newConsonant;
}
Insert cell
function findSuffix(word){
//find suffix from array of prefixes
//group word with other words
//remove syllables from oWord
}
Insert cell
function findPrefix(word){
//find prefix from array of prefixes
//group word with other words
//remove syllables from oWord
}
Insert cell
//This is broken I need to refine the coding later
function findRootWords(poem){
let roots = [];
for(let ii = 0; ii < poem.length; ii++){ //for every word in the poem
let rootWord = poem[ii];
let thisPrefix = [];
let thisSuffix = [];
//is there a prefix? --> remove it
for (let jj = 0; jj < prefixes.length; jj++){
if(rootWord.startsWith(prefixes[jj])){
thisPrefix.push(prefixes[jj]);
rootWord = rootWord.replace(prefixes[jj],"");
}
}
//is there a suffix --> remove it
for (let jj = 0; jj < suffixes.length; jj++){
if(rootWord.endsWith(suffixes[jj])){
thisSuffix.push(suffixes[jj]);
rootWord = rootWord.replace(suffixes[jj],"");
}
}
//add to broken down word into a list of objects of root words, prefixes, and suffixes --> when generating gibberish can keep track of letter start and end requirements
roots.push({root: rootWord, prefix: thisPrefix, suffix: thisSuffix});
}
return roots;
}

/*From paragraph that evolves:

if (next.includes(word) || word.includes(next)) {
continue; // skip substrings
}*/
Insert cell
function createPrefixes(prefixList){
let newPrefixList = [];
for(let ii = 0; ii < prefixList.length; ii++){
newPrefixList[ii] = generateGibberish(prefixList[ii]);
}
return newPrefixList;
}
Insert cell
function createSuffixes(suffixList){
let newSuffixList = [];
for(let ii = 0; ii < suffixList.length; ii++){
suffixList[ii] = generateGibberish(suffixList[ii]);
}
return newSuffixList;
}
Insert cell
//Generate an array of all words in an Array[0], with all repeated words removed
function generateUnusedWords(poem){
let words = poem.toLowerCase();
words = RiTa.tokenize(words);
//remove punctuation and -g(line break symbol)
let i = 0;
while(i < words.length){
if (words[i] == "." || words[i] == ","){
words.splice(i, 1);
}
else {
++i;
}
}
return [...new Set(words)];
}
//in future need to alter to account for prefixes and suffixes

/*From paragraph that evolves:

if (next.includes(word) || word.includes(next)) {
continue; // skip substrings
}*/
Insert cell
Insert cell
function generateWordArrays(thePoem){
let fullPoem = [];
let words1 = RiTa.untokenize(thePoem);
let words2 = translatePoem("originalWord", "gibberishWord", thePoem);
let words3 = translatePoem("gibberishWord", "userSynonym", thePoem);
for(let ii = 0; ii < words1.length; ii++){
fullPoem[ii] = Array(words1[ii], words2[ii], words3[ii]);
}
return fullPoem; //output = array of lists of strings
}
Insert cell
function translatePoem(fromWhat, toWhat, thePoem) {
RiTa.tokenize(thePoem[0]);
let newPoem = thePoem;
for (let i=0; i < thePoem.length; i++){
for (let j=0; j < Dictionary.length; j++){
if (Dictionary[j].fromWhat === thePoem[i]){
newPoem[j] = Dictionary[j].toWhat;
}
}
}
return newPoem; //output array of strings
}
Insert cell
function randomWord(wordArray, currentWord) {
// only works with flat arrays of words
let safetyCounter = 0,
word;
do {
word = wordArray[getRandomInt(wordArray.length)];
} while (
currentWord != undefined &&
word == currentWord &&
safetyCounter++ < 100
);
return word;
}
Insert cell
Insert cell
Insert cell
function gibPoem(oPoem){
let nPoem = oPoem;
for(let ii = 0; ii < oPoem.length; ii++){
if(oPoem[ii] == "-g"){
nPoem[ii] = "-g";
}
else{
nPoem[ii] = generateGibberish(oPoem[ii]);
}
}
return nPoem;
}
Insert cell
Insert cell
Dictionary = new Map();
Insert cell
Insert cell
mutable txt = testpoem.innerHTML
Insert cell
Insert cell
Insert cell
rootWords = [findRootWords(unmutableUnusedWords[0])];
Insert cell
testing2 = findRootWords(["undone", "done", "ouch"]);
Insert cell
testing = generateUnusedWords(unmutableTxt[0]);
Insert cell
display2 = md`Click ‘Gibberfy’`
Insert cell
Insert cell
Insert cell
prefixes = ["un", "pre", "in"/*this one may be tough*/, "con"]; //need to add more
Insert cell
suffixes = ["ing", "ish", "less", "ee", "ence", "ful" ]; // also need to add more
Insert cell
gibberishPrefixes = [createPrefixes(prefixes)]
Insert cell
gibberishSuffixes = [createSuffixes(suffixes)];
Insert cell
h = "texting"
Insert cell
function fun(){
let h = "testing";
let e = suffixes[0];
return h.replace(e, "");
}
Insert cell
fun();
Insert cell
function p(){
return suffixes[0];
}
Insert cell
p();
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { Button, Text } from "@observablehq/inputs"
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