Pony Songs – Bo

Bo is a rescue pony, rescued from a pharmaceutical test laboratory, and was the inspiration for this song. Unfortunately he was a bit camera shy for the video and so has Crumble here standing in for him.

V = Am C Dsus2 Am
C = Am Am Am C

Dreaming of a blue sky
Inside gets me down

Blinded by the neon lights
I can barely see the ground

Here you come again
Lead me by the hand

Take me now, I’m waiting
For the promised land

Where you gonna run to?
Where you gonna run to?
Where you gonna run to?
Where they can’t find you

Where you gonna run to?
Where you gonna run to?
Where you gonna run to?
Where they can’t find you

Dreaming of the blue skies
And those golden plains

I know I’m never going to leave this place
Come stay with me a while

Where you gonna run to?
Where you gonna run to?
Where you gonna run to?
Where they can’t find you

Where you gonna run to?
Where you gonna run to?
Where you gonna run to?
Where they can’t find you

Where you gonna run to?
Where they can’t find you

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

#poem #poetry #python #programming

The next stage in my Found Poem Generator project was to work on item number 6 in my list of enhancements. This is to update the program to read in the found poem lines from a csv text file rather than having them hard-coded in the program itself:

1. Output the created poems to a larger screen ie a graphical type large fonted output rather than  using the console window.
2. Provide two methods of running:
a. Auto-generate a new poem every 5 minutes or so
b. Provide buttons for user input to create poems on demand
3. Package the program up to be self-contained
4. Deploy the program onto some sort of platform that wasn’t my Mac laptop ie a Raspberry PI and large screen monitor or TV
5. Make the program auto-run when the device is started / restarted

Further enhancements could then be:

6. Use a text / csv file to load in the words and lines of the poems rather than the hardcoded ones used currently
7. Check the first letter of each line to capitalise it for poems and un-capitalise it for haikus, and add commas to the end of lines if they are missing or remove for haikus.

This tutorial got me most of the way there, opening the csv file and reading in the records. I could then access the individual fields of each row using the ‘row’ command ie ‘row[0]’ gives me the data from column 0 of the current row, ‘row[1]’ gives me the data from column 1 etc. I was then ready to read the lines from the csv file into my data structure in the program. This is where I came a little unstuck.

 

My project was based on each poem line or phrase being an individual object. Each object was created one after the other and given a sequential name using code like this:

p1 = phrase(blah, blah, blah)
p2 = phrase(blah, blah, blah)

So ‘p1’ becomes an object called ‘p1’ which has some attributes ie the poem phrase or line, the word and syllable counts. Once all the objects are created they get loaded into a registry so I can keep track of them and mark them off as used once they have been selected and displayed for a particular poem. My problem here was auto-generating the ‘p1 =‘ part of the code for each record in the csv file. 

There just didn’t seem to be an easy way of generating the new object names in order to instantiate objects for them. A re-think was required.

In my previous life I would have read the data from the csv into a multi-dimensional array and worked with that, so I thought I would explore this option first. Unfortunately Python doesn’t appear to support multi-dimensional arrays. It has lists which are single-dimensional arrays and dictionaries which are more structured versions of lists but in order to make either of these multi-dimensional one has to make a list of lists or a dictionary of dictionaries which started to look quite complex for my dataset which has 4 columns plus the ‘line has been used’ flag in the registry. So I googled for possible solutions and came across someone asking a similar question and one of the answers intrigued and surprised me – Python has SQLLite built in and it can be instantiated and run solely in memory. 

Neat. Now all I have to do is chuck out most of my code and rewrite using a SQL table in memory instead of instantiating all those phrase objects and using a registry to track them all.

This is of course a lesson in how not to develop, or one of the pitfalls of having bespoke software written, and potentially a pitfall of the agile software development methodology. If we don’t know at the start where we are going to end up then we can end up writing ourselves into a corner. Ever asked for what you feel like would be a small simple change to a piece of software and been quoted an extortionate amount of effort for it? This is where I was right at this moment, my whole design no longer supported the new functionality I need to write into it.

In reading the poem phrases in from a file I could no longer use my object based data structure. Now that I was no longer using this structure it meant that my method and code for selecting the lines of poem no longer worked and were no longer appropriate either. 

I began working methodically, finishing the function that reads the data in from the csv file and then working through the two functions that created the poem output and the Haiku output. Once I had finished the poem function, I was able to reuse much of the code to update the haiku function.

The previous methods used a random number generator to pick a line number and then simply iterated through all the poem line objects until it reached that line and then checked a flag to see if it had already been used. If it hadn’t been used before the line was selected and the flag updated. Either way the process then ran again until all the required lines had been selected. This sort of continuous scrolling though data is just about passable as a solution when working with a small array of data in memory, but the conversion of the data storage to a SQL based system meant this method was not really best practice. Sure it would work but it wouldn’t be pretty under the bonnet. It just would not do.

