Generating Poems: A Way with Words and Code

How I built a poem generator app using Watson APIs

Nikole McLeish
Jul 28, 2017 · 5 min read

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.

An example poem from my Poem Generator application.

How was it built?

"document_tone": {
"tone_categories": [
{
"category_id": "emotion_tone",
"tones": [
{
"tone_name": "Anger",
"score": 0.000105,
"tone_id": "anger"
},
{
"tone_name": "Disgust",
"score": 0.001659,
"tone_id": "disgust"
},
{
"tone_name": "Fear",
"score": 0.026971,
"tone_id": "fear"
},
{
"tone_name": "Joy",
"score": 0.066884,
"tone_id": "joy"
},
{
"tone_name": "Sadness",
"score": 0.946133,
"tone_id": "sadness"
}
],
"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?

Adding Lines

The Poem Generator is bootstrapped with a database of scored lines from various verses.

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.

Generating Poems

What other features does it have?

Note the various options along the bottom of the UI that you can toggle on and off.

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.

Database Management

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

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.

IBM CODAIT

Things we made with data at IBM’s Center for Open Source Data and AI Technologies.

Thanks to Mike Broberg and G. Adam Cox

Nikole McLeish

Written by

IBM CODAIT

Things we made with data at IBM’s Center for Open Source Data and AI Technologies.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade