<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/hardwired-for-tidy-text/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.johnmackintosh.com/2018-01-30-hardwired-for-tidy-text/"> HighlandR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Song lyric and sentiment analysis for all</h2>
<p>So – a while back I did a <a rel="nofollow" href="https://www.johnmackintosh.com/2017-10-29-It%27s-a-dirty-job/" target="_blank">tidy text analysis on Faith No More lyrics</a>. I had thought about doing this with Metallica album lyrics, as they have had a long career, spanning thier late teens/twenties to their 50’s.</p>
<p>However, I found the process of obtaining lyrics and getting them into shape for analysis too painful, so I chose a band with slightly less output.</p>
<p>Good news though – things have changed with the release of the <a rel="nofollow" href="https://github.com/josiahparry/geniusR" target="_blank">geniusr</a> package from <a rel="nofollow" href="https://medium.com/@JosiahParry" target="_blank">Josiah Parry</a> (@JosiahParry).</p>
<p>This makes getting song /album lyrics a piece of cake.</p>
<p>With my FNM analysis, I obtained individual tracks, organised them into folders by album, and then went through a lot of manual processing ( the site I obtained the lyrics from concatenated each line into a single string).</p>
<p>This package does away with all that.</p>
<p>Want to get all the lyrics for an album? Use the genius_album function</p>
<div>
<div>
<pre><code><span>MOP</span><span> </span><span>&lt;-</span><span> </span><span>genius_album</span><span>(</span><span>artist</span><span> </span><span>=</span><span> </span><span>&quot;Metallica&quot;</span><span>,</span><span>
</span><span>album</span><span> </span><span>=</span><span> </span><span>&quot;master of puppets&quot;</span><span>,</span><span> </span><span>nested</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>This returns a 4 column dataframe with the tracknumber, title and lyrics. Each line of the song lyric is one one row of the dataframe, in the perfect format for manipulating further with the tidytext package.</p>
<p>(You can of course just obtain lyrics for individual tracks, using the genius_lyrics function).</p>
<p>I want to say thanks to Josiah for being super-helpful in sorting out a glitch I chanced upon, and for being patient with me when I forgot to reinstall after he fixed it!</p>
<p>Anyway – I soon had 10 albums worth of lyrics. Most of the code is very similar to what I did for my FNM analysis, so I don’t want to get into that too much.<br>
I know that tidytext has been updated recently and I may go back and do some further work on this, but for now, I’m just going to stick up a few images and some notes.</p>
<p>Some of my earlier draft pics used the viridis palette, but as much as I love that (especially “Plasma”), it didn’t look right considering I was dealing with the guys who went for black text on a black cover for their 5th and most career changing album (“Metallica”).</p>
<p>Josiah pointed me in the direction of the <a rel="nofollow" href="https://github.com/cttobin/ggthemr" target="_blank">ggthemr</a> package.<br>
Armed with that, and some online colour palette sites, I cooked up a few custom palettes based on the band’s 10 studio album covers.</p>
<p>Where possible, I used the relevant palette, and I also created a “Combo” palettte for use where I needed more colours:</p>
<div>
<div>
<pre><code><span>colours_Reload</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;#080606&quot;</span><span>,</span><span>&quot;#484848&quot;</span><span>,</span><span>&quot;#9d9c9c&quot;</span><span>,</span><span>&quot;#f0c43e&quot;</span><span>,</span><span>&quot;#fba104&quot;</span><span>,</span><span> </span><span>&quot;#e43609&quot;</span><span>,</span><span>&quot;#AD0215&quot;</span><span>)</span><span>
</span><span>colours_Hardwired</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;#C8C8C8&quot;</span><span>,</span><span>&quot;#61a28b&quot;</span><span>,</span><span>&quot;#e7cd9b&quot;</span><span>,</span><span>&quot;#7E4611&quot;</span><span>,</span><span>&quot;#a93119&quot;</span><span>,</span><span> </span><span>&quot;#2c2625&quot;</span><span>,</span><span>&quot;#000000&quot;</span><span>)</span><span>
</span><span>Combo</span><span> </span><span>&lt;-</span><span> </span><span>define_palette</span><span>(</span><span> </span><span>swatch</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>colours_Reload</span><span>,</span><span>colours_Hardwired</span><span>),</span><span>
</span><span>gradient</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>lower</span><span> </span><span>=</span><span> </span><span>colours_Reload</span><span>[</span><span>1L</span><span>],</span><span> </span><span>upper</span><span> </span><span>=</span><span> </span><span>colours_Hardwired</span><span>[</span><span>7L</span><span>]))</span><span> </span></code></pre>
</div>
</div>
<p>So – following the pattern of my FNM analysis, here are a few plots &amp; thoughts:</p>
<h2>A brief history of Metallica</h2>
<p><img alt="2018-01-28-metallica-band-early.jpg" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-metallica-band-early.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>A Danish junior pro tennis player gets into heavy metal, follows Motorhead around the UK, ends up moving to the US and places an add to start a band. In the same week, a self-confessed loner, who happens to play guitar, also places an add to start a band. They hook up. The Danish kid is Lars Ulrich, drummer and mastermind behind the band. The loner is James Hetfield, who it turns out is a Riff God in waiting.</p>
<p>There have been others involved (notably Kirk Hammett on lead guitar and Cliff Burton (RIP), Jason Newstead and now Rob Trujillo on bass ) but Lars &amp; James are the main 2, involved in all the songwriting.</p>
<h3>Albums – the classic first 4</h3>
<ul>
<li>Kill ‘Em All the album that kickstarted a new musical movement.</li>
<li>Ride The Lightning 1 year later, they kick things up a notch, hitting on a classic formula. Fast opener. Title Track. Power Ballad at the end of side one. Instrumental. Fast closer. Some of these tunes are still getting played night after night.</li>
<li>Master of Puppets Another classic. I like “Leper Messiah” and “Orion” the most, but I’m awkward.</li>
<li>And Justice For All a bit left field. Ferocious opening and closing tracks, and also “One”. Drums on 11. Bass on 1.</li>
</ul>
<h3>The 90’s rock mega-stars</h3>
<ul>
<li>Metallica Enter Sandman, Sad But True, Nothing Else Matters – someone you know owns this album.</li>
<li>Load Woah ,they’re wearing make-up</li>
<li>ReLoad Seems like a bunch of stuff that didn’t make it on to Load?</li>
</ul>
<h3>The difficult 7th album</h3>
<ul>
<li>St.Anger Wooly hats, rage, and a dodgy drum sound. And <a rel="nofollow" href="http://www.imdb.com/title/tt0387412/" target="_blank">that documentary</a>.</li>
</ul>
<h3>Back on track</h3>
<ul>
<li>Death Magnetic Rick Rubin on production. Should have been awesome. Was it?</li>
<li>Hardwired to Self Destruct A return to form. Some great tracks (“Here Comes Revenge”). During “Halo on Fire” they turn into The Cult. This is a Good Thing.</li>
</ul>
<p><img alt="2018-01-28-Metallica-band.jpg" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-01-28-Metallica-band.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>OK – all ready? Let’s “Hit The Lights”:</p>
<p><img alt="2018-01-28-Most-Common-Words.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-01-28-Most-Common-Words.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I’ve only heard 2 tracks from St.Anger (lucky me). Produced at their lowest point, with an “acquired taste” drum sound ( Lars Ulrich forgot to switch the snares on for one recording, and liked the sound, so kept it for the entire album) and alongside the title track , “Frantic” is the other. It’s got a pretty repetitive chorus, which is why “Tick” is the most common word overall, though I did get a surprise when I first saw this.<br>
Altogether now – “ tick tick tick tick tick tick tick tock”<br>
No?<br>
It’s actually annoyingly catchy.</p>
<p><img alt="2018-01-28-Most-Common-Words-by-Song-Excluding-Stop-Words.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-01-28-Most-Common-Words-by-Song-Excluding-Stop-Words.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h3>Can I get a “Yeah”?</h3>
<p>Let’s dig in to this at album level:</p>
<p><img alt="2018-01-28-Most-Common-Words-by-Album.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-01-28-Most-Common-Words-by-Album.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The first thing that I want to highlight here is I couldn’t for the life of me get the bars to go into the correct descending order, despite copying the code precisely as I have used it several times before.<br>
In the end, I had to publish and be damned, but – I KNOW! – it’s not 100% correct.</p>
<p>The main thing here to note is that “Yeah” features heavily throughout the 90’s albums (Metallica, Load &amp; Reload). I pretty much stopped listening to the band sometime between “Metallica” and “Load” – and have to confess hadn’t picked up on this. (I did buy “Load” years later out of curiousity (and it was on sale!). It’s a good album. There’s some good stuff on there).</p>
<p>Term Document Frequency – finding most commonly used words, and the inverse term document frequency, finding words that are rarely used but also important:</p>
<p><img alt="2018-01-28-Term-Frequency-by-Album.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-Term-Frequency-by-Album.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-01-28-Inverse-Term-Document-Frequency-by-Album.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-Inverse-Term-Document-Frequency-by-Album.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Looking at the sentiment of the band’s output, and guess what? They’re not exactly shiny happy people,at least, not on paper:</p>
<p><img alt="2018-01-28-Sentiment-by-Album-timeline.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-01-28-Sentiment-by-Album-timeline.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I produced a couple of plots for the sentiment by track:</p>
<p><img alt="2018-01-28-Sentiment-by-track.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-Sentiment-by-track.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The more traditional bar plot, and also this one using geom_point instead:</p>
<p><img alt="2018-01-28-Sentiment-by-track-point.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-01-28-Sentiment-by-track-point.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I quite like this one to be honest, although I could do with a bigger monitor to fit everything in.</p>
<p>I also produced more of a timeline strip plot by release date.</p>
<p>Here’s the positive sentiment score by track over time:</p>
<p><img alt="2018-01-29-Positive-Sentiment-by-track-timeline.jpg" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-01-29-Positive-Sentiment-by-track-timeline.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Judging by this – “Master of Puppets” has the most positive net sentiment.</p>
<p>“St.Anger” is the most negative, followed by “All Within My Hands”. (For this plot, I multiplied the negative sentiment by -1 to force it below the axis):</p>
<p><img alt="2018-01-29-Negative-Sentiment-by-track-timeline.jpg" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-01-29-Negative-Sentiment-by-track-timeline.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>“St.Anger” also scores highly for positive sentiment, leaving “All Within My Hands” as the most miserable net sentiment score.</p>
<p><img alt="2018-01-29-Sentiment-by-track-timeline.jpg" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-01-29-Sentiment-by-track-timeline.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<h3>Topic Models</h3>
<p>As with last time, I’m going to take the easy way out and refer you to the tidytext website to explain topic models. Here they are for each album, with custom , cover specific colours.</p>
<p><img alt="2018-01-28-Topic_Models-KEA.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-01-28-Topic_Models-KEA.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-01-28-Topic_Models-RTL.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-01-28-Topic_Models-RTL.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-01-28-Topic_Models-MOP.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-01-28-Topic_Models-MOP.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-01-28-Topic_Models-AJFA.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-Topic_Models-AJFA.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-01-28-Topic_Models-Black.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-Topic_Models-Black.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>That’s right, I found several shades of black for you.</p>
<p><img alt="2018-01-28-Topic_Models-Load.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-Topic_Models-Load.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-01-28-Topic_Models-Reload.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-01-28-Topic_Models-Reload.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-01-28-Topic_Models-Anger.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-Topic_Models-Anger.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-01-28-Topic_Models-Magnetic.png" src="https://i1.wp.com/www.johnmackintosh.com/img/2018-01-28-Topic_Models-Magnetic.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-01-28-Topic_Models-Hardwired.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-Topic_Models-Hardwired.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Almost done for the time being. Some words have “not” or other negating words immediately in front of them, so this can change the meaning/ sentiment. Which words does this most commonly affect?</p>
<p><img alt="2018-01-28-Negative-trigrams-Word-2-of-3-by-negating-word.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-Negative-trigrams-Word-2-of-3-by-negating-word.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="2018-01-28-Negative-trigrams-Word-3-of-3-by-negating-word.png" src="https://i0.wp.com/www.johnmackintosh.com/img/2018-01-28-Negative-trigrams-Word-3-of-3-by-negating-word.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I looked at correlations of individual words, but it was not very informative, however, looking at the words used by album, there was quite a nice pattern emerging:</p>
<p><img alt="2018-01-28-album-word-correlations-original.png" src="https://i2.wp.com/www.johnmackintosh.com/img/2018-01-28-album-word-correlations-original.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Although the correlations are weak – we can see Metallica, Load and Reload being very closely linked. At the other end, Death Magnetic, Hardwired and Kill ‘Em All are fairly closely linked (30 + years apart) and in the middle are the classics – Ride The Lightning, Master of Puppets, and also And Justice For All.</p>
<p>I’d expected there to be a clear difference between the newer stuff and the old stuff. I certainly didn’t expect to see “Kill ‘Em All” and “Hardwired..” so close together. However- if you watch the “making of” videos on YouTube, you can see the bands writes the guitar and drums first, and then James adds lyrics later – and for the latest album, the rest of the band were also pitching in. I suspect that the lyrics aren’t perhaps as important as the music – certainly not compared to the And Justice For All period where everything seemed to tie together more.</p>
<p>To wrap up, this has been a much smoother task second time round, (I wasn’t planning to do this again) and if I ever do it again, going to have to look into purrr to make things even easier.</p>
<p>Thanks to Josiah – cool guy, cool R package!</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.johnmackintosh.com/2018-01-30-hardwired-for-tidy-text/"> HighlandR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/vkoJGpxkQYs" height="1"></a></div>