Dickens wrote fourteen and a half novels. I will start from analyzing five of them - “A Tale of Two Cities”, “Great Expectations”, “A Christmas Carol in Prose; Being a Ghost Story of Christmas”, “Oliver Twist” and “Hard Times”.
Project Gutenberg offers over 53,000 free books. I will download Dickens’ novels in UTF-8 encoded texts from there using gutenbergr package developed by David Robinson. Besides, I will be using the following packages for this project.
Download Dickens’ five novels by Project Gutenberg ID numbers.
The unnest_tokens package is used to split each row so that there is one token (word) in each row of the new data frame (tidy_dickens). Then remove stop words with an anti_join function.
After removing the stop words, here is a list of words starts from the most frequent.
Sentiment in Dickens’ Five Novels
tidytext package contains several sentiment lexicons, I am using “bing” for the following tasks.
Here I got the sentiment categories of Dickens’ words.
The word “miss” is the most frequent negative word here, but it is used to describe unmarried women in Dickens’ works. In particualr, Miss Havisham is a significant character in the Charles Dickens novel “Great Expectations”. Dickens describes her as looking like “the witch of the place”. In this case, probably “miss”” should be a negative word.
Word cloud is a good idea to identify trends and patterns that would otherwise be unclear or difficult to see in a tabular format.
And compare most frequent positive and negative words in word cloud.
Relationships between words
Each token now represents a bigram (two words paired). if one of the words in the bigram is a stop word, this word will be removed. After filtering out stop words, what are the most frequent bigrams?
Names are the most common paired words in Dickens’ novels.
Before visualizing it, I have to make these bigrams back to be united.
Yes, the most frequent bigrams in Dickens’ works are names. I also notice some pairings of a common verb such as “wine shop” from “A Tale of Two Cities” and “oliver twist”.
At last, visualizing a network of bigrams of Dickens’ five novels.
Wow, that was so much fun! I don’t want to finish as yet. I want to look into one of these five novels - “A Tale of Two Cities”.
This time I will download the plain text file for “A Tale of Two Cities” only, leave out the Project Gutenberg header and footer information, then concatenate these lines into paragraphs as following:
Create a data frame combine the line number of the book with the sentiment score, then extract positive and negative scores for visualization.
Seems the positive scores and the negative scores are almost equal overall, it does make sense given the content of the novel.
The End
Again, that was so much fun, my post just touched a bit of it on text mining. There are many more things to do such as comparing text across different novelists, save it to the next time.
Source code that created this post can be found here. I am happy to hear any feedback and questions.