Identify violence in music with Artificial Intelligence. Do we hear or listen to music?

An NLP project exploring violent-content detection in song lyrics, using an initial sample of 500 songs labelled by the team.

A learning project shared by the community. The article documents its team, methods and results; it does not establish that the prototype is ready for real-world use.

Identify violence in music with Artificial Intelligence. Do we hear or listen to music?

Latam online. First Edition. 2020

Have you ever stopped to think about what your children listen to?

Have you ever thought about how music influences our society and vice versa?

Currently we are surrounded by a large amount of music that, although it has certain filters and criteria for its publication, sometimes reaches the ears of a certain population that is affected by the message that is transmitted. Nowadays it is no longer necessary to have a record label to promote a song, since access to social networks allows anyone with a cell phone and internet access to record and publish their song, reaching thousands of people in the world, including minors, like your children.

For a song to be heard, there are several distribution channels such as television, radio and social networks, and if there is no adequate filter, all types of songs with different content may be reaching the ears of minors.

Our proposal seeks to identify violent content in song lyrics. We drew on the article ““Violence against women in music: A methodological approach”[1], which discusses violence against women in music and states:

“In the most negative cases, stereotypes are projected that place men and women in different social positions, even going so far as to justify and promote violence against women.”

That is why this project aims to contribute to the fulfillment of one of the seventeen Sustainable Development Goals, particularly Goal 5 on gender equality. We want to help people who listen to music on digital platforms reflect on the messages in songs.

At the start of the project, we imagined an app that could flag possible types of violence in a song’s lyrics using natural language processing (NLP). We proposed the following labels:

Types of violence according to the article “Violence against women in music: A methodological approach”, some icons were obtained from the article “What do we talk about when we talk about violence against women”, from .infojusnoticias.gov.ar

Assessing suitability for children

A “non-violent” label does not guarantee that a song is suitable for children: an adult should also review its message and context.

Data extraction

To do this, a sample of 500 songs began to be analyzed, which were selected from Latin music popularity lists, since the project is proposed for the Spanish-speaking population of Latin America, so the workflow was as follows:

  1. It was found in the rankings of Billboard and Scanner Sound, the list of songs most played with a web scraper, with what was obtained artist and title.

  2. Using the Musixmatch API, we obtained each song’s genre and explicit-language flag. At the time, our access only provided 40% of the lyrics.

  3. The lyrics of the songs were obtained from Google, which is fed by two providers: Musixmatch and LyricFind, with which, finally, the following fields were obtained:

  • Artist
  • Genre
  • Title
  • Whether it is explicit or not (inappropriate language)

Then, the song lyrics were extracted using APIs offered by the most popular music sites (Musixmatch and Billboard) through their developer tools.

Once the lyrics of the songs were obtained, manual labeling was carried out, which was carried out according to the types of violence previously explained. The development team of this project labeled the training categories, resulting in the following data source, its representation can be seen below:

Example of a data set obtained in a first labeling

It is important to note that, because the labeling of each category is subject to the criteria of each team member, the data set could have a bias. It is worth mentioning that this stage of the project is a proof of concept that will help us validate the feasibility of carrying out automatic labeling according to the stated objective. Furthermore, upon observing that the number of positives in each category was not enough for the algorithm to learn well, it was decided to add an additional category, called 'class', which indicates whether it has violent content. This change within scope will be addressed later.

EDA

We then carried out exploratory data analysis (EDA) with pandas, matplotlib, seaborn and Plotly. We examined the class distribution before preprocessing and prepared a corpus for language analysis.

Our initial exploratory data analysis (EDA) revealed the following patterns:

Analysis/distribution of types of violence

Bar graph showing the number of incidents in each class of the data set

In this bar graph we can observe the distribution of the types of violence contained in the selected music, in this case, the greatest predominance are musical genres of reggaeton, regional Mexican and pop in Spanish because the most listened to songs of the moment were obtained, however, this information will not be used for the algorithm since it could create an important bias, as can be seen in the histogram according to the genres. It is worth mentioning that this analysis was done before normalizing the data to have an overview of how the data is distributed.

Bar graph showing how the types of violence are distributed by musical genre.

A heat map was made to discover how the types of violence are related within the songs that were considered.

After analyzing the types of violence, we consider that separating the number of songs into the seven selected categories would not be enough to make a good classification since we have relatively few positives in each category. Based on this observation, for a first phase, the separation will be carried out only into 2 categories: “violent” and “non-violent”. Something very important to highlight is that even if a song does not contain violence, it does not mean that it is suitable for infants, because it could touch on topics not suitable for certain ages.

Analysis with word maps with and without the violence label

