Code Poetry Slam

So, yeah, a freakin ‘HUGO nominee recommeded ME for ArmadilloCon – yeah, good thing I accepted that invitation.

accepted that
thing I accepted dees
good thing I rodeheaver
yeah, good thing err(1)
– yeah, good wang
ArmadilloCon – yeah, king
ArmadilloCon –
for ArmadilloCon
ME for
nominee recommeded ME ville
‘HUGO nominee recommeded leh
‘HUGO nominee
a freakin ‘HUGO emmerson
a freakin
yeah so
dead men 's dust
vulgar salutation
long lewd lither loiterer
moral elevation ensues
float of a fishing-line
conical tops of mountains
barrel of small arms.
barrel of the wheel.
barrier of ice. ice
olive oil. _yellow basilicon_
battening the hatches. securing
battens of the hatches.
beam of the anchor. synonymous
land 's end bore
before or abaft the beam.
bend on the tack.
berthing of the head.
beset in ice. surrounded

Cruelty.
Cruelty bien.
cruelty, and murder. And what
crush cloth and bones as the jaws of the hyæna crush
crushing out
cruel man is ever courageous. The popular theory, that inhumanity is
crucified at the foot of the
men
laden with spoils.
such
chivalry;
cruelty could add. Two of Rome’s
crushing out
cruelty and despotism that
carrots to the English governor, whose men were dying of scurvy, have
the men of Elliot’s garrison were suffering severely from scurvy,
carrots to the English governor, whose men were dying of scurvy, have
mentioned: a point which
men likely to be hostile commanders, would obviously
from the pagan laws of war which every page of the history of Christian
4,734 Christian inscriptions of the same period, only 27 were
crucifixion,
Christian; if he wants to run away with a girl, he becomes a Christian;
foreign
war,
cruelty
cruelty, however, taken in connection with
carrots to the English governor, whose men were dying of scurvy, have
Cruelty.
Cruelty bien.


TRUE PURPOSE, SOCIAL WORLD

A true purpose
A social world
A senior officer 's signature


A fore-and-aft sail, setting with a boom and gaff, in
  ships, synonymous with the spencers of brigs and schooners, and the
  spanker or driver of ships.

The true purpose
The social world
The senior officer 's signature

In size, as a ship's boat, smaller than
  the barge, and, like it, carvel-built.
true purpose
debutante wears
The senior officer 's signature

true one--arising from lateral pressure and the effect of sea when
  close-hauled.
purpose of firing signals,
  as the officer who commands her is particularly ordered to carry no
  cargo, ammunition, or implements of war.

Why are we perverse and ask for something
  else?
ask for something fu dining-room service.
and ask for fudging
perverse and ask reyer
we perverse and hunk
are we perverse duguid?
are we britz
are indri
Why moondreamer?
Why did I invite myself to this dinner
  yesterday?
Why are you so
  cross?
Why?
 
A true purpose
social world
A senior officer 's signature

The Phrases and Pronunciation

My final poetry-generating Python program is based on noun phrases, a markov chain, a recursive function, and a loose poetic structure. The vocabulary was sourced from three public domain source texts:

The Sailor’s Word Book, An Alphabetical Digest Of Nautical Terms, Including Some More Especially Military And Scientific, But Useful To Seamen; As Well As Archaisms Of Early Voyagers, Etc. By The Late Admiral W. H. Smyth (1867)

Emily Post’s Etiquette: In Society, In Business, In Politics And At Home (1922)

Fyodor Dostoevsky’s Notes From Underground (1864)

These texts make for interesting juxtapositions. For one thing, Post describes good manners for society, while Dostoevsky’s narrator is an outcast from society. The Sailor’s Word Book is a dictionary, but its definitions of sailor’s words are the most poetic of my three source texts—especially when taken out of context. The admiral who wrote this book in 1867 refers to ships as female (i.e. “her head”) and officers as male (i.e. “steersman”) which makes everything sound like an analogy. Likewise, Post and Dostoevsky use plenty of gendered pronouns. They also use proper nouns like “Mrs. Worldly” and “Simonov.” These texts refer to people—whether they are objects (such as a ship), individuals or society as a whole. As a result, when I feed these three texts into my program, they often seem to be talking about the same thing.

 

MARKOV CHAIN

