Finding Haikus In Song Lyrics With JavaScript

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.

Advertisements

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.

yung-chang-108329

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.

Big O Notation & The Programs I’d Like To Write

A lot of the time when I tell people that I’m learning computer programming, they ask me what I’d like to make.

[Currently Reading: “Cracking the Coding Interview” by Gayle L. McDowell. Chapters I-VII]

I’m not a “computer guy” just yet, so sometimes a lot of the technical terminology that surrounds code escapes me. I’m reading “Cracking the Coding Interview” per the suggestion of my close friends. They tell me that working through the problems in the book will help me a lot with what I’m trying to accomplish. The idea is to throw me into the swimming pool in order to teach me how to swim.

The book so far is a mix of interview strategy and crash courses in coding concepts. The one I’m currently working on is an essential concept called “Big O Notation.” Big O is a way to roughly estimate the time an algorithm will run (runtime). This is useful especially when the algorithm or program is handling a large amount of information, because Big O measures how quickly you will get through the data that is pushed into the algorithm.

The tricky part for me comes to calculating Big O times. There are a handful of rules and math concepts that you need to grasp in order to calculate how long an algorithm will run. It has been a while since I’ve seen anything that looks like math, so it takes a little extra effort to make that jump again. However, from looking at examples and working through exercises, I get the sense that it will be relatively simple to determine Big O notation eventually. It will probably take me O(N) to get through the brunt of it though (lol).

tim-gouw-68319
This is a stock photo of a hardworking individual.

“Cracking The Coding Interview” also describes interview practices company-by-company. McDowell highlights the differences between a Facebook interview versus an Amazon interview and that kind of thing. What’s funny about these is how often McDowell mentions that companies are looking for someone who is enthusiastic about their product. I guess this is a no-brainer, but I can see how someone might forget to express how excited they are about, say, Microsoft, while in a technical coding interview. A lot of the time when I tell people that I’m learning computer programming, they ask me what I’d like to make. I don’t have a very good answer for that yet, because truthfully I just want to get out of customer service and retail as soon as possible.

The thing that excites me the most is the possibility of making computer-generated art (novels, poetry), which is cool but I am afraid that there is little-to-no money in this kind of thing. But I hope that I’m wrong, because it would be super interesting to develop programs that write television shows or articles that make sense and also hold a human audience. I also tell people that I’m interested in the cross-over between code and customer service. I know for a fact that an Amazon Echo device could have done 80% of my job as a bookseller, and I would be interested in developing services and products that do just that.

I know a lot of people are afraid of being put out of work by robots, and I believe this shift is inevitable. I don’t think the answer is telling technological progress to stop, but rather we should see what kinds of opportunities blossom once robots take a lot of the grunt-work away from us. Perhaps the massive shift away from human work will put pressure on our governments to provide Universal Basic Income for the population. Hopefully this kind of policy would allow us to spend our time on more meaningful pursuits.

My Favorite Wikipedia Pages

Information, when organized neatly, can be a sublime experience.

At a previous job, I spent a lot of time looking at Wikipedia pages to fill time. After a while, I realized that I returned to some pages over and over again. These pages were interesting because they elevated the medium of the Wikipedia page by becoming a piece that felt more like art than a reference page. I sat down with my friends and tried to hash out what exactly makes a Wikipedia page great, and we settled on the following criteria.

  1. The page must have good one liners. The page must be quotable for easy sharing and to leave you will a snippet that stays in your mind long after you’ve read the article.
  2. The page should have a compelling narrative. Stories are always good, and the page should draw you into one.
  3. The page should have interesting subsections. There is nothing better than scrolling down to the “Controversy” portion of someone’s Wikipedia page. My personal favorite is skipping directly to “Early Life” and “Early Career” since I want to see what successful people were doing at around my age.
  4. The page must excel at being a Wikipedia page. Interesting people and subjects are great, but I am interested in how these people fit into the format that Wikipedia has designed for us, from the table of contents to the footnotes.

That being said, here are some of my favorites.

