The world’s leading publication for data science, AI, and ML professionals.

Discover Music’s Mood with expert.ai

Music is a powerful mood modifier: NLP can be applied to lyrics or reviews to classify artists based on their music's mood

Photo by C D-X on Unsplash
Photo by C D-X on Unsplash

It’s no wonder that people create playlists based on how they feel. Music is a very powerful mood modifier and we have all used it, at least once, to give us the energy kick we needed or to pair it with our sorrows. What if we could classify music on the basis of its mood without having to actively listen to it or manually label each song?

Using NLP (Natural Language Processing) capabilities we can classify tracks based on the mood they evoke in the listener, by analyzing either the lyrics or the reviews for a certain artist. Using texts of reviews, we can also classify them from the most positive to the most negative, in order to organize an artist from the top-rated to the least-rated album. Once you have a classification, you can use it to arrange your music and implement an application to load tracks only for a certain pre-defined mood, or you could find new artists that create a mood similar to that of your favorite songs.

In this article, we are going to see how to set up a simple NLP application for such a goal, and we’ll use expert.ai NL API since it provides sentiment and mood classification.


The dataset

For this article, we will be focusing on The Penguin Cafe Orchestra, one of my favorite artists and we will see how you can structure the core of a music-classifying application. We used the reviews provided by Piero Scaruffi’s website, which contains a rich and interesting analysis of many artists for different genres of music. The first step was to copy and paste the album reviews for the chosen artist into text files, saving them with the name of the corresponding album on my local machine. Please notice: if you want to use Scaruffi’s reviews make sure to read the terms of use and reach out to him when in doubt.

What is The Penguin Cafe Orchestra about?

First, let’s see what comes out from the reviews just by analyzing the words used in them. We’ll firstly concatenate all the reviews in one variable, in order to have a single review for the whole artist’s production. Then we are going to take a look at the most frequent words, hoping that they will reveal more on The Penguin Cafe Orchestra:

Let’s see what this shallow-linguistics approach can do on the artist review and produce a word cloud to highlight the key elements for the Orchestra:

Image by Author
Image by Author

As you can see, we now have a more detailed idea of the kind of music they make and we could classify it on the basis of some of the words presented here, such as the genres they mix, the instruments they play, and so on… Unfortunately, no shallow-linguistics approach can give us hints about the mood of a text. We need to use a more refined approach to solve this issue.

How does their music make you feel?

Thanks to the word cloud we know more about The Penguin Cafe Orchestra, but we still have no idea of the mood of their music. We can find out more by looking at what emotions come out of their work through Scaruffi’s reviews. To do so, we are going to use expert.ai NL API. If you haven’t yet, please notice that you can register here and find a 101 guide in my previous article. Once the credentials are created, we can start using expert.ai NL API.

We are going to use the emotional traits taxonomy, which is the emotions-based text classifier. To use it, we need to initialize the client and then set the correct parameters: we choose the emotions taxonomy, set the language for the document, and pass the text to the service. Remember that our text is the collection of all reviews for The Penguin Cafe Orchestra albums. We then obtain each emotion accompanied by its percentage. The higher the number the more predominant that emotion is in the text:

['Happiness', 'Excitement', 'Joy', 'Amusement', 'Love']
[15.86, 31.73, 15.86, 31.73, 4.76]

We can plot this data using matplotlib and produce a pie chart:

Image by Author
Image by Author

Now that we have a classification based on emotions, we could arrange The Penguin Cafe Orchestra albums based on how good the reviews are. A future application could classify music on the basis of mood and then present a playlist containing the best work for each artist as the first result.

What’s their best album?

To classify text based on the opinion the writer expresses, we can use sentiment analysis. This gives us a score: if it’s above 0 it means that the text expresses a positive opinion, if it’s exactly zero (or very close to it) then we can say that the text is neutral, if it’s less than zero it’s negative. Sentiment analysis classifies on a polarity of positive and negative, and should not be confused with the emotions taxonomy, which classifies on a plethora of different emotions.

We can take a look at the sentiment analysis for every album’s review, in order to understand which album has the best review. To do so, we iterate on each album’s review and use expert.ai NL API to retrieve its sentiment and its strength:

[11.6, 2.7, 10.89, 3.9]
['Broadcasting From Home', 'Concert Program', 'Music From the Penguin Cafe', 'Signs of Life']

We can plot this data using matplotlib and produce a bar chart – the higher the bar, the better the album is according to the review:

Image by Author
Image by Author

In the above bar chart, we can easily see that all album reviews are positive, indicating that the reviewer liked every album. We can also see that some are more strongly positive than others and that Broadcasting From Home is the top-scoring album, which means that it’s the best according to the reviewer.

Conclusion

Much of the meaning we come across on a daily basis is conveyed by words: this is certainly true for the emails we receive, and the tweets we write, but it is also true for other topics. Using what we can freely find online, such as lyrics or reviews, we can apply NLP to music, and customize our experience when it comes to something as personal as this.

We hope that you enjoyed this Tutorial and look forward to learning about what you can do using expert.ai NL API.


Related Articles