Context Free Grammars
This week we learned about context free grammars. This technique follows a structure similar to rules for Mad Libs where predefined words or segments are substituted.
To make this easier in JavaScript I made use of a library developed by Kate Compton called Tracery.
As my mental faculties were constrained while waiting for the results from the US Presidential Election, I found myself reduced to thinking about little more than refreshing news feeds, eating, and sleeping. Sticking to that theme I wrote a P5.js sketch that generates pizza descriptions.
The experience of working with Tracery was straightforward. I can certainly see how this method of text generation can be valuable when the project involves a small dataset with a highly structured output.
Feel free to take a look at my code below!
var story = { "start": "#pizza#", "pizza": "#dough# pizza with #sauce# sauce topped with #cheese#, #toppings#, #toppings# and #garnish# garnish.", "dough": ["whole wheat", "sour dough", "thin crust", "deep dish"], "sauce": ["red","vodka","pesto", "white", "BBQ"], "cheese": ["mozzarella", "goat cheese"], "toppings": ["mushrooms", "speck", "chives", "onions", "bacon", "green peppers", "figs", "sundried tomatoes"], "garnish": ["fresh basil", "arugula", "balsamic", "caper"] } var grammar; var result; function setup() { createCanvas(windowWidth, windowHeight); grammar = tracery.createGrammar(story); result = grammar.flatten("#start#"); console.log(result); } function draw() { background(255); textSize(40); fill(0); text(result, 0, 0, windowWidth, windowHeight); }