<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/sentiment-analysis-of-a-christmas-carol/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/11/29/sentiment-analysis-of-a-christmas-carol/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Our family has been reading, listening to and watching “A Christmas Carol” for just abt 30 years now. I got it into my crazy noggin to perform a sentiment analysis on it the other day and tweeted out the results, but a large chunk of the R community is not on Twitter and it would be good to get a holiday-themed post or two up for the season.</p>
<p>One reason I embarked on this endeavour is that <a rel="nofollow" href="http://twitter.com/juliasilge" target="_blank">@juliasilge</a> &amp; <a rel="nofollow" href="http://twitter.com/drob" target="_blank">@drob</a> made it so gosh darn easy to do so with:</p>
<p><a rel="nofollow" href="http://amzn.to/2nfdwF3" target="_blank"><img alt="" src="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/11/51qE8dW8ZkL._SX379_BO1204203200_.jpg?resize=381%2C499&ssl=1" class="wp-image-7338" data-recalc-dims="1"></a></p>
<p>(btw: That makes an <em>excellent</em> <a rel="nofollow" href="http://amzn.to/2nfdwF3" target="_blank">holiday gift</a> for the data scientist[s] in your life.)</p>
<p>Let us begin!</p>
<h2>STAVE I: hrbrmstr’s Code</h2>
<p>We need the text of this book to work with and thankfully it’s long been in the public domain. As <a rel="nofollow" href="http://twitter.com/drob" target="_blank">@drob</a> noted, we can use the <code>gutenbergr</code> package to retrieve it. We’ll use an RStudio project structure for this and cache the results locally to avoid burning bandwidth:</p>
<pre><code>library(rprojroot)
library(gutenbergr)
library(hrbrthemes)
library(stringi)
library(tidytext)
library(tidyverse)

rt &lt;- find_rstudio_root_file()

carol_rds &lt;- file.path(rt, &quot;data&quot;, &quot;carol.rds&quot;)

if (!file.exists(carol_rds)) {
  carol_df &lt;- gutenberg_download(&quot;46&quot;)
  write_rds(carol_df, carol_rds)
} else {
  carol_df &lt;- read_rds(carol_rds)
}</code></pre>
<p>How did I know to use <code>46</code>? We can use <code>gutenberg_works()</code> to get to that info:</p>
<pre><code>gutenberg_works(author==&quot;Dickens, Charles&quot;)
## # A tibble: 74 x 8
##    gutenberg_id                                                                                    title
##           </code></pre>