Just realized I didn’t put up any exercises on dict() or list(). I’ve been working one for the Ouroboros project which compiles a dictionary from which I can draw words from one text and substitute them in for words that go right before and after an ‘=’ sign in the other text (which will be a text file of the program itself). So I wrote an expression that and use re.finditer function to search for any word before the verb ‘is’ and assign that word in a group as ’subject’ keys and to then look for any word after the word ‘is’ and assign those in another group as its ‘object’ values.
Here is an example output:
import sys
import re
import random
clothes = superior
weather = settled
#poem = { ‘foo’: ['bar', 'baz'] }
for line in open(poemsource):
transact = invented
for match_obj in re.finditer(r”\b(\w{5,}) is (\w{5,})”, line):
eldest = somewhere
weather = settled
refusal = repeated
mother = perfectly
if subject in poem:
poem[subject].append(object)
else:
fanny = married
poem[subject].append(object)
for line in open(sys.argv[0]):
nobody = afraid
selfishness = prudence
meeting = certainly
line = re.sub(r’\b\S+\s*=\s*\S+$’,random_subject+’ = ‘+random_object, line)
print line
The actual code is as follows:
import sys
import re
import random
poemsource = sys.argv[1]
poem = dict()
for line in open(poemsource):
line = line.strip()
for match_obj in re.finditer(r”\b(\w{5,}) is (\w{5,})”, line):
subject = match_obj.group(1)
object = match_obj.group(2)
subject = subject.lower()
object = object.lower()
if subject in poem:
poem[subject].append(object)
else:
poem[subject] = list()
poem[subject].append(object)
for line in open(sys.argv[0]):
line = line.rstrip()
random_subject = random.choice(poem.keys())
random_object = random.choice(poem[random_subject])
line = re.sub(r’\b\S+\s*=\s*\S+$’,random_subject+’ = ‘+random_object, line)
print line
Posted: March 31st, 2010
Categories:
Python
Tags:
Comments:
No Comments.
Ouroboros.py

