xxxxxxxxxx
var thefont, thetext;
var thewords;
var thechain = {}; // object literal
var voice;
var theword = 'the';
function preload()
{
thefont = loadFont('ChristmasCraftFONTDEMO.otf');
thetext = loadStrings('midsummer.txt');
}
function setup() {
voice = new p5.Speech();
voice.interrupt = true;
createCanvas(windowWidth, windowHeight);
background(100);
textSize(80);
//textFont(thefont);
textAlign(CENTER);
// do some regex stuff:
let thebigstring = ''; // start with a blank nothing
for(let i = 0;i<thetext.length;i++)
{
thebigstring+=thetext[i]+' ';
}
thebigstring = thebigstring.replace(/[^a-zA-Z0-9’ ]/g, ' ');
thebigstring = thebigstring.replace(/ ’/g, ' '); // kill apostrophes at beginning of words
thebigstring = thebigstring.replace(/’ /g, ' '); // kill apostrophes at end of words
thebigstring = thebigstring.toLowerCase();
thebigstring = thebigstring.replace(/ +/g, ' ');
thebigstring = thebigstring.trim(); // kill any dangling crud
thewords = thebigstring.split(' ');
console.log(thewords);
for(let i = 0;i<thewords.length;i++)
{
let currentword = thewords[i];
let nextword = thewords[(i+1)%thewords.length];
if(currentword in thechain)
{
thechain[currentword].push(nextword);
}
else
{
thechain[currentword] = [];
thechain[currentword].push(nextword);
}
}
frameRate(1);
}
function draw() {
background(255);
fill(255, 0, 0);
stroke(0, 255, 0);
text(theword, width/2, height/2);
voice.speak(theword);
theword = random(thechain[theword]);
}