The change was relatively straight-forward, the random line number is still generated, the line is read directly from the table using a SELECT and then checked for validity. If it passes it gets used, if it fails then we go round again until the requisite number of lines have been retrieved. After working on this method for the ‘poem’ function I made it a little more efficient by updating the SQL to only select lines that had not been used and were the correct number of syllables. This still meant the program spent an amount of time firing ‘SELECT’ statements and returning empty recordsets when they didn’t match the right criteria ie the random line selected had already been used or didn’t have the required number of syllables. An update for the future on this method would be to firstly identify all the valid lines and then select one randomly from that subset, this would bring the SQL interaction down to a maximum of two SELECTs per poem line rather than a variable amount which is happening now.

This version also contains a little tidying up, I’ve learnt more about the ‘Pack’ command and organised the screen a little better and also change the font size based on the number of lines of poem that need to be displayed – this prevents the buttons disappearing off the end of the screen on long poems. In the back of my mind I’m also wondering what will happen if I have long lines as well, these are likely to disappear of the the sides of the screen so I ought to write in some sort of dynamic font sizing routine to check for and handle these potential issues.

The updated source code has been placed in Dropbox for those of you enjoying working through it yourselves.

One final thought I had to add to the ‘To Do’ list is for the program to output each of the poems created to a text or log file to create a permanent record of all the combinations created.

 


More posts in this series:

Python ‘Found Poem’ Generator – Part 1 – Introduction
https://ijameslaurie.com/2018/11/03/found-poem-generator-introduction/

Python ‘Found Poem’ Generator – Part 2 – Adding Haikus
https://ijameslaurie.com/2018/11/13/found-poem-generator-adding-haikus/

Python ‘Found Poem’ Generator – Part 3 – The Next Level
https://ijameslaurie.com/2018/11/13/found-poem-generator-the-next-level/

Python ‘Found Poem’ Generator – Part 4 – Outputting Text To A Graphical Screen
https://ijameslaurie.com/2018/11/14/found-poem-generator-outputting-text-to-a-graphical-screen/

Python ‘Found Poem’ Generator – Part 5 – User Interaction – Adding Buttons
https://ijameslaurie.com/2018/11/16/found-poem-generator-user-interaction-adding-buttons/

–> This one –>Python ‘Found Poem’ Generator – Part 6 – Loading Data From A File
https://ijameslaurie.com/2018/11/21/found-poem-generator-loading-data-from-a-file/

Python ‘Found Poem’ Generator – Part 7 – Counting Syllables and Words
https://ijameslaurie.com/2018/12/03/found-poem-generator-counting-syllables-and-words/

 

 

 

 

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

Working through my list of enhancements for my ‘Found Poem’ Generator that I documented in a previous blog post: 

1. Output the created poems to a larger screen ie a graphical type large fonted output rather than  using the console window.
2. Provide two methods of running:
a. Auto-generate a new poem every 5 minutes or so
b. Provide buttons for user input to create poems on demand
3. Package the program up to be self-contained
4. Deploy the program onto some sort of platform that wasn’t my Mac laptop ie a Raspberry PI and large screen monitor or TV
5. Make the program auto-run when the device is started / restarted

Further enhancements could then be:

6. Use a text / csv file to load in the words and lines of the poems rather than the hardcoded ones used currently
7. Check the first letter of each line to capitalise it for poems and un-capitalise it for haikus, and add commas to the end of lines if they are missing or remove for haikus.

This blog post describes the update for item number 2(b):

2. Provide two methods of running:
a. Auto-generate a new poem every 5 minutes or so
b. Provide buttons for user input to create poems on demand

My previous experience with programming GUIs started a long time ago with Microsoft Access 2 and Visual Basic 4 and then, later, a number of Borland Delphi iterations with Microsoft SQL Server. With these development environments you got, well, development environments and that meant a WYSIWIG screen designer and a palette of buttons, text boxes, counters and other widgets to choose from. Not so with Python.

Tkinter Screen Layout Design

I had a google around for a WYSWIG screen designer but there really doesn’t seem to be anything that is recommended, one suggestion was to use graph paper but I’m not sure this project is complex enough to go that far. It seems that hand-coding buttons and labels is the way to go using one of the three Layout or Geometry Managers that Tkinter possess. These are Pack, Grid and Place:

‘Pack’, I’m informed, is the easiest to use but gives least control over placement of widgets, merely their relative positions to each other.

‘Place’ is used explicitly to set the position and size of a window, either in absolute terms or relative to another window. This didn’t sound like the sort of thing for creating and laying out a bunch of buttons.

