xxxxxxxxxx
37
let words = []; //1. declare variable words and initialize it with an empty
let p; //2. declare variable p
let str = ''; //declare str variable and initialize with an empty string
function preload() { //4. preload text file
loadStrings('bang.txt', process);
}
function setup() {
noCanvas(); 15. Don't draw a canvas
p = createP(); 16. Create a paragraph element and assign it p
}
function draw() {
if(frameCount%30 ==0) { //every 30 frames
let next_word = random(words); //pick a random word out of the words array and assign it to the next_word variable
str += next_word + ' '; //add next_Word and space to str available
p.html(str);
}
}
function process(lines) { //5.turning the text into individual words
for(let ln of lines) { // 5.iterate through each line of text
let tokens = splitTokens(ln); // 6. split the ine into word units
words = words.concat(tokens); //7.
}
for(let w = words.length - 1; w >=0; w--) {
let word = words[w]; //9.Get me the words stored at index w in the words array, assign it to a variable called word
word = word.replace(/[\-_!?.,;:\(\)]/g, ''); //10. take the word, replace any punctuation with nothing and assign it back to word.
word = word.toLowerCase(); //11. take the word, lowercase it and assign it back to word
word = word.trim(); //13. if empty word, remove it from the array
if(word.length < 1) words.splice(w, 1);
else words[w] = word;
}
}