Markov midterm and slam poetry gettysburg address

This week I continued to work on my midterm, adding a Markov chain component.  This generated new texts that were based on the alliteration/assonance machine.  It made the machine more interesting in part because it made the word choices slightly less obvious and generated new texts.  I used this method with the Devil Tree text.

I was also interested in making a slam poetry generator, because my journalism professor commented in class today that Norman Mailer’s style in Armies of the Night was like a slam poetry version of the Gettysburg address.  I wanted to see what that would sound like.  The algorithm is not very sophisticated, it basically just places line breaks every 1-5 words with some randomness, but its actually kind of effective.  This also works really well with the markov-alliteration machine because slam poetry uses alliteration kind of excessively.

Here are results of the alliteration-markov chain:

Then some slam poetry results, with both the gettysburg address and the devil tree, run through the markov alliteration machine:


# hw-3 markov chain with a-machine

import sys
args = sys.argv
import re
from markov import MarkovGenerator

# take in a file
txt_file = args[1]

# read file
txt = open(txt_file, 'r')
txt_lines = txt.readlines()

# return only words in a sequence where each vowel is
#repeated twice "good bet eat apple eat a"
# why?  i don't know
key = ""
tempKey = ""
firstWord = 0
keySet = 0
tempKeySet = 0
wordSet = 0
notWord = 0

thePoem = ""

# break through lines
for line in txt_lines:

	#split words
	words = line.split(" ")
	#go through words
	#print words
	for word in words:
		#go through letters
		tempKeySet = keySet
		for letter in word:
			if"[nplfv]", letter.lower()):
				if notWord == 0:
					if keySet == 0:
					# find the first vowel
						tempKey = letter.lower()
						keySet = 1
						wordSet = 1
						if letter.lower() == tempKey:
							keySet = 0
							wordSet = 1
							wordSet = 0
							notWord = 1
							tempKey = key
							keySet = tempKeySet							

				#print "*"+key, keySet

		#	else:
		#		print letter

		if wordSet == 1 and notWord == 0:
			#print word
			key = tempKey

			#print key
			wordSet = 0
			thePoem += " " + word
			if firstWord == 0:
				firstWord = 1
			notWord = 0
			wordSet = 0
			if firstWord == 0:
				keySet = 0
				firstWord = 1

#print thePoem
#that was the midterm
#here's some markov

generator = MarkovGenerator(n=1, max=100)

line = thePoem.strip()

for i in range(1):
    print generator.generate()
#this turns anything into slam poetry

class Slam(object):

	def __init__(self):
		self.counter = 0
		self.poem = " "

	def slam(self, txt_lines):

		import random

		words = line.split(" ")
		for word in words:
			self.counter += 1
			#print word
			self.poem += word + " "
			if self.counter is random.randrange(1,4) or self.counter is 5:
				#print self.poem
				print "n"
				self.poem += "n"
				self.counter = 0
		print self.poem

	def printPoem(self):
		print self.poem
		print "poop"

if __name__ == '__main__':

	import sys

	slammer = Slam()
	for line in sys.stdin:
		line = line.strip()

Author: owen ribbit


1 thought on “Markov midterm and slam poetry gettysburg address”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s