My Markov object creates a dictionary of probabilities for transitioning from one word to the next, but I added some special features. Its “generate” function takes a starting word as an argument. If the starting word exists in the source text (Sailor’s Words + Dostoevsky), it will generate text based on its dictionary of probable transitions from that word to any other word.

  # Generate a text from the information in self.ngrams
  def generate(self, start_word):
    from random import choice

    # If the start_word is in the text, generate a markov chain
    if start_word in self.all_words:
      current = choice([item for item in self.ngrams if item[0] == start_word])
      output = list(current)

      for i in range(self.max):
        if current in self.ngrams:
          possible_next = self.ngrams[current]
          next = choice(possible_next)
          output.append(next)
          current = tuple(output[-self.n:])
        else:
          break

      output_str = self.concatenate(output)
      return output_str
    else:
      return ''

This allowed me to create a generate_question() function that starts a Markov chain with What When Where Why or How. The questions are part of my poetic structure.

def generate_question():
    next_line = underground.generate(random.choice(['What', 'How', 'Why', 'When', 'Where']))
    return next_line

I felt it was important to feed sentences into the Markov chain, rather than lines. So I made an array of Strings and used TextBlob to parse the sentences which I fed into my Markov chain like this:

underground = MarkovGenerator(5, 6)
utext = []
for line in open('../texts/notesfromunderground.txt', 'r'):
    utext.append(line + ' ')
utext_sentences = TextBlob(" ".join(utext).decode('ascii', errors='replace')).sentences
for sentence in utext_sentences:
    underground.feed(sentence)

 

NOUN PHRASES

I used TextBlob to generate a list of noun phrases from Etiquette and Sailor’s Words.

Noun phrases are a very interesting way to parse a text. In fact, I could have stopped there and made a poem out of noun phrases like these:

dead men 's dust
vulgar salutation
long lewd lither loiterer
moral elevation ensues
float of a fishing-line
conical tops of mountains

This is a series of noun phrases from Sailor’s Words:

barrel of small arms.
barrel of the wheel.
barrier of ice. ice
olive oil. _yellow basilicon_
battening the hatches. securing
battens of the hatches.
beam of the anchor. synonymous
land 's end bore
before or abaft the beam.
bend on the tack.
berthing of the head.
beset in ice. surrounded

This is a series of noun phrases from Etiquette:

politeness implies
gentlemanly character
social intercourse
whole detail
person shows respect
time implies
reciprocal respect
gentlemanly character
womanhood combined.

Originally, I created a Noun Phrase List object, but this was time consuming to generate every time, so I just saved them as a text file using the NPLC object I created in my nplc module.

post = NPLC('../texts/sailorswordbook.txt', 4)
npz = open("sailor_nouns.txt", "wb")
for i in post.noun_phrases:
    npz.write(i.encode('utf-8', errors='replace').strip() + '\n')
npz.close()

 

I thought that these noun phrases could give my poems some rhythmic structure, especially if repeated. I sorted them by length:

# generate three noun phrases
nOne = random.choice(postnouns).strip()
nTwo = random.choice(postnouns).strip()
nThree = random.choice(sailornouns).strip()

print "nOne: " + nOne + ', nTwo: ' + nTwo + ', and Three: ' + nThree

# sort the noun phrases by length
if len(nOne) > len(nTwo):
    nX = nOne
    nOne = nTwo
    nTwo = nX

if len(nTwo) > len(nThree):
    nX = nTwo
    nTwo = nThree
    nThree = nX

I also created a function to prepend an appropriate preposition to a noun phrase

# This function tries to figures out what preposition to put before
# a Noun Phrase based on whether it is plural and based on its starting letter.
def preposition(line):
    first_letter = line[0]
    for word in line.split():
        tb = TextBlob(word)
        for w, t in tb.tags:
            if t == 'NN':
                b = Word(word)
                if word == str(b.singularize()):
                    # print word + " is probably singular like " + b.singularize()
                    if not_a_vowel(first_letter):
                        return random.choice(['The ', 'A ', '']) + line
                    else:
                        return random.choice(['The ', 'An ', '']) + line
                elif word == str(b.pluralize()):
                    return random.choice(['The ', 'Some ', 'Many ', 'Of ', 'For all of the ', '']) + line
    ## if it gets to this point, we dont know if it is plural, so just figure out if 'a' or 'an'
    if not_a_vowel(first_letter):
        return random.choice(['A ', 'The ', '']) + line
    else:
        return random.choice(['An ', 'The ', '']) + line

