What Does Your Music Say About You? Python Music Experiment Part 1

Subscribe to BrokenAirplane

I love the last project of the year. I have spent an entire year with these students learning about their interests and  helping them become proficient in math, science, and programming. I like to experiement with the last project and try something new that hopefully will go well but at least can be refined for next year. I was pleasantly surprised about how this final project went and so I thought I would share it with you.

I am always on the lookout for inspiration for projects and this one began 3 years ago with a fascinating article about Pandora (sorry I don't have it anymore). I was so interested in the way the system learned what you like and don't based upon more than just genre. In fact it was based upon over 400 different factors according to the Music Genome Project on which Pandora is based. I thought this year I would have the students analyze music based upon one of those factors, lyrics.

I asked the students to find 20 song lyrics and paste them into a Google Doc. What was interesting is how often I was asked if they could use songs with profanity, violence, etc. I simply responded that I wanted them to choose their all time favorite songs or the ones they listened to the most and if they didn't then the project wouldn't work.

Next we generated some Pseudocode. I asked them if you were to sit a small child down and tell him to write down how many times a certain word came up in a song what would you tell them?

This took about ten minutes of silent thinking, erasures, and debates so don't get the impression that they got it the first time (but who does).  Eventually the class came up with this.

1
2
3
4
Go through the song.
Each time you come to a word, check to see if that word has come up before.
If it has not, write the word down and a one next to it.
If it has, add one to the count/tally.

Next, we began to translate that into Python script. It gave me an opportunity to talk about data and dictionaries which is always a plus but when we were done, they had a big smile on their face as they realized how similar their pseudocode was to the actual code. To test the code as they were building it, we used Michael Jackson's Smooth Criminal as almost everyone knows it and in my opinion it has the most epic music video of all time. It also has some funky lyrics and punctuation that allows for refining the translate function.

The quick tutorial I gave them to learn about strings can be found here, but as I said this project will be refined as will the tutorial.

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from string import *
song = '''Insert Lyrics Here'''

cleanString = song.translate(None,',.:;!?') #removes these characters
cleanList = cleanString.split() #splits it into a list
    
songDict = {}
for word in cleanList:
    if word not in songDict:
            songDict[word] = 1 #if not already in the dictionary, add it
        else:
            songDict[word]+=1 #if already in the dictionary, add 1 to it
    
print word, songDict[word] #option 1

for word in songDict: #option 2
    if songDict[word] > 1:
        print songDict[word]

The first option will print out the entire dictionary without any filtering. The second option was devised by the students after seeing how many times a word came up only once and will create a threshold to only print words that show up more than once (or more if they want). I love it when they come up with a solution like that!

After they saw the output, I asked them to reflect upon what significant words came up the most for them. For some it was "I, me, you, my" etc. which they saw as an indicator of songs about love and intimacy. Others were surprised how often a curse, violent, or derogatory word came up. Since their humanities teacher is teaching them how to write Sonnets and going through Romeo and Juliet, we ran those words through the code as well. Not surprisingly, the words were very intimate since they are both romantic literature.

Feel free to use the code to analyze your own songs and if you have any thoughts for next year's version of this I would love to hear about it in the comments. I hope to incorporate more of Pandora into the next iteration as I was informed that the software uses a form of the distance formula to decide which song to play next. Ha! Take that those who think Algebra is irrelevant.

Code formatting was made possible via Hilite.me. There are other great options out there but this was the quickest for me. It also made it easy for you to copy and paste the code for your own use. If you have a recommendation of how to do it better, please let me know.

Subscribe to BrokenAirplane and keep up with all of the latest education and technology resources!