Making an Emily Dickinson Poetry Bot 
Tags: beginners, tutorial, machinelearning, learning
Published: 2019-04-19 17:30:50

---
title: Making an Emily Dickinson Poetry Bot 
published: true
description: Getting a Grasp on Machine Learning with Spell
tags: beginner, tutorial, machine learning, learning
canonical_url: https://www.nadyaprimak.com/blog/programming/poetry-bot/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/vnkj24hkopxgylh1u9py.jpeg
---

Yesterday 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.  {% link https://dev.to/nprimak/my-foray-into-machine-learning-with-style-transfer-and-spell-1lp1 %} 

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.

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 (https://medium.freecodecamp.org/the-ultimate-guide-to-web-scraping-with-node-js-daa2027dcd3) and then poked around some documentation for puppeteer as well as cheerio to figure out how I could get the scraper to press the "next" button 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 with 31 pages of poetry by Emily Dickinson. 

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. I wasn't able 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 to make it easy 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 that uses the model to generate new poetry, with an adjustable variable called "temperature" that 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: {% youtube xfuVcfwtEyw %} 

Of course if you're interested in seeing my code, I have it up on github here:
{% github nprimak/emily-dickinson-lstm %}

*If you enjoyed this article, consider following me on Twitter [@nadyaprimak](https://www.twitter.com/nprimak) or if you need more tips on breaking into the tech industry, you can read my book [Foot in the Door](https://www.amazon.com/Foot-Door-Getting-Industry-Programmer-ebook/dp/B0813XNFNF) in paperback or Kindle now.*


The post [Making an Emily Dickinson Poetry Bot](https://www.nadyaprimak.com/blog/programming/poetry-bot/) appeared first on [www.nadyaprimak.com/blog](https://www.nadyaprimak.com).