xxxxxxxxxx
139
// This code comes from this coding train tutorial: https://www.youtube.com/watch?v=9r8CmofnbAQ
let dadTexts;
let order = 6;
let ngrams = {};
let beginnings = [];
let loadInterval = 2;
let checkpoint = 0;
let doorDash = {
"start": "#story#",
"person": ["Priyanka", "Shiffman", "Santa", "God"],
"order": ["sushi", "sandwich", "pizza", "tape", "Arduino", "screws"],
"restaurant": ["McDonalds", "the Deli", "Tom's Restaurant", "the bowling alley", "the park", "the dump"],
"dasher": ["Obama", "Elvis", "llama", "unicorn", "Kim Kardashian"],
"story": "Hey #person#, your #order# has been picked up from #restaurant# and #dasher# is on the way! :-)",
}
let grammar;
let texts = [];
let homescreen;
let dadImg;
let foodImg;
function preload() {
dadTexts = loadStrings("dad.txt");
// console.log(dadTexts);
// Load all the images
homescreen = loadImage("homescreen.jpg");
dadImg = loadImage("dadCrop.png");
foodImg = loadImage("foodCrop.png");
}
function setup() {
// noCanvas();
createCanvas(360, 640);
// background(255);
image(homescreen, 0, 0, width, height);
createNgrams();
}
function draw() {
if (millis() - checkpoint > loadInterval * 1000) {
// Draw the homescreen
image(homescreen, 0, 0, width, height);
// generate the incoming text
getText();
// Really can only show 4 texts at a time,
// so we can shorten the array!
if (texts.length == 10) {
texts.pop(); // Only need an array of 8, remove the last two elements
texts.pop(); // Pop twice
}
for (let i = 0; i < texts.length; i++) {
// Even elements are who the text is from
if (i % 2 == 0) {
if (texts[i] == "Papa") {
console.log("Got a text from dad!");
image(dadImg, 15, i*40 + 200, width-30, 75);
} else {
console.log("Got a text from food!");
image(foodImg, 15, i*40 + 200, width-30, 75);
}
// console.log(texts[i]);
} else { // Odd elements have the content of the text
fill(255); // Make the text white
text(texts[i], 100, 40*i + 185, 230);
}
}
// console.log(texts);
checkpoint = millis();
// reset the wait time for text
loadInterval = random([2, 4, 6, 10]);
}
}
function getText() {
let chance = random([0, 1]);
if (chance == 0) {
// createP('Papa');
markovIt();
} else {
// createP('FoodDash');
grammar = tracery.createGrammar(doorDash);
let doorDashGrammar = grammar.flatten("#start#");
// createP(doorDashGrammar);
texts.unshift("FoodDash", doorDashGrammar);
}
}
function markovIt() {
let currentGram = random(beginnings);
let result = currentGram;
for (let i = 0; i < 100; i++) {
let possibilities = ngrams[currentGram];
let next = random(possibilities);
result += next;
let len = result.length;
currentGram = result.substring(len - order, len);
}
texts.unshift("Papa", result);
// console.log(texts);
// createP(result);
}
function createNgrams() {
for (let j = 0; j < dadTexts.length; j++) {
let txt = dadTexts[j];
// Create a list of n-grams
for (let i = 0; i <= txt.length - order; i++) {
let gram = txt.substring(i, i + order);
if (i == 0) {
beginnings.push(gram);
}
// If this ngram DOESN'T already exist
if (!ngrams[gram]) {
ngrams[gram] = [];
}
ngrams[gram].push(txt.charAt(i + order));
}
}
}