Brandon Roots
  • FILM
  • INTERACTIVE
  • 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

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 […]

Fractal Plant – Beta Build

Homemade Hardware, ITP

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 […]

Fractal Plant – Boards Ordered

Homemade Hardware, ITP

Fractal Plant – Boards Ordered

Over the last week I rerouted the traces in my PCB design. Following feedback during class that the auto-router feature in Eagle isn’t very smart I did manage to make some cleaner routing manually. As part of the process I went through each component and made sure they were placed a closely as possible to […]

Recent Posts

  • Fractal Plant – Foiled by  RegistersFractal Plant – Foiled by Registers
    May 9, 2022
  • Fractal Plant – Beta BuildFractal Plant – Beta Build
    April 24, 2022
  • Fractal Plant – Boards OrderedFractal Plant – Boards Ordered
    April 18, 2022
Brandon Roots
Accessibility by WAH