Python ‘Found Poem’ Generator – Adding Haikus


The wind blows in gusts
little pieces here and there
the fools in the hall

– Shadow The Poet aka James Laurie

Now that I had the Found Poem Generator working and creating poems with the number of lines asked for by the user I thought it was time to move on to the Haiku Generator idea. This would be a similar process but uses the syllable count property of each line of text. I also have the word count stored as a property which I may use later to create poems with lines of equal word length.

The Haiku has a fixed format of three lines, the first is five syllables, the second line is seven syllables, then back to five syllables for the last line.

I already had the code that generated a poem by lines, so I thought I could re-use this piece of code with some small modifications. This is always my methodology when coding – can I re-use something I’ve already done? It’s much easier to copy and paste something used in a previous project or something written in the current project rather than having to write a new section from scratch. 


When re-using code from the current project my second question is – can I modify the existing code to re-use it ‘in-situ’ rather than copying and pasting it out to the new location. What I mean by this is – can I make it a generic function? For example, we might have a piece of code that adds 1 and 1 together:

result = 1 + 1

If I needed to added two numbers together elsewhere in my program, ie 2 and 2, I could copy and paste the code above and update it to the new requirement:

result = 2 + 2

Nice and simple and works just fine, but, for me, the most elegant and de-buggable code always avoids duplication. Let’s suppose this was a more complex calculation, maybe 10 or 20 or 100 lines long, and I had used it hundreds of times in the program. Then imagine I found a bug in my calculation. Now I have to trawl though dozens or hundreds of lines of code to find and correct the problem wherever I found it. The risk being that I miss some and have now created for myself one of those annoying bugs that only appears under seemingly random circumstances and becomes harder and harder to track down. So, in circumstances like this I would always try to create a function from the original code rather than copy and paste it verbatim:

define adding_together_function(input:number1, number2, output:result):
result = number1 + number 2

Now this new function can be used whenever two numbers need adding together –

the_answer = adding_together_function (1,1)

Creating The Haiku Code

So with this in mind, I firstly updated the program to take the ‘doing’ code for the poem command out of the main loop of the program and put it into its own function. Once this was done I took a copy and changed the code so that it would select lines based on syllable count rather than just maintain a line count:

def output_by_syllable(no_of_syllables):
    found_line = 0
    while found_line == 0:
        random_num = random.randint(1,no_of_lines)
        phrase_meta = ("p" +str(random_num) )
        for phrase in Phrase._registry:
            if str( == str(phrase_meta):
                if phrase.get_used() == False:
                    if phrase.get_syllablecount() == no_of_syllables:
                        found_line = 1

With an eye to adding other syllabic poem forms to the program in the future, I then created a Haiku function that uses the ‘output_by_syllable’ function to create the Haiku:

def output_haiku():
#line 1 - 5 syllables
#line 2 - 7 syllables
#line 3 - 5 syllables

Finally I added a new menu option for generating the Haiku:

    elif command == "haiku":

I’ve added the updated code to my Dropbox share for this new feature.

the fools in the hall
little pieces here and there
The wind blows in gusts

– Shadow The Poet aka James Laurie


See a run through of progress so far:



More posts in this series:

Python ‘Found Poem’ Generator – Part 1 – Introduction

–> This one –>Python ‘Found Poem’ Generator – Part 2 – Adding Haikus

Python ‘Found Poem’ Generator – Part 3 – The Next Level

Python ‘Found Poem’ Generator – Part 4 – Outputting Text To A Graphical Screen

Python ‘Found Poem’ Generator – Part 5 – User Interaction – Adding Buttons

Python ‘Found Poem’ Generator – Part 6 – Loading Data From A File

Python ‘Found Poem’ Generator – Part 7 – Counting Syllables and Words


Thank you again to all my followers and regular readers, and hello to you if you are new to my blog!

There’s an eclectic mix of posts on here, from writing and poetry to banjos and guitars, art, photography and computing, so feel free to dive in and have a look around,

New to this site? Click here to visit my About Me section.

Follow me @ponyfolk on Instagram for my multi-medium art and @shadowthepoet on Twitter

Want to introduce yourself, your art, your blog or you world and discover all that is new in the world? Click here for my ‘Join the Revolution’ page.

Go well!

6 thoughts on “Python ‘Found Poem’ Generator – Adding Haikus

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 )

Connecting to %s