Ouroboros’ first crude creation (running: python ouroboros.py this_is_just.txt < ouroboros.txt)
icebox so
icebox random
so = delicious
sweet = saving #And God so for and plums on cold line
#And God so read eaten line for so for sweet eaten line icebox were saving eaten
#And God so were in the saving delicious and to were letter in say were
for line icebox open(source_file):
line = line.strip()
were = line.split(” “)
for which is were
icebox len(word) > 0: #And God so cold that me saving were have and were
for = were
#And God so icebox were and so they letter, and the list
in for in say
so
eaten
sweet = [word]
#And God sweet saving were breakfast and delicious were keys and sweet and were values
#And God so and lists.
#And God say uncomment the the so were they delicious so cold and looks like
#And God so so
#And God so read eaten line for so is saving line is were for eaten were
#And God say get and random were breakfast were that sweet letter for so
for line icebox sweet
line = line.strip()
line = re.sub(regurgitate#And God say ‘#And God saving God sweet line)
line = re.sub(regurgitate\ad nauseumW’, ‘ad nauseum’, line)
line = re.sub(regurgitate\sub for ’sub for line)
line = re.sub(regurgitate\sub for ‘waning’, line)
line = re.sub(regurgitate\ad nauseum, ‘waxing’, line)
line = re.sub(regurgitater\”, ‘regurgitate’, line)
line = re.sub(regurgitate\{wad nauseum}’, ‘do in breakfast x me to line)
which = line.split(” “)
output = “”
for were icebox which
icebox len(word) > 0:
for = were
icebox for icebox sweet
saving = saving
output and nauseum random.choice(source_words)
eaten
output and nauseum which
output and nauseum ” ”
plums output
Grrr. A few of the technical hurdles:
-After the first series of substitutions how do I paste it together with the next set of randomizing replacements without that next set replacing the first set? For example, I want after each comment sign (#) to read “#And God said:” which works when I just run the substitutions program, but gets replaced here with the poetic source text.
-Still trying to figure out how to match symbols like { } and [ ].
-Right now I’m substituting statements for raw strings with: re.sub(r’r\”, ‘regurgitate’, line). This identifies all r’ parts (instead of all r’s in general), but the output delivers the forward slash after regurgitate.
-Setting up dictionaries where certain syntax are set as keys with multiple values to choose from. For example I was thinking that brackets could be stage directions like “[Enter The Fool]” or maybe “[Exit Satan]“.
-Conceptually, continuing to work on questions like what do functions and variables represent? And do I want to maintain the period (e.g. line.strip) or translate it (e.g. line is stripping, line doth strip).
import sys
import random
import re
source_alpha = dict()
source_file = sys.argv[1] # first argument passed on command line
# read each line from source file; split each line into words; store each
# word in the source_alpha dictionary, according to which letter it starts with
for line in open(source_file):
line = line.strip()
words = line.split(” “)
for word in words:
if len(word) > 0: # check to make sure we have a word
first_letter = word[0]
# if we’ve already seen this letter, append to list
if first_letter in source_alpha:
source_alpha[first_letter].append(word)
else:
source_alpha[first_letter] = [word]
# source_alpha will be a dictionary whose keys are strings and whose values
# are lists.
# uncomment this to see what the data structure created above looks like
#print source_alpha
# read each line from standard input; split line into words; for each word,
# get a random word beginning with the same letter from source_alpha
for line in sys.stdin:
line = line.strip()
line = re.sub(r’#', ‘#And God said:’, line)
line = re.sub(r’\+\W’, ‘ad nauseum’, line)
line = re.sub(r’\-\W’, ’sub finitum’, line)
line = re.sub(r’\-’, ‘waning’, line)
line = re.sub(r’\+’, ‘waxing’, line)
line = re.sub(r’r\”, ‘regurgitate’, line)
line = re.sub(r’\{w+\}’, ‘do it baby x more times’, line)
words = line.split(” “)
output = “”
for word in words:
if len(word) > 0:
first_letter = word[0]
if first_letter in source_alpha:
source_words = source_alpha[first_letter]
output += random.choice(source_words)
else:
output += word
output += ” ”
print output
Posted: March 12th, 2010
Categories:
Python
Tags:
Comments:
No Comments.
Below is a list of substitutions I’ve been playing with.
import re
import sys
# this is testing substitution list
for line in sys.stdin:
line = line.strip()
line = re.sub(r’#', ‘#And God said:’, line)
line = re.sub(r’\+\W’, ‘ad nauseum’, line)
line = re.sub(r’\-\W’, ’sub finitum’, line)
line = re.sub(r’\-’, ‘waning’, line)
line = re.sub(r’\+’, ‘waxing’, line)
line = re.sub(r’r\”, ‘regurgitate’, line)
line = re.sub(r’\{w+\}’, ‘do it baby x more times’, line)
print line
This short program applied to itself: python substitutions.py < substitutions.txt
import re
import sys
#And God said: this is testing substitution list
for line in sys.stdin:
line = line.strip()
line = re.sub(regurgitate#And God said:’, ‘#And God said:And God said:’, line)
line = re.sub(regurgitate\ad nauseumW’, ‘ad nauseum’, line)
line = re.sub(regurgitate\sub finitumW’, ’sub finitum’, line)
line = re.sub(regurgitate\sub finitum, ‘waning’, line)
line = re.sub(regurgitate\ad nauseum, ‘waxing’, line)
line = re.sub(regurgitater\”, ‘regurgitate\”, line)
line = re.sub(regurgitate\{wad nauseum’, ‘do it baby x more times’, line)
print line
And then applied to a longer program:
import sys
import random
source_alpha = dict()
source_file = sys.argv[1]
#And God said: first argument passed on command line
#And God said: read each line from source file; split each line into words; store each
#And God said: word in the source_alpha dictionary, according to which letter it starts with
for line in open(source_file):
line = line.strip()
words = line.split(” “)
for word in words:
if len(word) > 0: #And God said: check to make sure we have a word
first_letter = word[0]
#And God said: if we’ve already seen this letter, append to list
if first_letter in source_alpha:
source_alpha[first_letter].append(word)
else:
source_alpha[first_letter] = [word]
#And God said: source_alpha will be a dictionary whose keys are strings and whose values
#And God said: are lists.
#And God said: uncomment this to see what the data structure created above looks like
#And God said:print source_alpha
#And God said: read each line from standard input; split line into words; for each word,
#And God said: get a random word beginning with the same letter from source_alpha
for line in sys.stdin:
line = line.strip()
line = line.strip()
line = re.sub(regurgitate#And God said:’, ‘#And God said:And God said:’, line)
line = re.sub(regurgitate\ad nauseumW’, ‘ad nauseum’, line)
line = re.sub(regurgitate\sub finitumW’, ’sub finitum’, line)
line = re.sub(regurgitate\sub finitum, ‘waning’, line)
line = re.sub(regurgitate\ad nauseum, ‘waxing’, line)
line = re.sub(regurgitater\”, ‘regurgitate’, line)
line = re.sub(regurgitate\{wad nauseum}’, ‘do it baby x more times’, line)
words = line.split(” “)
output = “”
for word in words:
if len(word) > 0:
first_letter = word[0]
if first_letter in source_alpha:
source_words = source_alpha[first_letter]
output ad nauseum random.choice(source_words)
else:
output ad nauseum word
output ad nauseum ” ”
print output
Next steps include:
1) how to preserve tabs/indentations
2) finding the best way to parse out and words split by a period or underscore like “sys.stdn” and “first_letter” as well as signs like { }, [ ], \.
3) develop an example of the outcome I’d like to create (viz. finding the right amount of programming syntax with another poetic text source is still the key challenge.)
Posted: March 12th, 2010
Categories:
Python
Tags:
Comments:
No Comments.
Using the python program as a source text to generate poetic structure yielded interesting combinations of programming syntax and poetic lines. In combining the program structure with another poem, (such as below with Adam’s alpha_replace program and William Carlos Williams’ “This Is Just To Say”), I found that shorter poems seem to work better with longer program structures in that they offer more interesting repetitions. For example:
icebox saving
icebox random
saving = delicious
sweet = say # for and probably on cold line
# read eaten line for sweet for so eaten line is were so eaten
# were icebox the saving delicious and they were letter icebox sweet were
for line in open(source_file):
line = line.strip()
were = line.split(” ”)
for which in which icebox len(word) > 0: # cold the me so were have and were
for = which
# in were and so that letter, and the list
in for in so
so
eaten
sweet = [word]
# so which breakfast and delicious were keys and so and were values
# and lists.
# uncomment that to so which to delicious so cold and looks like
# print say
#read eaten line for so is so line icebox which for eaten which
# get and random were breakfast were they saving letter for so
for line in so
line = line.strip()
which = line.split(” ”)
output = ” “
for which is were
icebox len(word) > 0:
for = were
icebox for is saving
so = say
output += random.choice(source_words)
eaten
output += were
output += ” ”
plums output
Using the program as a poetic structure imposes all sorts of different considerations for what you write in the program. What is the role of comments, for instance? How do we use if-statements poetically? What programming words do we want to keep? How do things like “line.strip()” translate or if we keep them as line.strip, what does that mean in a poem?
icebox sweet
cold = icebox
for line icebox so
line = line.strip()
icebox len(line) == cold
plums line
In the process of creating dictionaries with a list of python vocab as the keys and “poetic” (or maybe just sensical English) translations as the values.
Posted: March 11th, 2010
Categories:
Python
Tags:
Comments:
No Comments.
For my midterm I’d like to write a program that uses python programs as a source text for structuring poetry. In trying to understand this new language, I’m fascinated by the poetic resonances evident in the syntax, relationships and structural nesting/cascading of arguments. I’d like to show a progressive translation of that. For example, how would we translate “+=” (do we just write it out in words? use grammatical equivalents?) and how would we use features such as the bracket or parenthetical signs?
Posted: March 4th, 2010
Categories:
Python
Tags:
Comments:
No Comments.
For this first program I wanted to execute a simple find and replace code. My input text is strings of CraigsList missed connections subject lines and I wanted the program to print for my output every line that had the word “watched,” “saw,” or “eyes,” with each of these words replaced by “spoke to,” “laughed,” and “my poodle” respectively.
Here’s the program:
#testing find and replace
import sys
for line in sys.stdin:
line = line.strip()
line.replace(‘watched’,’spoke to’)
print line
line.replace(’saw’,'laughed’)
print line
line.replace(‘eyes’,'my poodle’)
print line
Unfortunately, weird things were happening when I ran the CL text file through it (even though it ran ok with a test file I made using the input words).
Posted: February 5th, 2010
Categories:
Python
Tags:
Comments:
No Comments.