How to Make a Poetry Bot

A while ago I published a post about how I used Spell, a machine learning framework that allows you to train your models faster without the pain of dependency management on your machine, to implement a style transfer. I’ve continued to use Spell, and have expanded into exploring other training models. In this post I’m going to show you how to make a poetry bot.

Today I wanted to write about making your own text bot with Spell. I’ve always been a huge fan of Emily Dickinson’s poetry. I know some find her work a bit morbid, but her poetry has spoken to me throughout many years and I continue to marvel at how someone who rarely left her home could have such incredible insight into the human condition, the natural world, and the realities of life and death.

A portrait of Emily Dickinson, the famous reclusive poet

Since Spell conveniently had a video tutorial for creating a bot trained on lyrics from DOOM, I figured I could apply this quite easily to Emily Dickinson’s poetry.

Surprisingly for me, the hardest part was not the training but the web scraping component. I started out with the Node.js tutorial for web scraping and then poked around some documentation for puppeteer. I also looked at cheerio to figure out how I could get the scraper to press the “next” button. This was necessary for when it reached the end of the results on a page.

At first I decided to scrape poets.org, which only had 5 pages of poetry by Emily Dickinson. It seemed like a paltry amount, and I knew Emily Dickinson had been very prolific. So, I did some searching online to find a better source. Although a bit more difficult to scrape, poemhunter.com ended up being a better source. It had 31 pages of poetry by Emily Dickinson. Jackpot!

I used NodeJS for scraping a website with all of Emily Dickinson’s poems

I did run into some issues at this point, with lots of errors about the socket hanging up that I thought was crashing the scraper. It was hard to deduce exactly what was the cause, but by modifying my file writing function to add each poem to the file one by one instead of appending each poem to an increasingly long string and writing the file at the end, I still got the data back that I was expecting.

All of Emily Dickinson’s poems were added to the same text file. This made it easier for the LSTM training model to process the data. Once the model was created, I downloaded it back to my machine and created a small web app with ML5. The app uses the model to generate new poetry, with an adjustable variable called “temperature”. The variable allows you to change the amount of randomness you see in the generated text. As opposed to the AI drawing purely from the poetry data in the file.

Here is a brief sample of output from the bot, for your reading pleasure:


Birds at the Corn— The Sun— With Men Orchard with Me— The Bone at House— The Bonnet Day— The Milly strain of He can the March— And then I star me— And Death— A Dread His Eyes the Sun— And then the Sun too Day the Birds— The Room To only still— And I too see the Soul That Desert face of Sun— The Man As House He stand— And stand to the Soul a Bee— And then the Soul as Beauty

If you’re interested in creating your own text bot with Spell, check out this video tutorial which I used:

Hopefully now you have a good idea of how to make a poetry bot! Of course if you’re having any trouble and want to see my code, I have it up on github here.