This is part 3 of a 3 part blog post. This post uses the data that was scraped in part 1 and prepared in part 2.
Now that we have the data in a nice format, let’s make a frequency plot! First let’s load the data and the packages:
library(&quot;tidyverse&quot;)
library(&quot;ggthemes&quot;) # To use different themes and colors
renert_tokenized = readRDS(&quot;renert_tokenized.rds&quot;)
Using the ggplot2 package, I can produce a plot of the most frequent words.
renert_tokenized %__%
count(word, sort = TRUE) %__%
filter(n __ 50) %__%
mutate(word = reorder(word, n)) %__%
ggplot(aes(word, n)) +
geom_col() +
xlab(NULL) +
coord_flip() +
theme_minimal() So, the most frequent word is kinnek, meaning King! kinnek is mentioned more times than renert, the name of the hero. Next are här and wollef meaning mister and wolf. In fifth position we have fuuss, for fox. I’ll let you use Google Translate for the other words 😄.
Now, I’m also doing sentiment analysis by using the AFINN list of words. This list of words have a score that gives its sentiment. You can download the original list from here.
Because such a list is not available in Luxembourguish, I have translated it using Google’s translate api. Here is the code to do that:
library(&quot;tidyverse&quot;)
library(&quot;translate&quot;) # google translate api
library(&quot;tidytext&quot;) # to load the AFINN dictionary
api_key = &quot;api_key_goes_here&quot;
set.key(api_key)
afinn = get_sentiments(&quot;afinn&quot;)
# I wrap the `translate()` function around `purrr::possibly()` so that in case of an
# error, I get the translations that worked back.
possibly_translate = purrr::possibly(translate::translate, otherwise = &quot;error&quot;)
afinn_lux = afinn %__%
mutate(lux = map(word, possibly_translate, source = &quot;en&quot;, target = &quot;lb&quot;)) %__%
mutate(lux = unlist(lux))
write_csv(afinn_lux, &quot;afinn_lux.csv&quot;)
For the above code to work, you need to have a Google cloud account, which you can create for free.
I did not check the quality of the translations, and I’m sure it’s far from perfect. It’s also available on the Github repository here. Again, contributions more than welcome!
Now, I need to merge the dictionary with the data from each song. First, let’s load the dictionary:
afinn_lux = read.csv(&quot;afinn_lux.csv&quot;)
# I only keep the `lux` column (and rename it to word) and the `score column`
afinn_lux = afinn_lux %__%
select(word = lux, score)
How does this dictionary look like? Let’s see:
head(afinn_lux)
## word score
## 1 opzeginn -2
## 2 verloossen -2
## 3 opzeginn -2
## 4 entfouert ginn -2
## 5 entlooss -2
## 6 entfouert -2
Let’s load the tokenized songs, and merge them with the dictionary:
renert_songs_tokenized = readRDS(&quot;renert_songs_tokenized.rds&quot;)
renert_songs_sentiment = map(renert_songs_tokenized, ~full_join(., afinn_lux))
I can now merge the data in a single data frame and do some further cleaning:
renert_songs_sentiment = renert_songs_sentiment %__%
bind_rows() %__%
filter(!is.na(score)) %__%
filter(!is.na(gesank))
What does the final data look like? Here it is:
head(renert_songs_sentiment)
## # A tibble: 6 x 3
## word gesank score
## ## 1 rifft éischte -2
## 2 léiw éischte -3
## 3 fest éischte 2
## 4 fest éischte 2
## 5 räich éischte 2
## 6 räich éischte 3
We see that there are words that are the same, but with different scores. That’s because the translation of the dictionary was most probably not very good. Oh well, let’s do a boxplot of the sentiment for each song:
order = c(&quot;éischte&quot;, &quot;zwete&quot;, &quot;drëtte&quot;, &quot;véierte&quot;, &quot;fënnefte&quot;, &quot;sechste&quot;, &quot;siwente&quot;,
&quot;aachte&quot;, &quot;néngte&quot;, &quot;zéngte&quot;, &quot;elefte&quot;, &quot;zwielefte&quot;, &quot;dräizengte&quot;, &quot;véierzengte&quot;)
renert_songs_sentiment %__%
ggplot(aes(gesank, score)) + scale_x_discrete(limits = order) + geom_boxplot() + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) As we can see, there is no discernible pattern. This can mean two things; either the general sentiment inside each song is fairly neutral, or the the quality of the translation was too bad for the results to make any sense.
That’s it for this series of posts! I hope you enjoyed reading it as much as I enjoyed writing it and analyzing the data!
Don’t hesitate to follow us on twitter @rdata_lu and to subscribe to our youtube channel. You can also contact us if you have any comments or suggestions. See you for the next post!