‘Grid’ is similar to Pack but allows a certain amount of control of the placement of widgets by allocating them x and y co-ordinates within a notional two dimensional grid. The dimensions are then determined by the Grid Manager. This sounded the most likely candidate to try and use.

Tkinter Command Buttons

So now I have an inkling of how to place my buttons, my next task is to find out how to create the buttons and then assign the ‘poem’ and ‘haiku’ commands to them such that I could generate some interactive output.

Turning to the code, the first thing I found was that the label I was using to place the text on the screen, using an example copied from the internet, was using the ‘pack’ command. In order to keep things simple, I decided to change course and stick with the ‘pack’ function and see what the outcome was before deciding if I needed to get more complex with my button placement.

In the end I created three buttons and a slider bar, the buttons were for quitting the program (just to make things simpler for testing, I’ll remove this one later), a ‘haiku’ button and a ‘poem’ button. The slider allows the user to slide the scale from 1 to the number of ‘found’ lines there are available before pressing the ‘poem’ button to generate a poem of that number of lines. 

FoundPoemGenerator_Screen_Buttons

In doing this I uncovered what I thought was a bug in the system where after generating one haiku the program would appear to freeze with the spinning pointer icon, yet the ‘poem’ button worked fine. I say it worked fine, a little more button pressing revealed that the ‘poem’ button also eventually ran into the same problem, and then the penny dropped. The code is designed to mark any lines used in a poem as such ie already used, this meant that the ‘haiku’ function was very quickly running out of 3 and 5 syllable lines to use and so the program got stuck in an endless loop of looking for lines that weren’t there.

The fix was easy, to clear the ‘line already used’ flag between each press of a button. I’m not altogether happy with this outcome as my design was to ensure each poem produced was unique, which logically thinking couldn’t actually work unless I had infinite lines of poem to draw from. A case of having to admit my design was flawed and forge ahead with the new working design.

Source code available here for this update.

 


More posts in this series:

Python ‘Found Poem’ Generator – Part 1 – Introduction
https://ijameslaurie.com/2018/11/03/found-poem-generator-introduction/

Python ‘Found Poem’ Generator – Part 2 – Adding Haikus
https://ijameslaurie.com/2018/11/13/found-poem-generator-adding-haikus/

Python ‘Found Poem’ Generator – Part 3 – The Next Level
https://ijameslaurie.com/2018/11/13/found-poem-generator-the-next-level/

Python ‘Found Poem’ Generator – Part 4 – Outputting Text To A Graphical Screen
https://ijameslaurie.com/2018/11/14/found-poem-generator-outputting-text-to-a-graphical-screen/

–> This one –>Python ‘Found Poem’ Generator – Part 5 – User Interaction – Adding Buttons
https://ijameslaurie.com/2018/11/16/found-poem-generator-user-interaction-adding-buttons/

Python ‘Found Poem’ Generator – Part 6 – Loading Data From A File
https://ijameslaurie.com/2018/11/21/found-poem-generator-loading-data-from-a-file/

Python ‘Found Poem’ Generator – Part 7 – Counting Syllables and Words
https://ijameslaurie.com/2018/12/03/found-poem-generator-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 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!

James

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

Following on from my last post regarding the proposed additions to the ‘Found Poem’ Generator I started working on the list of enhancements with the aim of building the Found Poem Generator into something that could be used ‘out in the wild’ as a stand-alone art installation:

  1. Output the created poems to a larger screen ie a graphical type large fonted output rather than  using the console window.
  2. Provide two methods of running:
    1. Auto-generate a new poem every 5 minutes or so
    1. Provide buttons for user input to create poems on demand
  3. Package the program up to be self-contained 
  4. Deploy the program onto some sort of platform that wasn’t my Mac laptop ie a Raspberry PI and large screen monitor or TV
  5. Make the program auto-run when the device is started / restarted

Further enhancements could then be:

  1. Use a text / csv file to load in the words and lines of the poems rather than the hardcoded ones used currently
  2. Check the first letter of each line to capitalise it for poems and un-capitalise it for haikus, and add commas to the end of lines if they are missing or remove for haikus.

 

This post deals with number 1:

  1. Output the created poems to a larger screen ie a graphical type large fonted output rather than  using the console window.

The first task here was to identify a way of actually doing this, Python is a modular language where a lot of functions exist in external libraries that need to be added in to what ever you are writing. A search on the internet suggested Pygame or Tkinter might be the way to go. Pygame is a game development library whereas Tkinter is a GUI development library. Although neither jumps out as being ideal for simply outputting text to a large screen, Tkinter had the benefit of being built-in / pre-installed with Python so that was my first choice.

Another search on the internet, now looking for how to output text to a graphical screen using Tkinter, turned up a code snippet that I used to very quickly update the code to output to the graphical screen:

1-OutputToLargeScreen