Will Smith. (https://en.wikipedia.org/wiki/Will_Smith).
I think about Will Smith’s Wikipedia page a lot, mostly because of the “Religious Beliefs” subsection. Will Smith and his family seem to be very spiritual people (just look at the interview with Willow and Jaden Smith in which they delve into their own personal philosophies). In the “Religious Beliefs” section of the Wikipedia page, there is a whole paragraph dedicated to the fact that he has spoken favorably of Scientology (though is not a Scientologist). “”I just think a lot of the ideas in Scientology are brilliant and revolutionary and non-religious,” he says. I am a little confused how and why this made it onto Will Smith’s Wikipedia page, alongside all of his other accomplishments. I understand the controversy surrounding Scientology and by no means endorse it. I wonder why Will Smith’s opinion on the matter holds any weight in the debate, especially when this quote illustrates that the ideas in Scientology are, to him, non-religious. I guess I just think about Will Smith in many regards*, none of which include this offhand mention of Scientology.

Lord Byron. (https://en.wikipedia.org/wiki/Lord_Byron).
I looked up Lord Byron after watching the movie “The Trip” with Steve Coogan and Rob Brydon. I was blown away. Lord Byron was the biggest fuckboy ever to tread planet Earth. His Wikipedia page is expansive and divided into wild chapters of his various escapades. Byron’s Wikipedia page was particularly useful to me because the information was tangentially work-related and I felt like I wouldn’t get in trouble for studying literary history. But also I was there for all of the insane fuckery that Lord Byron got himself into. This page requires multiple visits, and the subsections read well by themselves (“Fondness For Animals”, “Education and Early Loves”, “Political Career”). The fact that all of this is plotted out in an outlines and structured manner when Byron’s life seem to be the opposite of outlined and structured makes it a great read.

byron
Look at this fuck-o.

Minnesota Nice. (https://en.wikipedia.org/wiki/Minnesota_nice).
I visited this page after watching the Coen Brother’s “Fargo” (1996). Regional cultural differences were relevant to me at the time. I was living in Portland where people seemed to be generally friendlier than other place I had visited. I think about this page a lot, especially when I move to new towns and try to get a sense of the cultural climate. Living in Seattle is refreshing after living in Portland, since people are more inclined to be just outright nasty. I sometimes think it is directly linked to population density, that the more tightly packed people are crammed into a given space, the more of an asshole they will typically be in everyday life.

Here are some honorable mentions for some Wikpedia pages that my friends and I like that also may tickle your interests.

Everyone has their favorite topic, so you can imagine that this list goes on for a while. When I asked my friends what their favorite pages were, everyone had an enthusiastic response. This thrills me, because it shows me that people are curious and excited about the things they find, especially in a kind of dry format. Information, when organized neatly, can be a sublime experience.


*Most often in the context of the movie “I, Robot”.

How “The Wire” Frames My Career Perspective

I watched “The Wire” recently and was completely hooked for two reasons. First, it is a well-written, well-performed, and extremely compelling television series. Secondly, it helped me gain some cognitive tools for how to optimize my life and career aspirations by helping me process my environment differently. 

I watched “The Wire” recently and was completely hooked for two reasons. First, it is a well-written, well-performed, and extremely compelling television series. Secondly, it helped me gain some cognitive tools for how to optimize my life and career aspirations by helping me process my environment differently.

David Simon spent twelve years reporting on crime for the Baltimore Sun before losing his respect for journalism. Convinced that news had little effect on the population, he began to write books about crime and drug trade, along with the TV series “The Wire”. He said that the show  is “really about the American city, and about how we live together. It’s about how institutions have an effect on individuals. Whether one is a cop, a longshoreman, a drug dealer, a politician, a judge or a lawyer, all are ultimately compromised and must contend with whatever institution to which they are committed.”

david-simon
David Simon, creator of “The Wire”.

What makes “The Wire” so interesting is the well-researched insight into how and why people get involved in crime, or any other business in urban environments. It is heavily influenced by the design of a city or system, and how the people within it move to find their own path of least resistance. Sure, we all have an illusion of free-will, but sometimes immediate gains depend on the structure of what surrounds us.

This has helped my career  by practicing a “zoom out” every once in a while, where I evaluate my role in the larger system. I try to stay in touch with my skill set, and then see where I can place those skills easily in a system for maximum gain. For example, I recently switched my career from bookseller to barista and doubled the amount of money that I make per hour. By “zooming out”, I saw that my career as a bookseller had no future and no paths to something valuable or profitable in the short term, and sticking with the job would only prolong my financial woes. I quit immediately and picked up a coffee job in Seattle, and now I work in a industry that pays some of its employees more money that most booksellers.

It is so easy to lose scope. Our day to day lives encourage us to keep blinders on in order to focus on the tasks at hand. However, a regular practice of contemplating the infinite (whether it be in church, at yoga, or while meditating on the bus) allows us to realize that (1) we are are small units within much larger structures and (2) our problems are even smaller units within these structures.

nikita-maru-70928

Broadening scope is like giving yourself a road map to optimize your value. Once you have this map, you start to see routes to a better career. You see which businesses, social circles, and institutions can bring you closer to your goals. As a barista, I get to meet a high volume of customers on a regular basis. I make more money and get more flexibility with my schedule. Living in Seattle, I get to meet with my friends who are software developers more often. I have maximized my social capital, I have a job that suits my needs, and I have much more opportunities to develop my programming skills.

Watching “The Wire” is like watching all of the cogs and gears of a clock turn. Applying this framework to my surroundings is endlessly fascinating, and also gives me reason to learn more about the architecture of my environment. I am now watching a lot of “current economics” videos on Khan Academy because I have a newly developed interest in how my cities spin, and hopefully it will allow me to create my own effective sub-system within it.

My First Taste of the Coding Interview

Part of being a software developer is dealing with problems that you have no idea how to solve.

Over dinner, I spoke with my close friends Zac and Elliott about what my next steps should be if I want to break into a software development job. “First, buy Cracking The Code Interview,” said Zac, “and just read the whole thing.” I immediately took his advice, and the next morning I walked to the closest bookstore and bought the Cracking The Coding Interview by Gayle Laakmann McDowell. While at the store, I picked up an Arduino Starter Kit as a fun way to apply some of the stuff I’ve been learning.

FullSizeRender (1)

“When I first started on the problems in Cracking The Code Interview, I spent a lot of time banging my head against a wall,” Elliott told me. Zac chimed in “But it’s just practice.” My friends explained that if I spent a little bit of time working on code problems in the book, I would become familiar with more complex problems than the ones I am currently solving. “It’s all about putting yourself in an uncomfortable situation. Part of being a software developer is dealing with problems that you have no idea how to solve,” said Elliott.

After dinner, we drove back home and I flipped through Cracking The Code Interview. Suddenly, Elliott slapped down a stack of papers and a red Sharpie in front of me. “We’re going to solve some problems,” he said. We picked two questions (ranked “easy” on leetcode.com) and Elliott had me talk through the problems and tell him how I would solve them. A couple times I offered solutions using JavaScript methods and he laughed. “I want you to solve this with math,” he told me. I got a crash course in modulus operators and common pitfalls from dealing with loops and arrays. I lost track of time, but we probably worked on the problems for more than an hour.

“I threw a lot of stuff at you but you handled it well,” said Elliott, swirling a glass of wine. “When I was preparing for my interview, the first thing I did when I woke up in the morning was answer code questions. I had to be ready to solve complex challenges under any circumstances, so my mom would ask me stuff from Cracking The Code Interview at random times.”

FullSizeRender

This kind of training and diligence thrills me. I remember watching the movie Somm and hearing something like this: There are some people who hear about difficult tasks and think “wow that’s really incredible” and others who hear the same thing and think “I want to do that.” I definitely fall into the latter category. Zac told me he felt similarly, that always when someone tells him about something that was really difficult, he immediately wants to try it to see if he can go about it in a smarter way.

A lot of getting good at anything is just training, so today I’m putting in the time to get through Cracking The Code Interview while getting my feet wet with Java. It really helps, though, to have such a supportive friend group. I told Elliott that helping me solve problems on paper felt really useful. “Anytime. Not literally,” he said,  before shutting the door.

My First Year of JavaScript

What I’ve noticed is that when you’re starting out with programming, you will often put in a heroic amount of effort to produce something that seems, on the surface, unimpressive.

I’ve spent about a year taking online courses with Treehouse (teamtreehouse.com) in order to break into computer programming. It’s been a ride, and I’ve met a ton of interesting people along the way. I suspect that there might be a lot of people in my position, people who have realized that their retail jobs will soon become obsolete and that a functional understanding of computers and code is necessary in order to be a human in the coming years. So, I would like to share what I’ve learned (and what I’m learning) in my forays into tech in order to inspire my peers, or at least make them feel less alone.

I chose JavaScript because it seemed to have a large fan-base. In Portland, Oregon, I had friends who connected me with JavaScript meetups and opportunities to learn the potential of what was possible with the language. On Treehouse, the JavaScript teachers also seemed SUPER excited about JavaScript (Dave McFarland almost leaps over his desk to tell you how excited he is to teach you JavaScript). With such an enthusiastic community, how could I not get started?

Screenshot 2017-09-12 at 3.44.05 PM
Serious face and Treehouse stats.

JavaScript is a programming language mainly used to make stuff that you see in your web browser. It allows you to make animate, direct traffic, create tools, and much more. The terrain for what is possible with JavaScript is vast and growing, and a lot of what I have been looking into recently has included using JavaScript to write programs for internet-connected devices like the Amazon Echo, or even for Arduinos. Most of my coursework has been focused on making “dynamic web pages”, which basically means websites that you can actually use to do things as opposed to just reading them. For example, you could use JavaScript to make a website that keeps your shopping list, or turns music into animations, or generates random movie-plots.

The movie-plot idea was one of my first projects in JavaScript. It is based on a book that I encountered at Powell’s, which kind of resembled those books where you can change the head, torso, and legs of a monster by turning the pages. I wanted to make a program that created random, silly treatments for movies. And lo! I did.

Screenshot 2017-09-12 at 3.30.29 PM
This is the kind of stuff my computer spits out when I run the program.

This is pretty rudimentary, but I’m still proud of it and it was one of the first projects that I followed from inception to completion. I’ve created a lot more since this first one, but we’ll save those for future blog posts. What I’ve noticed is that when you’re starting out with programming, you will often put in a heroic amount of effort to produce something that seems, on the surface, unimpressive. However, take your victories and roll with them, because the more you do the unimpressive things the more you will learn to make stuff that is useful.