<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/text-mining-with-r-on-vikings-episode-scripts/"></div></div><div><a data-url="http://www.r-bloggers.com/text-mining-with-r-on-vikings-episode-scripts/" data-counturl="http://www.r-bloggers.com/text-mining-with-r-on-vikings-episode-scripts/" data-count="horizontal" data-text="Text Mining with R on Vikings episode scripts" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.networkx.nl/data-science/text-mining-r-vikings/"> R – Networkx</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="vikings" src="http://i1.wp.com/www.networkx.nl/wp-content/uploads/2016/07/vikings.png?w=456" data-recalc-dims="1"></p>
<h3>Synopsis</h3>
<p>I'm a hugh fan of the TV show Vikings. I thought it would be cool to mine the tv shows scripts to figure out which terms are the most used in the show and what the correlations are between the most frequent terms and episodes.</p>
<p>Who do not know this serie here is some information of <a target="_blank" href="http://www.thetvdb.com/?tab=series&id=260449&lid=7">Vikings</a></p>
<h3>Getting the data</h3>
<p>Before we can start we need to get the data. I have found a website with a lots of tv and movie scripts. All the scripts are embed in HTML code that we must extract. R has a package rvest for this and we will use it to get our data.</p>
<p>Rvest is a library that easily harvest (Scrape) web pages <a target="_blank" href="https://cran.r-project.org/web/packages/rvest/index.html">rvest</a>.</p>
<pre># Loading the required libraries
library(rvest)</pre>
<pre>## Loading required package: xml2</pre>
<p>The script is written as we can harvest another tv show if we want only with setting it to another tv show. </p>
<p>Setting our variables, such as, which tv show we want, download directory, base urls etc.</p>
<pre># Which tv show, if you want another show, first check on the website which tv show url is used. 
tvshow &lt;- &quot;vikings&quot;

# Creating download directory and change to it
directory = paste(&quot;~/Data Analysis/files/&quot;, tvshow, sep=&quot;&quot;)
dir.create(directory, recursive = TRUE, showWarnings = FALSE)
setwd(directory)

# Setting base url and complte url
baseurl &lt;- &quot;http://www.springfieldspringfield.co.uk/&quot;
url &lt;- paste(baseurl,&quot;episode_scripts.php?tv-show=&quot;, tvshow, sep=&quot;&quot;)</pre>
<p>There are 4 seasons with 10 episodes each of Vikings, it do not want to scrape it piece by peice, so first we scrape all episode urls before we can downloding the scripts. Before that we need to explorer the websites source for which nodes to select. </p>
<p>Go to the urls you want to scrape, in my case <a target="_blank" href="http://www.springfieldspringfield.co.uk/episode_scripts.php?tv-show=vikings">this</a>. In case of a Chrome browser right click the first epidose of season 1 and select inspect. You wil see the following:</p>
<p><img title="Inspect Vikings" alt="Chrome Inspect" src="http://i0.wp.com/www.networkx.nl/wp-content/uploads/2016/06/inspect-vikings.png?w=456" data-recalc-dims="1"></p>
<p>As you can see after the tag <code>&lt;h3></code> the first href tag of s01e01 with the class <code>class=&quot;season-episode-title&quot;</code>. This class we need to select as our node.</p>
<pre># read the HTML page
scrape_url &lt;- read_html(url)
# node selector
s_selector &lt;- &quot;.season-episode-title&quot;

# scrape href nodes in .season-episode-title
all_urls_season &lt;- html_nodes(scrape_url, s_selector) %>%
  html_attr(&quot;href&quot;)</pre>
