xxxxxxxxxx
36
let words = []; //1 declare a variable words and initialize it with an empty
let p; // 2 declare variable called p to hold paragraph element
let str = ''; // 3 declare str variable and initialize with an empty string
function preload() { // 4 preload text files
loadStrings('bang.txt', process);//1
}
function setup() {
noCanvas();//16 remove canvas
p = createP(); // 17 create a paragraph element and assign it p
}
function draw() {
if(frameCount%30 ==0) { //18 every 30 frame
let next_word = random(words); //19 pick a random word out of the word array
str += next_word + ' '; //20 add next word and space to str variable
p.html(str); //21
}
}
function process(lines) { // 5 turning the text into individual words
for(let ln of lines) { // 6 iterate each line of the text files
let tokens = splitTokens(ln); // 7 split the line into word units
words = words.concat(tokens); //8 add tokens to the words array as individual words
}
for(let w = words.length - 1; w >=0; w--) { // 9 loop through words backwards
let word = words[w]; // 10 get me the words stored at index w in the words array, assign it to a variable called words
word = word.replace(/[\-_!?.,;:\(\)]/g, ''); // 11 take the word, replace any punctutation with blank space
word = word.toLowerCase(); // 12 replace upper with lower
word = word.trim(); // 13 get rid of white space
if(word.length < 1) words.splice(w, 1); // 14 if empty word, remove it from the array
else words[w] = word; //
}
}