Finding Haikus In Song Lyrics With JavaScript

When I tell people that I’m learning how to code, almost everyone asks me what I want to make. I like the idea of procedurally generated art, or computer programs that make things that are interesting to read or look at. One of my first projects was making a program that crawled through popular songs and pulled out lines that matched the structure of a haiku.

A haiku is a form of poetry that follows a very strict structure. The poem is always three lines, and each line has a set number of syllables (five in the first line, seven in the second, five in the third). You may have written these in grammar school at some point. They usually look like this:

I am a haiku
I am a perfect haiku
Thanks for listening

Step One: Finding The Lyrics

I wanted to make a program that found song lyrics that happened to match this structure. The next step was understanding how to use an Application Programming Interface, or API. APIs are basically endpoints that allow us to access information from websites. I like to think of them like electrical outlets: we don’t really need to know how electricity is created or moves in order to plug in a toaster. APIs provide a place for us to plug in the “appliances” we make and to use the “electricity” (information) we receive from them.


To make this project work, I used the Genius API. Genius is a website that specializes in song lyrics and annotation. By using the Genius API, I could use JavaScript to access all the song data that was already available. However, here’s the catch: The Genius API gave me all the information I needed about particular songs, BUT it didn’t give me the text of the lyrics themselves. How frustrating!

Fortunately, many other people encountered this problem before. After some Googling, I found that the best way to get lyrics-text from Genius was to use a web-scraper, or a program that “scrapes” a webpage for relevant information. This took a little bit of trial-and-error, but I ended up with something that flowed like this: my program got the URL of the song-lyrics-page from the Genius API, and then put that URL into a web-scraper that pulled the lyrics out for me so I could use them for my haikus. Phew!

Step Two: Finding The Haikus In Song Lyrics

Now I had a chunk of lyrics-text. Next, I had to count the syllables of each word in the song so that I could find lines that matched the structure of a haiku. Let’s use this arbitrary example to illustrate the concept. Here are some of the lyrics to Taylor Swift’s recent song “…Ready For It?”

In the middle of the night, in my dreams
You should see the things we do, baby
In the middle of the night in my dreams
I know I’m gonna be with you
So I take my time
Are you ready for it?
Ooh, are you ready for it?
Baby, let the games begin
Let the games begin
Let the games begin
Baby, let the games begin
Let the games begin
Let the games begin
[from “…Ready For It?” by Taylor Swift]


To count syllables, I used a pre-existing program called “syllable” that tells me how many syllables are in a given selection of text. I incorporated this program using NPM, which you can google and read about elsewhere if you are so inclined. My rules were: IF you find a line with five syllables AND the next line has seven syllables AND the line after that has five syllables, give me those three lines. And that, lovely humans, is programming in a nutshell. Here’s a fun little look at what this particular chunk of the program looks like:

Screenshot 2017-09-25 at 12.14.48 PM
Formatting the code for WordPress was a pain in the ass so here’s an annoying photo of text.

Step Three: Finding Songs With Haikus In Them Using Billboard

Under the terms I defined, it is startlingly difficult to find haikus in song lyrics. For simplicity’s sake, I separate each song into lines and find the syllable count of each line (as opposed to finding 17 syllable units and disregarding lines). Using this method, I did not find a lot of haikus. If I wanted to find haikus in song lyrics, I needed to run this program through a large assortment of songs. What better place to get a list of songs than from Billboard’s Top 100 list?

Fortunately, there was another pre-existing program on NPM called “billboard-top-100” which returns (or “gives me”) a full list of the most popular songs that week, as determined by Billboard, a popular song-ranking publication. Using this program, I generate the list and search each song in the Top 100 for haikus. The flow of my program now looks like this:

Billboard => Genius API => Web Scraper => Haiku Finder

Since running this program, I typically find 3 haikus per 100 songs, which isn’t a great number but makes it that much more satisfying when I come across a good one. I ran the program just now and this is what it spat out:

Screenshot 2017-09-25 at 12.28.50 PM

Notice our example from above made it into the list! Definitely not T-Swift’s most poetic work, but damn she really has a grip on structure.

As you may have noticed, programming sometimes feels like building a Rube Goldberg machine. You are connecting endpoints of things that traditionally aren’t made to fit together, but you are writing code anyway to glue one kind of functionality to another. It’s tedious work to do and learn, but it looks like it can be progressively more fun as you get the handle on the basics.

The Guardian just released an article that argues that pursuing a career in code is not as lucrative as one might imagine, considering how many people are scrambling to get into the industry (link).  I think code is a good thing to learn regardless, but I am now discouraged as a daytime barista looking to jump into a higher pay grade. It is also likely that the need for coders will diminish as programs soon end up writing themselves (this is also already happening).

I like learning, but I’m keeping an eye out for jobs and opportunities that will put me in a better position 5-10 years from now. Code is fun, but perhaps it isn’t as great of an investment as I initially thought. I’m going to keep at it while exploring my other options, and it may help me with short-term gains at least before I catch the wave of an another emerging industry.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s