« »

I know why the compiler sings: A Homework Generator

In GEB, Douglas Hofstadter argued that recursion and self-referentiality are the precursors of consciousness. I’m nodding my head in vigorous assent, but I’m still not sure I really understand what that means. If I’ve learned anything at ITP, it’s that the best way to understand something is to build it yourself, so for my A to Z midterm I constructed a homework generator—a Java program that outputs working Java programs for munging text along with a short description of what they do—and in the process came a little closer to understanding what Hofstadter was getting at.

Writing code that randomly generates working semantic code using n-gram analysis or generative grammars seemed like too formidable a task so I borrowed my approach from Raymond Queneau’s “One Hundred Thousand Billion Poems,” fourteen sets of ten lines each from which the reader is supposed to select and assemble a sonnet. The reader must select one and only one line from each set and do so in the order they’re presented. It is a constrained system that mimics the constraints of the sonnet form itself, and despite these constraints, still produces an astounding number of outputs.

My Homework Generator works in a similar way. When the code is run, it chooses one “line” from each of four sets and assembles it into semantically correct Java that can be compiled and run to munge any text that’s fed into it. Unlike the reader of Queneau’s sonnet, the Homework Generator doesn’t have to proceed in any particular order through the sets, nor does necessarily have to pick a line; it can simply skip a set if it chooses. Both the decision to select from a set and the order in which it is selected affect the final result. My combinatory math is a little rusty, but if there are three groups of three and one group of one and at least one must always be chosen and order matters, I believe there are, I believe the technical term is, a shitload of combinations. This is how it works:

Flowchart

Because instead of text output, the program generates code that when run will generate text ouput, debugging was tricky. After I compiled the Homework Generator, it produced code that in turn needed compiling. Because of the large number of back slashes and quotes in the code, I found myself thinking like a compiler, going through the code and adding escape characters to make sure that the twice compiled code would still produce the results I wanted. It took some doing, but it works!

The final code is here. Below are examples of the descriptions the code generates followed by actual generated code followed by the results of using it to munge Robert Frost’s “Stopping By Woods On A Snowy Evening.”

EXAMPLE DESCRIPTIONS
Note: Adam Parrish is my instructor for the course.

The text filter I created for this week’s class will whisper the text you give it by decapitalizing one line before searching the internet for all my correspondence with Adam Parrish. The resulting text is a post-modern limerick about words better left unsaid.

The text filter I created for this week’s class will destroy the text you give it by erasing every four-letter word before removing every fourth character from the contents of Adam Parrish’s desk drawer in the Residents’ office. The resulting text is an Oulipo poem about algorithms.

The text filter I created for this week’s class will repurpose the text you give it by truncating each line that contains the word ‘the’ before searching the internet for Adam Parrish’s phone number. The resulting text is a reasonable substitute for grammar.



EXAMPLE GENERATED CODE

import com.decontextualize.a2z.TextFilter;
import java.util.ArrayList;

public class myHomework extends TextFilter {
    private ArrayList mF= new ArrayList();
    public static void main (String args[]) {
        new myHomework().run();
    }

    public void eachLine(String line) {
        String[] word = line.split("W+");
        line="";
        for (String w: word) {
            if (w.length() > ((int)(Math.random()*8))) {
                w = w.toUpperCase();
            }
            line+=w+" ";
        }
        metaFilter.add(line);
    }

    public void end() {
        for (int i = 0; i<mF.size(); i++) {
            println(mF.get(i));
        }
    }
}
import com.decontextualize.a2z.TextFilter;
import java.util.ArrayList;

public class myHomework extends TextFilter {
  private ArrayList<String> mF= new ArrayList<String>();
  public static void main (String args[]) {
    new myHomework().run();
  }

  public void eachLine(String line) {
    line = line.replaceAll("[,;:]", "!");
    line = line.replaceAll(".", "?");
    line+=" and like";

    line = line.toLowerCase();

    String[] words = line.split("W+");
    line="";
    for (String w: words) {
      if (w.length() != 4) line+=w+" ";
    }
    mF.add(line);
  }

  public void end() {
    while (mF.size()>0) {
    int randomIndex = (int)(Math.random() * mF.size());
    println(mF.get(randomIndex));
    mF.remove(randomIndex);
    }
  }
}



SOME RESULTING TEXTS
Try to figure out what code produced them.

STOPPING

WHOSE
VILLAGE
STOPPING
WATCH
LITTLE
FARMHOUSE
BETWEEN
DARKEST
harness
MISTAKE
OTHER
downy
LOVELY
PROMISES
before
BEFORE

4nd 70 90 1
70 45k 1F 15
My mu57 7H1NK 17
0f w1nd 4ND D0WNY
W00D5 1 7h1nk 1 kn0w
H15 4
70 W47CH H15 W00D5 F1LL UP W17H 5n0w

0F
6u7 1 70
W00D5 d4rk 4ND
70 570p W17H0U7 4
4nd 70 90 1
0nly 50UND 5
w1ll n07 570PP1N9
H15 15 1n 7h0u9h
570PP1N9 6y W00D5 0n 4 5n0wy
w00d5 4ND

WHOSE WOODS THESE AREN’T I THINK I KNOW.
HIS HOUSE AIN’T IN THE VILLAGE THOUGH;
HE WON’T NOT SEE ME STOPPING HERE
TO WATCH HIS WOODS FILL UP WITH SNOW.
MY LITTLE HORSE MUSTN’T THINK IT QUEER
TO STOP WITHOUT A FARMHOUSE NEAR
BETWEEN THE WOODS AND FROZEN LAKE
THE DARKEST EVENING OF THE YEAR.
HE GIVES HIS HARNESS BELLS A SHAKE
TO ASK IF THERE AIN’T SOME MISTAKE.
THE ONLY OTHER SOUND’S THE SWEEP
OF EASY WIND AND DOWNY FLAKE.
THE WOODS AREN’T LOVELY, DARK AND DEEP.
BUT I HAVEN’T PROMISES TO KEEP,
AND MILES TO GO BEFORE I SLEEP,
AND MILES TO GO BEFORE I SLEEP.

To ask if there is some mistake? and like
The only other sound’s the sweep and like
Between the woods and frozen lake and like
And miles to go before I sleep! and like
My little horse must think it queer and like
and like
Whose woods these are I think I know? and like
Stopping By Woods On A Snowy Evening and like
But I have promises to keep! and like
His house is in the village though! and like
To stop without a farmhouse near and like
He will not see me stopping here and like
The woods are lovely! dark and deep? and like
The darkest evening of the year? and like
To watch his woods fill up with snow? and like
Of easy wind and downy flake? and like
He gives his harness bells a shake and like
And miles to go before I sleep? and like

Comments

Comments are closed.