As a proof of concept, this is working great for me at the moment, although there does seem to be some kind of bug or issue where the graphical screen doesn’t display or close properly. This appears to be a Mac specific issue, so I’m not too concerned at the moment as the project will ultimately be running on a Raspberry Pi, although I can foresee some more possible challenges with developing on a Mac and then porting to the Pi.

So onto the next stage now, providing buttons on the screen to allow prospective users to interact with the program and generate their own poems.

 

Source code for this update to the project can be downloaded here.

 


More posts in this series:

Python ‘Found Poem’ Generator – Part 1 – Introduction
https://ijameslaurie.com/2018/11/03/found-poem-generator-introduction/

Python ‘Found Poem’ Generator – Part 2 – Adding Haikus
https://ijameslaurie.com/2018/11/13/found-poem-generator-adding-haikus/

Python ‘Found Poem’ Generator – Part 3 – The Next Level
https://ijameslaurie.com/2018/11/13/found-poem-generator-the-next-level/

–> This one –>Python ‘Found Poem’ Generator – Part 4 – Outputting Text To A Graphical Screen
https://ijameslaurie.com/2018/11/14/found-poem-generator-outputting-text-to-a-graphical-screen/

Python ‘Found Poem’ Generator – Part 5 – User Interaction – Adding Buttons
https://ijameslaurie.com/2018/11/16/found-poem-generator-user-interaction-adding-buttons/

Python ‘Found Poem’ Generator – Part 6 – Loading Data From A File
https://ijameslaurie.com/2018/11/21/found-poem-generator-loading-data-from-a-file/

Python ‘Found Poem’ Generator – Part 7 – Counting Syllables and Words
https://ijameslaurie.com/2018/12/03/found-poem-generator-counting-syllables-and-words/

 

Python ‘Found Poem’ Generator – The Next Level

When the latest Ledbury Poetry Festival e-mail dropped into my inbox last week it set me thinking again about the ‘Found Poem’ Generator I had been working on using Python. The festival was announcing the debut of their collaboration between poet Sara Jane Arbury, Joined Up Heritage and Ledbury Poetry Festival who have created a poetic walk around eight key heritage sites in Ledbury using ‘found poetry’ comprising words from First World War poets and phrases contained in the buildings themselves.

Ledbury has quite a history with poetry, being the birthplace of Poet Laureate John Masefield and has close associations with the Dymock Poets.

With my current interest in ‘found poetry’, following the course I participated in on FutureLearn featuring Michael Simmons Roberts and Helen Mort, and my latent interest in Ledbury and the Ledbury Poetry Festival having lived in Ledbury for a short while, this set the thoughts rolling around in my head: what if I could extend my Found Poem Generator to create a display piece or interactive kiosk? Could this then be utilised as a poetry installation out in the real world?

So, in order to achieve this I need to:

  1. Output the created poems to a larger screen ie a graphical type large fonted output rather than  using the console window.
  2. Provide two methods of running:
    1. Auto-generate a new poem every 5 minutes or so
    1. Provide buttons for user input to create poems on demand
  3. Package the program up to be self-contained 
  4. Deploy the program onto some sort of platform that wasn’t my Mac laptop ie a Raspberry PI and large screen monitor or TV
  5. Make the program auto-run when the device is started / restarted

Further enhancements could then be:

  1. Use a text / csv file to load in the words and lines of the poems rather than the hardcoded ones used currently
  2. Check the first letter of each line to capitalise it for poems and un-capitalise it for haikus, and add commas to the end of lines if they are missing or remove for haikus.

Subscribe to my blog and watch out for these enhancements and future posts – coming soon!

 


More posts in this series:

Python ‘Found Poem’ Generator – Part 1 – Introduction
https://ijameslaurie.com/2018/11/03/found-poem-generator-introduction/

Python ‘Found Poem’ Generator – Part 2 – Adding Haikus
https://ijameslaurie.com/2018/11/13/found-poem-generator-adding-haikus/

–> This one –>Python ‘Found Poem’ Generator – Part 3 – The Next Level
https://ijameslaurie.com/2018/11/13/found-poem-generator-the-next-level/

Python ‘Found Poem’ Generator – Part 4 – Outputting Text To A Graphical Screen
https://ijameslaurie.com/2018/11/14/found-poem-generator-outputting-text-to-a-graphical-screen/

Python ‘Found Poem’ Generator – Part 5 – User Interaction – Adding Buttons
https://ijameslaurie.com/2018/11/16/found-poem-generator-user-interaction-adding-buttons/

Python ‘Found Poem’ Generator – Part 6 – Loading Data From A File
https://ijameslaurie.com/2018/11/21/found-poem-generator-loading-data-from-a-file/

Python ‘Found Poem’ Generator – Part 7 – Counting Syllables and Words
https://ijameslaurie.com/2018/12/03/found-poem-generator-counting-syllables-and-words/