That uses my not_a_vowel function:

def not_a_vowel(letter):
    for char_to_check in ["a", "e", "i", "o", "u"]:
        if letter == char_to_check:
            return False
    return True

Which I originally wrote in order to approximate the number of syllables in a gibberish word. I did not deal with the complexities of “Y.”

RECURSION

The noun phrases, questions and markov chains help give my poems rhythm and space. I pick up the pace with a recursive function called redo_line:

## This function takes a word an changes it to another word that rhymes or has the same # of syllables.
## Sometimes, it also appends a random nounphrase
## rhyming_word is in a separate file, the rhymebot module.
def changeWord(word):
    new_word = ''
    #   nounize(word)
    if word.upper() in all_words:
        new_word = rhyming_word(word, 1)
        if new_word:
            return new_word.lower()
        else:
            new_word = str(random.choice(syl_lookup[syl_bible[word.upper()]]))
            # new_line = new_line + " " + new_word.lower()
    else:
        gibSyl = gib_syls(word)
        new_word = str(random.choice(syl_lookup[gibSyl]))
        # new_line = new_line + " " +
    return new_word.lower() + " " + random.choice(postnouns + sailornouns).strip()


## This function iterates recursively through a line, three words at a time.
## It transforms the last word each time by calling the changeWord function.
def redo_line(line):
    words = line.split()
    if len(words) > 0:
        w = words.pop()
        y = changeWord(w)
        if len(words) > 3:  #crucial variable
            new_line = ' '.join(words)
            try:
                print ' '.join(new_line.split()[-3:]) + " " +y.lower()
            except:
                print ' '.join(new_line.split()[-2:])
            redo_line(new_line)
        if len(words) == 3:
            new_line = ' '.join(words)
            try:
                print ' '.join(new_line.split()[-2:]) + " " +y.lower()
            except:
                print ' '.join(new_line.split()[-1:])
            redo_line(new_line)
        if len(words) == 2:
            new_line = ' '.join(words)
            try:
                print ' '.join(new_line.split()[-1:]) + " " +y.lower()
            except:
                print ' '.join(new_line.split()[-1:])
            redo_line(new_line)
        if len(words) == 1:
            print words[0] + ' ' + y
            print underground.generate(words[0])
            print underground.generate(words[0])
            print words[0] + w
            return

I originally developed this function for my midterm project. Here, I use it towards the end of the poem to iterate through the poem’s three noun phrases and the poem’s opening line.

You can view the full code on my github and the two sample poems from my reading are excerpted below:

PHRASES & PRONUNCIATION

  ABBLAST.
young people
How is enjoyment in this to be
  explained?
 
An elderly guest
phrases and pronunciation
phrases and pronunciation
pronunciation phrases and gillon
and pronunciation phrases remodeled
phrases and pronunciation Townsend's
guest phrases and catamaran
guest phrases liquefied
guest amdahl's
elderly simonette
elderly
 
As far as
  my personal opinion is concerned, to care only for well-being seems to
  me positively ill-bred.
time by his sighed perfect little house
that time by kerins
at that time bly
attention at that duhaime
my attention at cooperate
distracted my attention introduced
Apollon distracted my ritalin
that Apollon distractedly
fact, that Apollon hausfeld
in fact, that 'm chapter xxv
thing, in fact, inhibit
good thing, in aux telegraph pole
a good thing, doron
was a good er luncheon menu=
was a wood
was japonica
It chides
It would be to the interests of humanity and courtesy were it made
  indispensable.
It was first devised for
  the service of mortars, and named after the inventor, Gomer, in the late
  wars.
It
The elderly guest
The phrases and pronunciation
The phrases and pronunciation

 

TRUE PURPOSE, SOCIAL WORLD

A true purpose
A social world
A senior officer 's signature


A fore-and-aft sail, setting with a boom and gaff, in
  ships, synonymous with the spencers of brigs and schooners, and the
  spanker or driver of ships.

The true purpose
The social world
The senior officer 's signature

In size, as a ship's boat, smaller than
  the barge, and, like it, carvel-built.
