User:Mirjam Dissel/Refrigerator
< User:Mirjam Dissel
Revision as of 11:33, 21 June 2011 by Mirjam Dissel (talk | contribs)
Markov chain haiku generator
haiku.txt
without the power
to even fall the dandelion
standing in winter
Red lights running down
The street and past the pine tree
Where has the moon gone?
Inconceivable.
You keep on using this word.
You misunderstand.
These words are crafty
They have stolen precious time
that you can't get back
Fresh cut hair itches
A reminder of my loss
I weep for the past
I know many words
but I may not speak them here
at grandfather's grave
Winter brings cold pain
These joints creak and crack and pop
When did I get old?
Appetite fulfilled
All wanted, nothing needed
A life alone paid
Looking at nature,
seeing struggle and splendour
a new thought was caught.
Five, seven and five.
This seventeen syllables
caught in a poëm.
Horizon, moving
Only when you do, constant
Ruler of the sky
The wind is ice cold
the ice is everywhere
I'm stuck in winter
Rhododendronbush:
Each bud saved her flower
for this sunny spring.
An old pond!
A frog jumps in-
The sound of water.
a beautiful weed
sprouting in the summer sun...
happy, but alone
Great Japan!
where a bird sings
the Lotus Sutra
it's 4:10 am
I can't think straight, and much less...
count syllables
bust out PDA...
what do I see on the screen?
broken LCD!
salty blue water
waving to the sandy shore
a roller coaster
A single grain of dust
at the mercy of the wind..
the life of a man
the old hand
swats a fly
already gone
Here so late at work.
Must not abandon Kevin.
Tired unity!
Short summer night.
A dewdrop
On the back of a hairy caterpillar.
do caterpillars
think butterflies beautiful?
find your own cocoon
Flowers bloom and die
Wind brings butterflies or snow
A stone won't notice
gamer's eyes light up
but, alas, he soon finds out
there's another stage
House of grass widow's-
only in the neighborhood
roosters cry the dawn
Worker bees can leave
Even drones can fly away
The Queen is their slave
Evening snow falling,
a pair of mandarin ducks
on an ancient lake.
corner spider
rest easy, my soot-broom
is idle
in a new bucket
the same water...
looks so fresh!
refrigerator.py
#homework: look at new lines. it's like previous words (do i have previous word or not? that is new line!) store it in database.
#also, think about second order markov chain! database for sequences of two words. use tuples
import random, os, time
# one class to parse Haikus and build a Markov DB
class HaikuParser(object):
def __init__(self, markov_db):
self.db = markov_db
def parse(self, filename):
#import textfile
f = open(filename)
prev_word = None
#make words of textblock
for line in f:
if len(line.strip()):
for word in line.split():
if prev_word:
if prev_word in self.db:
self.db[prev_word].append(word)
else:
self.db[prev_word] = [word]
else:
if "<BEGIN>" in self.db:
self.db["<BEGIN>"].append(word)
else:
self.db["<BEGIN>"] = [word]
prev_word = word
else:
prev_word = None
f.close()
#loop through each word and put in dictionary: word as index and next word as value.
#if word exists, append next word to existing index
class HaikuMaker(object):
def __init__(self, markov_db):
self.db = markov_db
def make(self):
out = ""
num_words = random.randrange(9,12)
# take a <BEGIN> word
word = random.choice(self.db["<BEGIN>"])
for i in range(num_words):
out = out + word + " "
if word in self.db:
word = random.choice(self.db[word])
else:
#break
key = random.choice(self.db.keys())
word = random.choice(self.db[key])
return out.strip()
#choose index in dictionary (starting point), take value and look for value in dictionary.
#do the same: take this new index and take the value and see if it exist in the dictionary.
#print print print
if __name__ == "__main__":
#create dictionary
d = {}
hp = HaikuParser(d)
hp.parse("haiku.txt")
# not needed anymore
# print(hm.make())
# output image black font on white
for i in range(20):
hm = HaikuMaker(d)
thenewHaiku = hm.make()
i += 1
conv = "convert -background white -fill black -font Times-Roman -pointsize 50 -size 600x caption:'%s' haiku%s.gif" % (thenewHaiku, i)
os.system(conv)
time.sleep(0.50)