|\ /|     scroll down for older posts.
= o.o =

Dienstag, 4. Juni 2013

poems in python - part II: variations on Basho's pond

I won't go into the details of the higher art of writing haiku, because I can't. A common misconception about haiku is the notion that every poem must have a structure of 5-7-5 syllables. Nope, there's more to it.

But let's disregard high poetry for now and focus on one little splash: Basho's pond poem can be translated into English as follows:

at the age old pond
a frog leaps into water
a deep resonance

thus, the grammatical structure of this particular haiku reads like this:

preposition article adjective noun
article noun verb preposition noun
article adjective noun




So, I'll just take that grammatical structure and let my python-script generate some randomly created haiku. The code is provided at the end of this post:

after a cold territory
no governor shoots in dirt
the low cherries



for one cuddly thing
no marble wrings inside space
that juicy fifth



for one relieved soap
one doctor forswears on pear
one faithful winter



after one fancy club
a man wets after mom
one brief kitten



in that curly underwear
this carpenter overhears for apple
a delightful vase



for that little sweater
that space rebinds inside work
the late pest



for that broad drum
a pocket swears throughout jail
that greasy pig



in that rainy name
that coast sits for chicken
no late hope



for one delightful mint
one jeans weaves inside toothpaste
the long hill


...etc


Code, based on yesterday's snippet:

import random
# Functions

# Open a plaintext file named 'filename' with items
# separated by newlines, put its contents into a list
# and give one random item as output.
def get_from_file(filename):
  input_file = open(filename)
  list_of_contents = [
    line.strip() for line in input_file.readlines()
  ]
  input_file.close()
  output = random.choice(list_of_contents) + " " # add space after word
  return output

def noun_sg():
  return get_from_file('./files/noun_sg.txt')

def prep():
  return get_from_file('./files/preposition.txt')

def art_sg():
  return get_from_file('./files/article_sg.txt') #TBD: a or an

def verb_sg():
  return get_from_file('./files/verb_sg.txt')

def adj():
  return get_from_file('./files/adjective.txt')

#    at the age old pond
#    a frog leaps into water
#    a deep resonance

def sentence_a():
  x =[prep()+art_sg()+adj()+noun_sg()]
  print random.choice(x)


def sentence_b():
  x =[art_sg()+noun_sg()+verb_sg()+prep()+noun_sg()]
  print random.choice(x)

def sentence_c():
  x =[art_sg()+adj()+noun_sg()]
  print random.choice(x)

# Main
sentence_a()
sentence_b()
sentence_c()

Keine Kommentare:

Kommentar veröffentlichen