true purpose
d��butante wears
The senior officer 's signature

true one--arising from lateral pressure and the effect of sea when
  close-hauled.
purpose of firing signals,
  as the officer who commands her is particularly ordered to carry no
  cargo, ammunition, or implements of war.

Why are we perverse and ask for something
  else?
ask for something fu dining-room service.
and ask for fudging
perverse and ask reyer
we perverse and hunk
are we perverse duguid?
are we britz
are indri
Why moondreamer?
Why did I invite myself to this dinner
  yesterday?
Why are you so
  cross?
Why?
 
A true purpose
social world
A senior officer 's signature

Phrases and Pronunciation

ABBLAST.
young people
How is enjoyment in this to be
explained?

An elderly guest
phrases and pronunciation
phrases and pronunciation
pronunciation phrases and gillon
and pronunciation phrases remodeled
phrases and pronunciation Townsend’s
guest phrases and catamaran
guest phrases liquefied
guest amdahl’s
elderly simonette
elderly

As far as
my personal opinion is concerned, to care only for well-being seems to
me positively ill-bred.
time by his sighed perfect little house
that time by kerins
at that time bly
attention at that duhaime
my attention at cooperate
distracted my attention introduced
Apollon distracted my ritalin
that Apollon distractedly
fact, that Apollon hausfeld
in fact, that ‘m chapter xxv
thing, in fact, inhibit
good thing, in aux telegraph pole
a good thing, doron
was a good er luncheon menu=
was a wood
was japonica

It chides
It would be to the interests of humanity and courtesy were it made
indispensable.
It was first devised for
the service of mortars, and named after the inventor, Gomer, in the late
wars.
It

The elderly guest
The phrases and pronunciation
The phrases and pronunciation

True Purpose, Social World, Senior Officer’s Signature

A true purpose
A social world
A senior officer ‘s signature

A fore-and-aft sail, setting with a boom and gaff, in
ships, synonymous with the spencers of brigs and schooners, and the
spanker or driver of ships.

The true purpose
The social world
The senior officer ‘s signature

In size, as a ship’s boat, smaller than
the barge, and, like it, carvel-built.
true purpose
debutante wears
The senior officer ‘s signature

true one–arising from lateral pressure and the effect of sea when
close-hauled.
purpose of firing signals,
as the officer who commands her is particularly ordered to carry no
cargo, ammunition, or implements of war.

Why are we perverse and ask for something
else?
ask for something fu dining-room service.
and ask for fudging
perverse and ask reyer
we perverse and hunk
are we perverse duguid?
are we britz
are indri
Why moondreamer?
Why did I invite myself to this dinner
yesterday?
Why are you so
cross?
Why?

A true purpose
social world
A senior officer ‘s signature

Recursive One Word Generative Poetry

Input any word. My program will turn it into another word that has the same number of phonemes. Then it will find an example of that word in use via the Wordnik API. Then it will recursively run through four bit chunks, starting from the end, modifying the last word in each line using the CMU phoneme dictionary. I got a bit carried away parsing the CMU pronouncing dictionary and this includes some functions that I didn’t wind up using, but would like to use in the future.

Children

So, yeah, a freakin ‘HUGO nominee recommeded ME for ArmadilloCon – yeah, good thing I accepted that invitation.
accepted that
thing I accepted dees
good thing I rodeheaver
yeah, good thing err(1)
– yeah, good wang
ArmadilloCon – yeah, king
ArmadilloCon –
for ArmadilloCon
ME for
nominee recommeded ME ville
‘HUGO nominee recommeded leh
‘HUGO nominee
a freakin ‘HUGO emmerson
a freakin
yeah so

hello

JOSEPH BAST, president of the Heartland Institute, whose unshaven face beards a very similar resemblance to the president of another country on this Earth — no, not the Czech Republic, but go head and guess!
head and
but go head zoch
Republic, but go peart
Czech Republic, but uwe
the Czech Republic, bohr
the Czech
no, not the yeung
— no, not so
Earth — no, pitt
Earth —
this Earth
country on this pay
another country on fors
of another country o.s
president of another smolen
the president of clemo
to the president of(1)
resemblance to the tumbleson
similar resemblance to h
very similar resemblance auth
a very similar creditworthy
beards a very zimpfer
face beards a knapke
unshaven face beards owe
whose unshaven face wesat
Institute, whose unshaven prow
Heartland Institute, whose sicilia
the Heartland Institute, luau
the Heartland
president of the mastodon
BAST, president of thao

