Introduction

Samuel Langhorne Clemens, known worldwide by his pen name Mark Twain, was an American author renowned worldwide as one of the most influential writers in the English language. He has authored more than 25 novels,in this project I will be analysing four of his very famous works :-


I will download the novels from Project Gutenberg (A library of over 60,000 free eBooks) using the gutenbergr package.

Tokenizing by n-gram

We got the data, the next step is tokenization.Where we split the data into individual words using the unnest_tokens().Then remove stop words using antijoin().

Let us have a quick look at the most frequently used words in Mark Twain’s novels.

Earlier we simply counted the occurence of each word and listed the most frequent words.But this doesn’t provide much information on its own as we are unaware of the context in which these words were used.Thus we will now work with tokens containing two words(bigrams) or three words(trigrams) as it can help to extract useful phrases which leads to some additional insights.

Bigrams

## # A tibble: 516,058 x 3
##    gutenberg_id title                        bigram           
##           <int> <chr>                        <chr>            
##  1           74 The Adventures of Tom Sawyer the adventures   
##  2           74 The Adventures of Tom Sawyer adventures of    
##  3           74 The Adventures of Tom Sawyer of tom           
##  4           74 The Adventures of Tom Sawyer tom sawyer       
##  5           74 The Adventures of Tom Sawyer sawyer by        
##  6           74 The Adventures of Tom Sawyer by mark          
##  7           74 The Adventures of Tom Sawyer mark twain       
##  8           74 The Adventures of Tom Sawyer twain samuel     
##  9           74 The Adventures of Tom Sawyer samuel langhorne 
## 10           74 The Adventures of Tom Sawyer langhorne clemens
## # ... with 516,048 more rows

In the below steps, we first split the bigrams into two. Remove if any of the word is a stopword and then unite them back again.

## # A tibble: 47,257 x 4
##    gutenberg_id title                        word1     word2    
##           <int> <chr>                        <chr>     <chr>    
##  1           74 The Adventures of Tom Sawyer tom       sawyer   
##  2           74 The Adventures of Tom Sawyer mark      twain    
##  3           74 The Adventures of Tom Sawyer twain     samuel   
##  4           74 The Adventures of Tom Sawyer samuel    langhorne
##  5           74 The Adventures of Tom Sawyer langhorne clemens  
##  6           74 The Adventures of Tom Sawyer clemens   contents 
##  7           74 The Adventures of Tom Sawyer contents  chapter  
##  8           74 The Adventures of Tom Sawyer tom       aunt     
##  9           74 The Adventures of Tom Sawyer aunt      polly    
## 10           74 The Adventures of Tom Sawyer polly     decides  
## # ... with 47,247 more rows
## # A tibble: 47,257 x 3
##    gutenberg_id title                        bigram           
##           <int> <chr>                        <chr>            
##  1           74 The Adventures of Tom Sawyer tom sawyer       
##  2           74 The Adventures of Tom Sawyer mark twain       
##  3           74 The Adventures of Tom Sawyer twain samuel     
##  4           74 The Adventures of Tom Sawyer samuel langhorne 
##  5           74 The Adventures of Tom Sawyer langhorne clemens
##  6           74 The Adventures of Tom Sawyer clemens contents 
##  7           74 The Adventures of Tom Sawyer contents chapter 
##  8           74 The Adventures of Tom Sawyer tom aunt         
##  9           74 The Adventures of Tom Sawyer aunt polly       
## 10           74 The Adventures of Tom Sawyer polly decides    
## # ... with 47,247 more rows

After filtering, essentially the next step is to find the most common bigrams in each of the novels.

We can see that the most common bigrams in Mark Twain’s novels is names such as injun joe,mary jane,aunt sally,aunt polly,miss watson etc.

Finally, we will visualize relationships between these bigrams by plotting a network of bigrams with ggraph.

In the word network we can see that quantitative words like “yards”,“miles”,“dollars”,“feet” all have a common node “hundred”.Also we can see salutation such as “aunt”,“miss”,“col” are followed by their names.Other than those there are a lot of names of characters coming up.

Trigrams

## # A tibble: 12,776 x 4
##    word1   word2    word3       n
##    <chr>   <chr>    <chr>   <int>
##  1 miss    mary     jane       15
##  2 salt    lake     city       15
##  3 genuine mexican  plug        8
##  4 ten     thousand dollars     8
##  5 hundred thousand dollars     7
##  6 twenty  thousand dollars     7
##  7 stephen dowling  bots        6
##  8 aye     aye      sir         5
##  9 de      river    en          5
## 10 gold    hill     news        5
## # ... with 12,766 more rows

The popular trigrams are “miss mary jane”, “salt lake city”, “genuine mexican plug”, “ten/hundred/twenty thousand dollars”.

Sentiment Analysis

Comparison Cloud

At first we will look at the words that contribute the most to positive and negative sentiment.We will be using the Bing lexicon for this purpose.

Overall Sentiment of Novels

Here we first find a sentiment score for each word using the Bing lexicon and inner_join().And then divide each novel into 80 sections and define a column index to keep track of where we are in the narrative.This index counts up sections of 80 lines of text.We then use spread() so that we have positive and negative sentiment in separate columns, and lastly calculate a net sentiment (positive - negative).Now we can plot the net sentiment score against the index on the x-axis that keeps track of narrative time in sections of text.

Next we will plot a stacked barchart to find the proportion of positive and negative sentiments in each of the four novels.

All the novels have a greater proportion of negative sentiment.But earlier when we divided each novel into sections we found that most of the sections were negative. I suspect many words are falsely labelled as positive.

To verify this we will use the bigrams which we collected earlier and use them to find the most common words which are preceded by negation.

Here we can see pairings like “can’t help”,“don’t like”,“no good”,“not help” etc,which otherwise will be termed as positive.

NRC Lexicon

We will use the NRC Word Emotion Association Lexicon to add 8 more sentiments to our analysis.
Before we find the major sentiments in the novels,let us first check how the words contribute to the sentiments in the NRC lexicon.

Next will look at how the sentiments were distributed for each of the four novels.Since “The Glided Age” and “Roughing It” are relatively biggers novels in terms of number of words, I have divided every words contribution from a particular novel by the total contribution of all the words in that novel.So that all the novels are in similar scale.

Interestingly all the novels show a similar trend for a particular sentiment.The only major difference is in the novel “The Glided Age” where the degree of joy is greater than the degree of sadness and fear.Which is not the trend followed by other novels.
Earlier also we saw that “The Glided Age” was relatively more positive compared to the other novels.

With that we come to the end of this project.Wow! that was so much fun, learned a lot of new things along the way.
The source code for this project can be found here.

References

Text Mining with R