Brandon Roots
  • INTERACTIVE
  • FILM
  • ABOUT
  • CONTACT
November 9, 2020

Context Free Grammars

broots ITP, Programming A to Z

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);
}
Pixels Project: All Hail The Dodeca Project 2: Dodeca – Part 3

Related Posts

Fractal Plant – Foiled by  Registers

Homemade Hardware, ITP, Solar Plant

Fractal Plant – Foiled by Registers

Since receiving the PCBs and successfully soldering the board together I have been trying to rewrite code for the I2C port expander. This has been immensely difficult! The Inkplate Arduino Library makes considerable use of an “Mcp” class, which is written to work with the MCP23017 GPIO expander IC. These chips are quite difficult to […]

“Handling” Playtest Week

Handling, ITP

“Handling” Playtest Week

Last week we attended “Playtest Thursday” on the second floor of 370 Jay St with our games. I came away from the experience with some very specific feedback. Seeing a number of people play the game showed me things I didn’t anticipate. Some folks approached the cabinet and immediately treated it as a touch screen. […]

Fractal Plant – Beta Build

Homemade Hardware, ITP, Solar Plant

Fractal Plant – Beta Build

The boards arrived! Amazingly within an hour of one another. Based on the experience I think that JLCPCB is a better value. With shipping OSHPark was $55.50 for 3 boards. JLCPCB was $26.36 for 10 boards. Aside from a higher cost OSHPark also left sharp bits of tabs around the edges of the boards which […]

Recent Posts

  • Fractal Plant – Foiled by  RegistersFractal Plant – Foiled by Registers
    May 9, 2022
  • “Handling” Playtest Week“Handling” Playtest Week
    May 5, 2022
  • Fractal Plant – Beta BuildFractal Plant – Beta Build
    April 24, 2022
Brandon Roots