<p>Show some structure of the all_url_seasons.</p>
<pre>str(all_urls_season)</pre>
<pre>##  chr [1:40] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s01e01&amp;quot; ...</pre>
<pre>head(all_urls_season)</pre>
<pre>## [1] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s01e01&amp;quot;
## [2] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s01e02&amp;quot;
## [3] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s01e03&amp;quot;
## [4] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s01e04&amp;quot;
## [5] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s01e05&amp;quot;
## [6] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s01e06&amp;quot;</pre>
<pre>tail(all_urls_season)</pre>
<pre>## [1] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s04e05&amp;quot;
## [2] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s04e06&amp;quot;
## [3] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s04e07&amp;quot;
## [4] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s04e08&amp;quot;
## [5] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s04e09&amp;quot;
## [6] &amp;quot;view_episode_scripts.php?tv-show=vikings&amp;amp;episode=s04e10&amp;quot;</pre>
<p>As we can see in the structure display we have 40 urls of the episodes. Now we have all variables and season urls, we can harvest the scripts and save them to seperate text files for doing our text mining.</p>
<pre># Loop through all season urls 
for (i in all_urls_season) {
  uri &lt;- read_html(paste(baseurl, i, sep=&quot;/&quot;))
  # same thing here first check which node we need to select, so forst do a inspect of the site
  script_selector &lt;- &quot;.scrolling-script-container&quot;
  # scrape all script text to a variable
  text &lt;- html_nodes(uri, script_selector) %>% 
    html_text()

  # Get last five characters of all_urls_season as season for saving this to seperate text files
  substrRight &lt;- function(x, n) {
    substr(x, nchar(x)-n+1, nchar(x))
  }
  seasons &lt;- substrRight(i, 5)
  # Write each script to a seperate text file
  write.csv(text, file = paste(directory, &quot;/&quot;, tvshow, &quot;_&quot;, seasons, &quot;.txt&quot;, sep=&quot;&quot;), row.names = FALSE)
}</pre>
<h3>Starting with Text Mining</h3>
<p>Now that we have all Viking episode scripts we can do some text mining with the tm library.</p>
<pre># load library
library(tm)</pre>
<pre>## Loading required package: NLP</pre>
<pre># set filepath to scripts
cname &lt;- file.path(directory)
# see if the filepath contains our scripts
(docname &lt;- dir(cname))</pre>
<pre>##  [1] &amp;quot;vikings_01e01.txt&amp;quot; &amp;quot;vikings_01e02.txt&amp;quot; &amp;quot;vikings_01e03.txt&amp;quot;
##  [4] &amp;quot;vikings_01e04.txt&amp;quot; &amp;quot;vikings_01e05.txt&amp;quot; &amp;quot;vikings_01e06.txt&amp;quot;
##  [7] &amp;quot;vikings_01e07.txt&amp;quot; &amp;quot;vikings_01e08.txt&amp;quot; &amp;quot;vikings_01e09.txt&amp;quot;
## [10] &amp;quot;vikings_02e01.txt&amp;quot; &amp;quot;vikings_02e02.txt&amp;quot; &amp;quot;vikings_02e03.txt&amp;quot;
## [13] &amp;quot;vikings_02e04.txt&amp;quot; &amp;quot;vikings_02e05.txt&amp;quot; &amp;quot;vikings_02e06.txt&amp;quot;
## [16] &amp;quot;vikings_02e07.txt&amp;quot; &amp;quot;vikings_02e08.txt&amp;quot; &amp;quot;vikings_02e09.txt&amp;quot;
## [19] &amp;quot;vikings_02e10.txt&amp;quot; &amp;quot;vikings_03e01.txt&amp;quot; &amp;quot;vikings_03e02.txt&amp;quot;
## [22] &amp;quot;vikings_03e03.txt&amp;quot; &amp;quot;vikings_03e04.txt&amp;quot; &amp;quot;vikings_03e05.txt&amp;quot;
## [25] &amp;quot;vikings_03e06.txt&amp;quot; &amp;quot;vikings_03e07.txt&amp;quot; &amp;quot;vikings_03e08.txt&amp;quot;
## [28] &amp;quot;vikings_03e09.txt&amp;quot; &amp;quot;vikings_03e10.txt&amp;quot; &amp;quot;vikings_03e90.txt&amp;quot;
## [31] &amp;quot;vikings_04e01.txt&amp;quot; &amp;quot;vikings_04e02.txt&amp;quot; &amp;quot;vikings_04e03.txt&amp;quot;
## [34] &amp;quot;vikings_04e04.txt&amp;quot; &amp;quot;vikings_04e05.txt&amp;quot; &amp;quot;vikings_04e06.txt&amp;quot;
## [37] &amp;quot;vikings_04e07.txt&amp;quot; &amp;quot;vikings_04e08.txt&amp;quot; &amp;quot;vikings_04e09.txt&amp;quot;
## [40] &amp;quot;vikings_04e10.txt&amp;quot;</pre>
<pre># Crete a Corpus of the text files so we can do some analysis
docs &lt;- Corpus(DirSource(cname), readerControl = list(id=docname))
# Show summary of the Corpus, we have 40 document in our Corpus
summary(docs)</pre>
<pre>##                   Length Class             Mode
## vikings_01e01.txt 2      PlainTextDocument list
## vikings_01e02.txt 2      PlainTextDocument list
## vikings_01e03.txt 2      PlainTextDocument list
## vikings_01e04.txt 2      PlainTextDocument list
## vikings_01e05.txt 2      PlainTextDocument list
## vikings_01e06.txt 2      PlainTextDocument list
## vikings_01e07.txt 2      PlainTextDocument list
## vikings_01e08.txt 2      PlainTextDocument list
## vikings_01e09.txt 2      PlainTextDocument list
## vikings_02e01.txt 2      PlainTextDocument list
## vikings_02e02.txt 2      PlainTextDocument list
## vikings_02e03.txt 2      PlainTextDocument list
## vikings_02e04.txt 2      PlainTextDocument list
## vikings_02e05.txt 2      PlainTextDocument list
## vikings_02e06.txt 2      PlainTextDocument list
## vikings_02e07.txt 2      PlainTextDocument list
## vikings_02e08.txt 2      PlainTextDocument list
## vikings_02e09.txt 2      PlainTextDocument list
## vikings_02e10.txt 2      PlainTextDocument list
## vikings_03e01.txt 2      PlainTextDocument list
## vikings_03e02.txt 2      PlainTextDocument list
## vikings_03e03.txt 2      PlainTextDocument list
## vikings_03e04.txt 2      PlainTextDocument list
## vikings_03e05.txt 2      PlainTextDocument list
## vikings_03e06.txt 2      PlainTextDocument list
## vikings_03e07.txt 2      PlainTextDocument list
## vikings_03e08.txt 2      PlainTextDocument list
## vikings_03e09.txt 2      PlainTextDocument list
## vikings_03e10.txt 2      PlainTextDocument list
## vikings_03e90.txt 2      PlainTextDocument list
## vikings_04e01.txt 2      PlainTextDocument list
## vikings_04e02.txt 2      PlainTextDocument list
## vikings_04e03.txt 2      PlainTextDocument list
## vikings_04e04.txt 2      PlainTextDocument list
## vikings_04e05.txt 2      PlainTextDocument list
## vikings_04e06.txt 2      PlainTextDocument list
## vikings_04e07.txt 2      PlainTextDocument list
## vikings_04e08.txt 2      PlainTextDocument list
## vikings_04e09.txt 2      PlainTextDocument list
## vikings_04e10.txt 2      PlainTextDocument list</pre>
<pre># Inspect the first document, it has 12958 characters
inspect(docs[1])</pre>
<pre>## &amp;lt;&amp;lt;VCorpus&amp;gt;&amp;gt;
## Metadata:  corpus specific: 0, document level (indexed): 0
## Content:  documents: 1
## 
## [[1]]
## &amp;lt;&amp;lt;PlainTextDocument&amp;gt;&amp;gt;
## Metadata:  7
## Content:  chars: 12943</pre>
<p>There is a lot of information in the script we do not need and is not useful for text mining. We need to clean it up. We remove all numbers, convert text to lowercase, remove punctuation and stopwords, in this case english.</p>
<pre>docs &lt;- tm_map(docs, tolower)
docs &lt;- tm_map(docs, removePunctuation)
docs &lt;- tm_map(docs, removeNumbers)
docs &lt;- tm_map(docs, removeWords, stopwords(&quot;english&quot;))</pre>
<p>Now we will perform stemming, a stem is a form to which affixes can be attached. An example of this is wait, waits, waited, waiting, all of them are common to wait. </p>
<pre>library(SnowballC)
docs &lt;- tm_map(docs, stemDocument)</pre>
<p>We have removed a lot of characters which resulted in a lot of whitespaces, we remove this also.</p>
<pre>docs &lt;- tm_map(docs, stripWhitespace)</pre>
<p>Let's have a look to our first document.</p>
<pre>inspect(docs[1])</pre>
<pre>## &amp;lt;&amp;lt;VCorpus&amp;gt;&amp;gt;
## Metadata:  corpus specific: 0, document level (indexed): 0
## Content:  documents: 1
## 
## [[1]]
## [1] x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
## [2]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
## [3]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
## [4]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
## [5]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
## [6]  ragnar odin ragnar give give heart love voice sing night wake ill see tomorrow brings voice sing come lets go home watch two im taking bjorn thing tomorrow yet hes old enough hes twelve years old take next year next year soon enough hell need silk ribbon lagertha youre handsome boy funny ears though dont sleep lots women kattegat can without days another way saying love huh always dream dreamt feeding blood pudding mean means youre giving heart get dog boy frodi come boy earl will deal criminal fences well discuss summer raids will go earl decides owns ships hell send us east always baltic lands want know west cities gods see im satisfied odin gave eye acquire knowledge give far will earl haraldson let good like nice tight three one two three good gyda gyda feed goats go know youre alone men gone youre thirsty will give drink youre hungry will feed otherwise must go weve satisfied needs dont want kill woman couldnt kill tried hundred years went confess love set upon bear enormous hound guarded home killed bear spear managed strangle hound bare hands gained hand marriage tell story just ready receive arm ring become man yes man fights looks family thats right look family mean look us say wasnt big decision make may change many things now go sleep big day tomorrow bjorn brother frey gods youve grown hello rollo thing youre real man now come lets get drink looking little pale bjorn dont go lie think earl going send us year bastards east poor know thats sail west ive heard tales rollo great towns cities treasures hordes gold silver new god ive heard stories mean cant sail across open ocean believe theres way go west something will change everything now just listen back met someone wanderer told possible go west open sea using sunboard needs sit water another wanderer now tell ragnar thisthis wanderer ever sailed west just get water now candle sun every day sun rises sky noon see shadow shortens noon shortest tells far south travelled didnt just tell listen mark circle around pointer shadow shortest noon next day sea place board back water around noon watch shadow shadow touches circle course remains true yes passes outside circle like drifted must steer south yes never reaches circle far south must steer north thats keep noon shadow circle course will true west sun can board help find way using follow outside call sunstone see sun now go west come come silence silence olaf anwend pleaded guilty charge stealing tomorrow will run gauntlet stones turf punishment yes lord thank lord let known fine paid anyone fails throw something bring next accused murderer eric trygvasson accused murder sigvald strut january wasnt murder lord killed selfdefense liar first person met afterwards law obliges fact passed several houses reported thought relatives dead man might living law allows pass two houses circumstances murdered brother cold blood true argued disputed land wanted land youre liar coward says im coward im coward silence normal procedures followed killing atoned compensating victims family murder dishonorable deed amongst people carried secret unacknowledged likely give rise series revenge killings involve family lord knew land knew claim enough ask look accused think guilty raise arms decision must unanimous eric trygvasson found guilty murder yes justice wish die beheading lord wish granted shall executed tomorrow will shall feast talk summer raids yeah yeah smiling father wants die well without fear atone sins must watch sake hope reaching valhalla feed pigs curse may never enter valhalla may never feast gods done someone told wanted land knew trygvasson best claim refused sell hear boy things done around olaf son ingolf bjorn son ragnar may accept gift salt earth belong land sea arm rings bind loyalty lord chieftain oath swear rings must honored kept understand swear yes lord freely give fealty lord chieftain yes lord good may put arm rings come wooooo let us feast lord want feast also want know will raiding summer cant wait ragnar tell us want know right know well will raid east eastlands russia every year go places alternative choose yes yes choice yes heard rumors stories travel west will somehow reach land rich plentiful tell will risk ships reputation deluded fantasy ships pay go tell go now thats end matter lets feast let see fine ring ragnar lothbrok earl haraldson wants speak private stay uncle ragnar lothbrok sit hungry yes lord want feast hall want sail ships anything else want keep talking west know land great riches cant sure believe dont care believe insulted first time believe will last told go farmer content lot farms great demand many people like possess land understand im saying understand dont ever stick nose face dont trust watch sons said youd found going now im tired talk gods dont come im waiting sit want want know gods store boy im interested gods desire great future see can withdraw goodwill time great future must challenge law must convince gods alter runes work favor laws men far workings shapings gods take laws men hands answer already answer dont well go ask gods afraid wait outside right havent helped ancient one perhaps asked wrong questions someone special visit name floki floki like loki god yes different different hes god didnt come thing hes shy floki son bjorn hello well thank sir let see fathers eyes unfortunately unfortunately means will like therefore will want better will hate can tell just looking face trees can tell trees will make best planks just looking can look inside tree floki boatbuilder among things one inside tree two almost perfect planks will bend curve like womans body thighs back split tree will find can see think im joking joke many things son ragnar never shipbuilding imagine ships just dead things boat will lighter carry bigger sail construction different built strong central plank two strakes nailed directly onto knees frame ones look cleated lashed onto frames can move relation means boat wont butt waves like goat move like ripple hull deeper will men set oars will cut sheerstrakes ports can closed boat sea think handle long sea voyages thats im building will strong enough wont know try anchor left last summers raids dont worry well soon rich dwarves missed anything happen away miss ached love longing belly empty laughter want want make laugh dont want laugh now want ride like bull like wild bull hello young bjorn hello rollo parents sex well guess well wait gyda mother teaching use shield yes know use shield mother famous shieldmaiden famous shieldmaiden come children bed lets leave men im man ring let stay awhile bed say good night good night children good night hey tell news boat nearly ready wont go command wont go unless equal brothers will always equal must find crew many men will go wishes haraldson many will afraid may even go betray us must go piss yesterday girl town thank goodlooking girl shouted pleasure didnt see face saw lagertha dont talk like think time thats bad dont insult shieldmaiden never insult youre great warrior perhaps great man saw something see sign made certain right thing good night brother set sail floki sail shell sink shouldnt pretended build boat beyond humble capabilities ill set sail floki im sorry ragnar ive wasted money joke shut man ha now runs cool keel beautiful didnt believe told now ragnar lothbrok</pre>
<p>I have hash it because wordpress has problems with editing the post.</p>
<p>We are ready with preprosessing the data and turn the document back as plain text documents.</p>
<pre>docs &lt;- tm_map(docs, PlainTextDocument)</pre>
<p>Create a Term Document Matrix of our documents. Which reflects the number of times each term in the corpus is found in each of the documents. And add some readable columnnmes.</p>
<pre># Create a tdm
tdm &lt;- TermDocumentMatrix(docs)
# Add readable columnnames, in our case the document filename
docname &lt;- gsub(&quot;vikings_&quot;, &quot;&quot;,docname)
docname &lt;- gsub(&quot;.txt&quot;, &quot;&quot;,docname)
docname &lt;- paste(&quot;s&quot;,docname, sep=&quot;&quot;)
colnames(tdm) &lt;- docname
# Show and inspect the tdm
tdm</pre>
<pre>## &amp;lt;&amp;lt;TermDocumentMatrix (terms: 5608, documents: 40)&amp;gt;&amp;gt;
## Non-/sparse entries: 22220/202100
## Sparsity           : 90%
## Maximal term length: 17
## Weighting          : term frequency (tf)</pre>
<pre>inspect(tdm[1:10,1:6])</pre>
<pre>## &amp;lt;&amp;lt;TermDocumentMatrix (terms: 10, documents: 6)&amp;gt;&amp;gt;
## Non-/sparse entries: 2/58
## Sparsity           : 97%
## Maximal term length: 10
## Weighting          : term frequency (tf)
## 
##             Docs
## Terms        s01e01 s01e02 s01e03 s01e04 s01e05 s01e06
##   abandon         0      0      0      0      0      0
##   abandoned       0      0      0      0      0      0
##   abandoning      0      0      0      0      0      0
##   abandons        0      0      0      0      0      0
##   abbot           0      0      0      1      0      0
##   abdication      0      0      0      0      0      0
##   abed            0      0      0      0      0      0
##   abide           0      0      0      0      0      0
##   abl             0      0      0      0      0      0
##   able            0      0      0      0      0      1</pre>
<p>Do the same for a Document Term Matrix (this is a transpose of a tdm)</p>
<pre>dtm &lt;- DocumentTermMatrix(docs)
rownames(dtm) &lt;- docname
dtm</pre>
<pre>## &amp;lt;&amp;lt;DocumentTermMatrix (documents: 40, terms: 5608)&amp;gt;&amp;gt;
## Non-/sparse entries: 22220/202100
## Sparsity           : 90%
## Maximal term length: 17
## Weighting          : term frequency (tf)</pre>
<pre>inspect(dtm[1:10,1:6])</pre>
<pre>## &amp;lt;&amp;lt;DocumentTermMatrix (documents: 10, terms: 6)&amp;gt;&amp;gt;
## Non-/sparse entries: 2/58
## Sparsity           : 97%
## Maximal term length: 10
## Weighting          : term frequency (tf)
## 
##         Terms
## Docs     abandon abandoned abandoning abandons abbot abdication
##   s01e01       0         0          0        0     0          0
##   s01e02       0         0          0        0     0          0
##   s01e03       0         0          0        0     0          0
##   s01e04       0         0          0        0     1          0
##   s01e05       0         0          0        0     0          0
##   s01e06       0         0          0        0     0          0
##   s01e07       0         0          0        0     0          0
##   s01e08       0         0          0        0     0          0
##   s01e09       0         0          0        0     0          0
##   s02e01       1         0          0        0     0          0</pre>
<p>Now we have done that we can ask questions about, what are de most frequently terms in the scripts and what are the associations between terms.</p>
<h3>Term frequency</h3>
<p>Let have a look of the most frequently terms first and show the top 20.</p>
<pre>freq &lt;- sort(colSums(as.matrix(dtm)), decreasing=TRUE)
head(freq,20)</pre>
<pre>##   will ragnar   dont   know   want    can   come    man    now   king 
##    806    467    387    373    354    342    335    300    290    264 
##    see   gods    yes   like   must    one father   well    god    son 
##    256    247    245    237    231    231    221    193    191    187</pre>
<h3>Plotting the terms frequencies</h3>
<p>Add is to a data frame so we can plot it and show the top 20.</p>
<pre>tf &lt;- data.frame(term=names(freq), freq=freq)   
head(tf,20)</pre>
<pre>##          term freq
## will     will  806
## ragnar ragnar  467
## dont     dont  387
## know     know  373
## want     want  354
## can       can  342
## come     come  335
## man       man  300
## now       now  290
## king     king  264
## see       see  256
## gods     gods  247
## yes       yes  245
## like     like  237
## must     must  231
## one       one  231
## father father  221
## well     well  193
## god       god  191
## son       son  187</pre>
<p>Let's plot it.</p>
<pre># descending sort of teh tf by freq
tf$term &lt;- factor(tf$term, levels = tf$term[order(-tf$freq)])
library(ggplot2)</pre>
<pre>## 
## Attaching package: &amp;#39;ggplot2&amp;#39;
## 
## The following object is masked from &amp;#39;package:NLP&amp;#39;:
## 
##     annotate</pre>
<pre>p &lt;- ggplot(subset(tf, freq>200), aes(term, freq))    
p &lt;- p + geom_bar(stat=&quot;identity&quot;)   
p &lt;- p + theme(axis.text.x=element_text(angle=45, hjust=1))   
p</pre>
<p><img alt="plot of chunk unnamed-chunk-16" src="http://i2.wp.com/www.networkx.nl/wp-content/uploads/2016/07/unnamed-chunk-16-1.png?w=456" data-recalc-dims="1"> </p>
<p>The most frequent term is <code>will</code>, after that is the main actor <code>ragnar</code>.</p>
<h3>Further Analysis</h3>
<p>As we can see in our first look at the tdm, we have a lot op sparse terms in our documents (90%). That is a lot, lets remove these.</p>
<pre>tdm.common = removeSparseTerms(tdm, sparse = 0.1)
tdm</pre>
<pre>## &amp;lt;&amp;lt;TermDocumentMatrix (terms: 5608, documents: 40)&amp;gt;&amp;gt;
## Non-/sparse entries: 22220/202100
## Sparsity           : 90%
## Maximal term length: 17
## Weighting          : term frequency (tf)</pre>
<pre>tdm.common</pre>
<pre>## &amp;lt;&amp;lt;TermDocumentMatrix (terms: 36, documents: 40)&amp;gt;&amp;gt;
## Non-/sparse entries: 1391/49
## Sparsity           : 3%
## Maximal term length: 6
## Weighting          : term frequency (tf)</pre>
<p>That is a 87% less sparsity. See how many terms we had and now have.</p>
<pre>dim(tdm)</pre>
<pre>## [1] 5608   40</pre>
<pre>dim(tdm.common)</pre>
<pre>## [1] 36 40</pre>
<p>Hmm from 5608 terms to only 36 terms, we inspect the first 10 terms of the first 6 documents.</p>
<pre>inspect(tdm.common[1:10,1:6])</pre>
<pre>## &amp;lt;&amp;lt;TermDocumentMatrix (terms: 10, documents: 6)&amp;gt;&amp;gt;
## Non-/sparse entries: 59/1
## Sparsity           : 2%
## Maximal term length: 6
## Weighting          : term frequency (tf)
## 
##         Docs
## Terms    s01e01 s01e02 s01e03 s01e04 s01e05 s01e06
##   also        1      1      1      0      2      4
##   away        1      2      4      4      1      2
##   can         9      7      6      5      8      7
##   come        9     10     12      5      8      9
##   dont       12     14     11     13     12     10
##   father      1      4      4      6     12      3
##   gods        9      6      3      9      9      4
##   good        7      8      3      3      3      2
##   great       7      2      5      4      3      2
##   just        7      7      6      6      3      2</pre>
<p>Let visualize these most common terms in a heatmap with ggplot. As ggplot works with a matrix we need to convert the <code>tdm.comon</code> to a matrix because the tdm is a spare matrix.</p>
<pre>tdm.dense &lt;- as.matrix(tdm.common)
dim(tdm.dense)</pre>
<pre>## [1] 36 40</pre>
<p>We need the data as a normal matrix in order to produce the visualisation. </p>
<pre>library(reshape2)
tdm.dense.m &lt;- melt(tdm.dense, value.name = &quot;count&quot;)
head(tdm.dense.m)</pre>
<pre>##    Terms   Docs count
## 1   also s01e01     1
## 2   away s01e01     1
## 3    can s01e01     9
## 4   come s01e01     9
## 5   dont s01e01    12
## 6 father s01e01     1</pre>
<p>Make the heatmap visualization.</p>
<pre>library(ggplot2)
ggplot(tdm.dense.m, aes(x = Docs, y = Terms, fill = log10(count))) +
     geom_tile(colour = &quot;white&quot;) +
     scale_fill_gradient(high=&quot;steelblue&quot; , low=&quot;white&quot;)+
     ylab(&quot;&quot;) +
     theme(panel.background = element_blank()) +
     theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))</pre>
