Generating Poems: A Way with Words and Code
How I built a poem generator app using Watson APIs
Editor’s note: This article marks the first in an occasional series by the 2017 summer interns on the Watson Data Platform developer advocacy team, depicting projects they developed using IBM Cloud data services, Watson APIs, the Watson Studio platform for data science, and more.
Some people have a way with words. Others have a way with code. If you’re using my Poem Generator project, you don’t necessarily need either. The application creates poems based on user input. Using the Watson Tone Analyzer service, the user’s feelings are scored on a scale of 0 to 1. The Poem Generator then uses these feelings to craft a poem.
How was it built?
The Poem Generator is a Flask web application. Flask is a Python microframework for web development. The application uses Watson Natural Language Understanding, in addition to the Tone Analyzer service, both available on the IBM Cloud. Tone Analyzer analyzes the emotional sentiment of a text. Natural Language Understanding can extract topics from text with keywords and entities.
"category_name": "Emotion Tone"
In addition, the application uses a PostgreSQL database and offers in-app database management. On IBM Cloud, you can create a PostgreSQL database using either Compose for PostgreSQL or with ElephantSQL.
How does it work?
The Poem Generator keeps a database of lines and their emotional content, if any. When a user enters a line, the application calls the Tone Analyzer service and receives scores on the emotional content of that line. If a score for a particular emotion is high enough, that emotion will be marked.
Lines that do not score high enough for any emotions are marked as fillers. These lines have no distinct emotional content, allowing them to be placed in any poem without affecting the tone. Alternately, the application allows users to import and export multiple lines as well.
Users receive the input prompt, “How are you feeling?” If there is emotional content in their input, the generator will gather all lines that register any emotion. The application then randomly selects lines to craft a 5-line poem. The generator will also randomly determine and select a filler line for lines 1, 3 and 5.
What other features does it have?
When generating poems, users can toggle certain features. The first is Word Replacement. This feature uses Watson Natural Language Understanding to extract keywords from the user’s input. Then this service will analyze the generated poem for keywords. If both contain keywords, then a random keyword from each will be swapped. I added this feature to allow more personalization to the poems.
Other features include options to change how the application selects emotions. The Dominant Emotion feature will select the highest-scoring emotion and create a poem based on that.
The Shared Emotions feature will select lines that match the range of emotions flagged in the user’s input. This feature requires lines in the database that can simultaneously satisfy multiple emotions. Lines that have more than one prominent emotion, however, are more difficult to find, causing this feature to rarely generate successful poems.
The last feature for generating poems is No Fillers. Fillers are lines that do not exhibit strong emotions. When users select this feature, the generator will not add filler lines to the poem.
The Poem Generator uses a PostgreSQL database. When using the application for the first time, it creates a table if one does not already exist in the database. Eight columns comprise this table:
filler. The database prevents duplicates by requiring unique lines.
When creating a table for the first time, a collection of modern lines will be automatically added. You can view all the lines in the database via the app’s UI. These lines can be added, modified and re-scored, or deleted in the application. There are also options to export all lines in your database to a CSV file. You can do a bulk import of lines using a CSV too. Additionally, you can delete all lines from the table in your PostgreSQL database.
Current limitations and future improvements
People could potentially use the Poem Generator to create stories or songs. One limitation, however, was the context of a line’s location in a particular poem. When analyzing the tones of a line of poetry, the emotional content does not always match the emotions of the overall poem. Thus, a poem containing lines marked as sad may exhibit other emotions, like fear or anger.
Looking toward future improvements, this generator could use machine learning to determine which lines are better together. If users could rank or like certain poems, the overall emotional content, line placement, and lines included could be used to create better poems.
It was my first time working with the Watson APIs, and I hope it serves as a useful example to others just getting started as well.
If you enjoyed this article, please clap and recommend it to other Medium readers.