Published
Edited
Mar 23, 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
document.addEventListener('keydown', e => {
e.stopPropagation();
if (e.key == "Enter") {
//if Enter is pressed
console.log(`${e.key} pressed`); // for DEBUGGING ONLY
if (unusedWords[0].length > 0) {
let text = ``;
let index = getRandomInt(0, unusedWords[0].length); //random index of unusedWords
let wordToReplace = unusedWords[0][index]; //word to replace chosen from list of unused words
let gibberish = generateGibberish(wordToReplace); //generate gibberish based off word to replace

//adding user input & gibberish to Dictionary object:
//if(textInput !== ""){
// Dictionary.push({word: gibberish, definition: Text});
//}

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

mutableTxt[0] = becomeGibberish(
RiTa.tokenize(mutableTxt[0]),
wordToReplace,
gibberish
); //word to gibberish
unusedWords[0].splice(index, 1); //removed the replaced word from the list of unused words

display1.innerText = mutableTxt[0].replace(/-g/g, " \n");
display2.innerText = gibberish.concat(":");
} else {
display1.innerText = mutableTxt[0].replace(/-g/g, " \n");
display2.innerText = "Done.";
}
//}
}
})
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
md`How highlighting works:`
Insert cell
md`${'<mark>"hello"</mark>'}`
Insert cell
html`<mark>"hello"</mark>`
Insert cell
md`<mark>hello</mark>`
Insert cell
Insert cell
Insert cell
Insert cell
//choosing random word in poem to turn into Gibberish
function becomeGibberish(poem, wordToReplace, gibberish) {
for (let ii = 0; ii < poem.length; ii++) { //loop through all elements of words(mutableTxt)
let word = poem[ii];
word = word.toLowerCase();
if(findPrefix(word) != ""){
word = gibberishPrefixes[findIndex(prefixes, findPrefix(word))] + word; //gibberish prefix
}
if(findSuffix(word) != ""){
word = word + gibberishSuffixes[findIndex(suffixes, findSuffix(word))]; //gibberish suffix
}
if (word.toLowerCase().includes(wordToReplace)) {
//if the element contains the random unused word
if (/[A-Z]/.test(poem[ii][0])) {
poem[ii] = RiTa.capitalize(word); // keep capitals
} else {
poem[ii] = word; //["<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 newPoem;
}
Insert cell
look = gibberishPrefixes[findPrefix("unorder")]
Insert cell
ouch = becomeGibberish(["unorder", "up"], "order", "cccc")
Insert cell
ouch2 = becomeGibberish("order")
Insert cell
md`Test: ${becomeGibberish(
RiTa.tokenize("very short poem"),
"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;
}
}
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
//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 prefix = "";
let suffix = "";
let thisPrefix = [];
let thisSuffix = [];
//is there a prefix? --> remove it
prefix = findPrefix(rootWord);;
rootWord = rootWord.replace(prefix,"");
//is there a suffix --> remove it
suffix = findSuffix(rootWord);;
rootWord = rootWord.replace(suffix,"");
//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(rootWord);
}
return [...new Set(roots)];
}

/*From paragraph that evolves:

if (next.includes(word) || word.includes(next)) {
continue; // skip substrings
}*/
Insert cell
function findPrefix(word){
let pref = "";
for (let jj = 0; jj < prefixes.length; jj++){
if(word.startsWith(prefixes[jj])){
pref = prefixes[jj]
}
}
return pref;
//find prefix from array of prefixes
//group word with other words
//remove syllables from oWord
}
Insert cell
function findSuffix(word){
let suff = "";
for (let jj = 0; jj < suffixes.length; jj++){
if(word.endsWith(suffixes[jj])){
suff = suffixes[jj]
}
}
return suff;
//find suffix from array of prefixes
//group word with other words
//remove syllables from oWord
}
Insert cell
function createPrefixes(prefixList){
let prefixListCopy = prefixList;
let newPrefixList = []
for(let ii = 0; ii < prefixListCopy.length; ii++){
newPrefixList[ii] = generateGibberish(prefixListCopy[ii]); //associative array
}
return newPrefixList;
}
Insert cell
function createSuffixes(suffixList){
let suffixListCopy = suffixList;
let newSuffixList = []
for(let ii = 0; ii < suffixListCopy.length; ii++){
newSuffixList[ii] = generateGibberish(suffixListCopy[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){
//remove prefix
words[i] = words[i].replace(findPrefix(words[i]),"");
//remove suffix
words[i] = words[i].replace(findSuffix(words[i]),"");
if (words[i] == "." || words[i] == "," || words[i] == "-g"){
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 findIndex(array, string){
for (let index = 0; index < array.length; index++){
if (array[index] == string){
return index;
}
}
}
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
Insert cell
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`${'Press Enter:'}`;
Insert cell
Insert cell
Insert cell
Insert cell
prefixes = ["un", "pre", "in", "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
import {Text} from "@observablehq/inputs";
Insert cell
import {Button} 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