<p><img alt="plot of chunk unnamed-chunk-22" src="http://i2.wp.com/www.networkx.nl/wp-content/uploads/2016/07/unnamed-chunk-22-1.png?w=456" data-recalc-dims="1"> </p>
<p>Okay what have we here? Now we can check which common terms are used in which season and episode of Vikings. The term <code>will</code> is common used in the most episodes excepts in s03e90. That's a strange episode, isn't it? (I've look that up, it no realy an episode it's a documentary of Vikings <img alt="🙂" class="wp-smiley" src="http://i2.wp.com/s.w.org/images/core/emoji/72x72/1f642.png?w=456&ssl=1" data-recalc-dims="1"> )</p>
<p>Now we plot a correlogram of the episodes.</p>
<p>Note: Correlogram is a graph of correlation matrix. It is very useful to highlight the most correlated variables in a data table. In this plot, correlation coefficients is colored according to the value. Correlation matrix can be also reordered according to the degree of association between variables. </p>
<pre>corr &lt;- cor(tdm.dense)
library(corrplot)
corrplot(corr, method = &quot;circle&quot;, type = &quot;upper&quot;, tl.col=&quot;black&quot;, tl.cex=0.7)</pre>
<p><img alt="plot of chunk unnamed-chunk-23" src="http://i2.wp.com/www.networkx.nl/wp-content/uploads/2016/07/unnamed-chunk-23-1.png?w=456" data-recalc-dims="1"> </p>
<p>Transpose the tdm.dense so we can plot a correlogram of the terms.</p>
<pre>tdm.dense.t &lt;- t(tdm.dense)
corr.t &lt;- cor(tdm.dense.t)
corrplot(corr.t,method = &quot;circle&quot;, type = &quot;upper&quot;, tl.col=&quot;black&quot;, tl.cex=0.7)</pre>
<p><img alt="plot of chunk unnamed-chunk-24" src="http://i0.wp.com/www.networkx.nl/wp-content/uploads/2016/07/unnamed-chunk-24-1.png?w=456" data-recalc-dims="1"> </p>
<p>Well we can do a lot more analysis, mining and visualisations, this is it for now.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://www.networkx.nl/data-science/text-mining-r-vikings/">Text Mining with R on Vikings episode scripts</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.networkx.nl/">Networkx</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/text-mining-with-r-on-vikings-episode-scripts/"></div></div><div><a data-url="http://www.r-bloggers.com/text-mining-with-r-on-vikings-episode-scripts/" data-counturl="http://www.r-bloggers.com/text-mining-with-r-on-vikings-episode-scripts/" data-count="vertical" data-text="Text Mining with R on Vikings episode scripts" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.networkx.nl/data-science/text-mining-r-vikings/"> R – Networkx</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/w7yYIuS6RgQ"></a></div>