In a second approach, during the preprocessing of the data, word maps were obtained, like a second EDA, in order to identify the most frequent words in violent and non-violent content and recognize some “stopwords” that we must consider or, if applicable, words that should be lemmatized.

The preprocessing of the data, in this case, was carried out as follows:

  1. Normalization of song data/lyrics (accents, capital letters, punctuation marks and elimination of words like 'oh', 'yeah', 'ma', etc.)

  2. Word tokenization

  3. Stopword removal

  4. Lemmatization (in a next stage it is considered to see if 'stemming' could help obtain better results)

  5. Vectorization of the songs (Bag of Words is used in this stage of the project).

After preprocessing, we generated word clouds again. The Spanish words “haber”, “querer”, “hacer” and “tener” dominated both classes, so we added them to our stopword list and repeated the analysis. This is a task-specific choice: removing frequent words can also discard context.

map of “non-violence” words before removing very common stop words

map “with violence” words before removing very common stop words

map of “non-violence” words after removing very common stop words

map of “with violence” words after removing very common stop words

As can be seen, both classifications still have some words in common such as “say” and “know.” However, different words can be seen on the map of the songs 'with violence', such as "forget", "die", "lose", "leave", etc.

Once the corpus was ready and represented as a bag of words, we used the labels “violent” and “non-violent”, based on the team’s annotation criteria.

Training and model selection

It was tested with different algorithms, after applying the 'bag of words' representation and using the lemmatization technique with the nltk tools, for the models the sklearn libraries such as Naive Bayes, Random Forest, Decision Tree, SVM and SGD were used.

For each model, we reviewed the classification report, accuracy, F1 score and confusion matrix.

Results obtained from the classifiers, confusion matrix and metrics obtained with the sklearn and seaborn libraries

We also used ROC curves to compare model behaviour visually across different thresholds.

ROC curves of the models obtained with the sklearn and matplotlib libraries

ROC curves of the models

As seen in the ROC curve graph, the models that had the best performance were 'Random Forest' and 'Naïve Bayes', which have very similar results. However, if we look at the confusion matrices and the 'F1 Score' we can conclude that with the data we have, 'Naïve Bayes' is the model that has the best performance at this stage of the project. Since for us it is better to have a violent label even if it does not have one, than for a song with violence to be wrongly classified and reach minors. That is, in technical terms it is better for us to have a type I error (false positives) than a type II error (False negatives).

Project scalability

With the available data, we trained a binary classifier: “violent” or “non-violent”. We did not have enough examples per category to model the different types of violence separately. With more labelled data, we could explore three or four categories and assess whether they can be distinguished reliably.

If good results are obtained, an API could be created for use in mobile applications or as a plug-in, in order to facilitate the recognition of a song and know if said song has violent content or not.

Knowing in advance whether or not a song has violent content will help empower users and reflect on the impact that what is heard in popular culture has on our lives and the culture of the population.

The project also proposed exploring its use in studying “media violence”, a concept associated with the reforms known as Ley Olimpia in Mexico. The cited news report, published in November 2020 [2], summarises the following conduct; the classifier cannot determine by itself whether a song violates the law:

Any act that through any means of communication:

-Promote sexist stereotypes.

-Advocate violence against women and girls.

-Produces or allows the production and dissemination of sexist hate speech.

-Promotes gender discrimination or inequality between women and men.

-Cause harm to women and girls of a psychological, sexual, physical, economic, patrimonial or femicidal nature.

Example of API with the logo of the development team

Glossary:

NLP: natural language processing, the field of AI concerned with how computers process human language.

Corpus (pl. corpora): A linguistic corpus is a large and structured set of real examples of language use. These examples can be texts (the most common), or oral samples (generally transcribed). [3]

Lemmatization: Maps an inflected word to its lemma, the form typically used as a dictionary entry.

Stopword: Very common words that are not very informative from a lexical point of view, such as conjunctions (and, or, nor, what), prepositions (a, en, para, por, among others) and very common verbs (ser, ir, and others).[4]

References:

Members

  • Álvarez Leandro
  • Cuadros Alejandra
  • Morales Leobardo
  • Ramírez Héctor
  • Samaniego Luis

Project presentation: DemoDay

Open video or resource

Repository

In the following repository you will find the code used to develop this application:https://github.com/SaturdaysAI/Projects/tree/master/LATAM_remote/NLP_Violencia-en-musica–master

More artificial intelligence!

Saturdays.AI makes AI more accessible through shared learning and projects with a social purpose. Our motto is AI4ALL IS AI4GOOD.

Explore the available courses

Meet the Saturdays.AI community

FROM CURIOSITY TO PRACTICE

Your next step starts here.

Put an idea into practice or find people to continue learning with.

Receive new stories in your RSS reader ↗

A story from the Saturdays.AI community. Originally published on March 16, 2022.

← More community stories