<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/analysis-of-the-renert-part-3-visualizations/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.blog.rdata.lu/post/2018-01-26-analysis-of-the-renert-part-3/"> rdata.lu Blog | Data science with R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p></p>
<p>
<img src="https://i0.wp.com/www.blog.rdata.lu/images/renert.jpg?w=456" data-recalc-dims="1">
</p>
<p><em>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.</em></p>
<p>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:</p>
<pre><code>library(&quot;tidyverse&quot;)
library(&quot;ggthemes&quot;) # To use different themes and colors
renert_tokenized = readRDS(&quot;renert_tokenized.rds&quot;)</code></pre>
<p>Using the <code>ggplot2</code> package, I can produce a plot of the most frequent words.</p>
<pre><code>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() </code></pre>
<p><img src="https://i2.wp.com/www.blog.rdata.lu/post/2018-01-26-analysis-of-the-renert-part-3_files/figure-html/unnamed-chunk-3-1.png?w=450" data-recalc-dims="1"></p>
<p>So, the most frequent word is <em>kinnek</em>, meaning <em>King</em>! <em>kinnek</em> is mentioned more times than <em>renert</em>, the name of the hero. Next are <em>här</em> and <em>wollef</em> meaning <em>mister</em> and <em>wolf</em>. In fifth position we have <em>fuuss</em>, for <em>fox</em>. I’ll let you use Google Translate for the other words <img alt="😄" src="https://s.w.org/images/core/emoji/2.3/72x72/1f604.png" class="wp-smiley">.</p>
<p>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 <a rel="nofollow" href="http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6010" target="_blank">here</a>.</p>
<p>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:</p>
<pre><code>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;)</code></pre>
<p>For the above code to work, you need to have a Google cloud account, which you can create for free.</p>
<p>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 <a rel="nofollow" href="https://github.com/b-rodrigues/stopwords_lu" target="_blank">here</a>. Again, contributions more than welcome!</p>
<p>Now, I need to merge the dictionary with the data from each song. First, let’s load the dictionary:</p>
<pre><code>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)</code></pre>
<p>How does this dictionary look like? Let’s see:</p>
<pre><code>head(afinn_lux)</code></pre>
<pre><code>##             word score
## 1       opzeginn    -2
## 2     verloossen    -2
## 3       opzeginn    -2
## 4 entfouert ginn    -2
## 5       entlooss    -2
## 6      entfouert    -2</code></pre>
<p>Let’s load the tokenized songs, and merge them with the dictionary:</p>
<pre><code>renert_songs_tokenized = readRDS(&quot;renert_songs_tokenized.rds&quot;)
  
renert_songs_sentiment = map(renert_songs_tokenized, ~full_join(., afinn_lux))</code></pre>
<p>I can now merge the data in a single data frame and do some further cleaning:</p>
<pre><code>renert_songs_sentiment = renert_songs_sentiment %>%
  bind_rows() %>%
  filter(!is.na(score)) %>%
  filter(!is.na(gesank))</code></pre>
<p>What does the final data look like? Here it is:</p>
<pre><code>head(renert_songs_sentiment)</code></pre>
<pre><code>## # A tibble: 6 x 3
##    word  gesank score
##   </code></pre>