Fullstack Academy - Romance.js - Programming Valentine's Day Poetry

Comments: "Fullstack Academy - Romance.js - Programming Valentine's Day Poetry"

URL: http://blog.fullstackacademy.com/post/76605703216/romance-js-programming-valentines-day-poetry


On this Valentine’s Day - two aspiring poets at Fullstack Academy want to help you use programming to further your own romantic endeavors.

A bit of background

Nimit and I (David), now instructors at Fullstack Academy, actually met on the first day of college over 14 years ago and bonded over our mutual love of Web technologies and the ethos of open-source (our youthful form of rebellion consisted of running Linux instead of Windows). That’s our founder story and why we continue to this day to love working with the Web and training aspiring Web developers. However, what’s less well known is that we also share a mutual love of poetry and considered ourselves somewhat accomplished amateur poets throughout college.

Although our poetry career never fully blossomed, on occasion you might still find one of us counting syllables on fingers or looking up rhymes as we construct a poem for our loved ones. I’ve always found the hardest thing to do is find inspiration - find something that knows what I want to say and how I want to say it.  Fortunately, with a bit of computer programming, we can train our computer to mimic any poet, including ourselves.

If you’ve started with Codecademy or Codeschool’s JavaScript course, this exercise should be right up your alley. It’s a fun way to explore language and we hope it will inspire you this Valentine’s Day.

Sounding like ourselves

If you’ve ever played Mad Libs, you’ve experience the amusing quality of text interpolated with some randomness. Our mind has an amazing ability to impose a story onto that randomness. Our poetry program is like Mad Libs but with two differences:

Our program chooses every word rather than filling in blanks Our program trains itself by building a set of all words that follow each word in the corpus (the corpus is the poetry or text you want to mimic). Our program picks a random word from the corpus to start. Then it picks from the set of words that have followed this word.

For example, if we take one of my favorite poems from e.e. cummings:

since feeling is first
who pays any attention
to the syntax of things
will never wholly kiss you;

wholly to be a fool
while Spring is in the world

my blood approves,
and kisses are better fate
than wisdom
lady i swear by all flowers. Don’t cry
—the best gesture of my brain is less than
your eyelids’ flutter which says

we are for each other: then
laugh, leaning back in my arms
for life’s not a paragraph

And death i think is no parenthesis

We first identify the unique words that appear in the poem: “since”, “feeling”, “is”, “first”, “who”, “pays”, “any”, “attention”, etc. Then for each word, we identify the list of words that follow that word. For example, for the word “is” we see “first”, “in”, “less”, “no” and for the word “my” we see “blood”, “brain”, “arms”. We store these “following” words associated with each starting word.

The magic about this is that based on how we train our model (what body of text we put in and what sequences it has) it can create a pretty good approximation of what the author of that corpus sounds like.

Although you can be the judge of whether or not you like the poetry this generates, it does pose an interesting philosophical question: are we anything more than the aggregate of all our experiences and language combined in some probabilistic web.

Setting up the Corpus (poetry you want to mimic)

For our code to work, it first needs some body of text to train itself on.  Let’s set up a place in our HTML where we can dump a bunch of text.  For that, we’re going to use a trick that web developers use to store long strings in HTML - script tags that aren’t JavaScript.  By setting it up this way, the browser ignores it but makes it still easy for us to grab the text out. Feel free to follow along by opening your own JSFiddle.net fiddle.

Now let’s set up a way to store all the pairs.  For our implementation, we’ll create a JavaScript object where the keys are single words and the values are an array of all the words that follow that word in our corpus.  Every time we see a new word combination, we’ll store the combination in our object for use later.  We’re going to store duplicates as well so later on, we don’t have to worry about storing probability weights, we just need to randomly choose a word and the probability will work itself out.

Great, now we have two helpful pieces of data: words and wordpairs.  words is a long list of every word in our corpus and wordpairs is where the pairs are stored.  wordpairs[“love”] will return an array (a list) of every word that our program has ever seen follow love.

Making Poetry

Now that we’ve prepped the kitchen we just need to flambe the paper (don’t worry, your program will botch its metaphors even worse). Let’s write a function called “writePhrase” that takes a length of words and returns a phrase of poetry.  We also write a helper function to randomly choose from an array since we’ll be doing that several times.

Take a look at this following JSFiddle (set up with Shakespeare’s Sonnets) to see what kind of output it creates. Open the JSFiddle  and you can change out the corpus and see what kind of other poems you can create! Perhaps you can use it as an inspiration for a beautiful poem for your own loved one.

Further Challenges

The core system that we’ve built is called a Markov Chain (read more on Wikipedia). It’s a simple and elegant way to model various problems in Computer Science and as we’ve seen, can be used to generate amusing text based off a training corpus. However, there are still many directions to go from here:

  • Our program doesn’t handle sentence structure in the original corpus, this makes it start and stop each phrase awkwardly
  • We’re pretty aggressive about removing punctuation, this produces a bland ending text - try to keep two versions of the word, one that has been normalized and one that keeps the original formatting


If you’d like to continue exploring programming and dive deeper into computer science and web development, feel free to reach out to me at david@fullstackacademy.com. We’re hosting a free one-day introduction to web programming called Road to Code in early March at Fullstack Academy where beginners learn to do other awesome things with code!

Original post on Hacker News