At one point, I was including the fill line rather than the last four words:

midterm2.py
172-29-18-92:midterm jasonsigal$ python midterm2.py c458d5c93b0a018f000070bc23407600a2332144b9ac156e1
hello
PEIFER: My wife is the one who always wanted to go to Africa.
PEIFER: My wife is the one who always wanted to go to
PEIFER: My wife is the one who always wanted to go jha
PEIFER: My wife is the one who always wanted to jha
PEIFER: My wife is the one who always wanted our
PEIFER: My wife is the one who always yearlings
PEIFER: My wife is the one who schiltz
PEIFER: My wife is the one o.s’
PEIFER: My wife is the noon
PEIFER: My wife is hour(1)
PEIFER: My wife u.
PEIFER: My fifth(1)
PEIFER: herb

Continue reading

Letters From the Book of Etiquette

Python helped me to find the poetry within Emily Post’s Etiquette.

First, I ran a script that output a dictionary of words that appear more than 10 times, sorted by how often they appear. I scanned this dictionary and grouped my favorites into lists. I also created a list of lines leading up to ‘rests’ (commas and colons) and periods so that I could control the pacing amidst the randomness. Here is a randomly generated poem from the ‘rest’ list:

greet each other by waving their arms aloft,
the names acquired in her own social life.
and ardor:
Guests.
silver buffer,
salad which is cold,
that of their intimates,
If because of illness or absence,
class),
part in it, as it is supposed to be bad luck.

I decided to create a series of mini-poems that follow a certain structure. Each starts with the word Dear…

Dear Miss Strange:
order.
The best type of young men pay few,
friends nearby.
Old-fashioned sentiment prefers that it be white,
leg in his finger
written in full,
change that has taken place. In case of a very small funeral
weather,
smaller letters than the name.

Dear Mrs. Town:
look for untruth. To
save your efforts for the next fancy dress ball,
people of position are people of position the
ground floor,
is put the salad fork, the meat
writes in something else. If she has any orders or criticisms to make,
advance–whether here or in Europe
is the first requisite in table-manners,
corridor.

Dear Sir:
order.
because of their distinction and smartness,
people strictly observe this rule.
ARE HONEST,
manner obtrudes upon her, he lifts his hat
first two go up the chancel steps and stand at the top; one on the right,
London is the home
manners,
half way when already Mlle.

Dear Mrs. Kindhart (or Martha):
always introduce:
or washing in a little tin basin,
people who deport themselves abominably, who
a crate to cross the ocean,
However, to go back to table setting: A cloth
of gold brocade,
In the world of smart society–in America
In going to inquire for sick people,
5.

Dear Mrs. Brown:
give most of their time to their grown and growing
melting-pot,
servant’s right hand;
those at the bridge tables. They all say,
(instead of the fruit knife and fork
Other pet offenses are drumming on the table with one’s fingers,
five o’clock,” “Mrs. Jones will be home
trunks,
everywhere.

Poetry with Python 001: Cruelty

In Reading & Writing Electronic Text, we’ll be writing Python scripts that take text as input, and output poetry.

I searched Project Gutenberg for ‘Manners’ which led me to two public domain books. First, I found Manners and Rules of Good Society (Or Solecisms To Be Avoided), by a Member of the Aristocracy, 1916. I also found Military Manners and Customs by James Anson Farrer, 1885.

I began trying to figure out what words appear a lot, by only printing lines that contained certain words or character strings using line.find(“____”);

Decontextualizing “cruelty” from Military Manners and Customs produced interesting results: cruelty4.txt

But I wanted to incorporate other words, so I created sets for interesting characters that kept popping up like “scurvy” “men” or “man” and “Chr.” Then I wanted to randomize their order, and realized I had to use list() instead of set() and imported random. I further subdivided the Cruelty set into long and short lines to giv control over the rhythm/pacing. I created an order for the random.choice()’s so that the poem, and I preset the first and last lines to  make it feel more like a composition.

Here’s the Python script: cruelty6_fin.py

Here’s the resulting poem: cruelty6.txt*

*The earlier version at the top is actually my favorite.