The source codes and contens come from the book Text Mining with R Enjoy
Intro
- Let’s address the topic of opinion mining or sentiment analysis. When human readers approach a text, we use our understanding of the emotional intent of words to infer whether a section of text is positive or negative, or perhaps characterized by some other more nuanced emotion like surprise or disgust. The flow chart is shown in Figure 2.1.
knitr::include_graphics("img/Figure_2.1_Sentiment_Analysis.png")

- One way to analyze the sentiment of a text is to consider the text as a combination of its individual words and the sentiment content of the whole text as the sum of the sentiment content of the individual words. This isn’t the only way to approach sentiment analysis, but it is an often-used approach, and an approach that naturally takes advantage of the tidy tool ecosystem.
- The Sentiment DataSet
| library(tidytext) |
| sentiments |
The three general-purpose lexicons are + AFINN from Finn Årup Nielsen, + bing from Bing Liu and collaborators, and + nrc from Saif Mohammad and Peter Turney.
Common
- All three of these lexicons are based on unigrams, i.e., single words.
- These lexicons contain many English words and the words are assigned scores for positive/negative sentiment, and also possibly emotions like joy, anger, sadness, and so forth.
Differences
The nrc lexicon categorizes words in a binary fashion (“yes”/“no”) into categories of positive, negative, anger, anticipation, disgust, fear, joy, sadness, surprise, and trust.
The bing lexicon categorizes words in a binary fashion into positive and negative categories.
The AFINN lexicon assigns words with a score that runs between -5 and 5, with negative scores indicating negative sentiment and positive scores indicating positive sentiment.
tidytext provides a function get_sentiments() to get specific sentiment lexicons without the columns that are not used in that lexicon.
Considerations
- They were constructed via either crowdsourcing (using, for example, Amazon Mechanical Turk) or by the labor of one of the authors, and were validated using some combination of crowdsourcing again, restaurant or movie reviews, or Twitter data.
- Thus, we need to consider to apply these sentiment lexicons to styles of text dramatically different from what they were validated on, such as narrative fiction from 200 years ago.
- Moreover, There are also some domain-specific sentiment lexicon available, constructed to be used with text from a specific content area.
- In conclusion, in this tutorial, it’s better to find the workflow of sentiment analysis and to get an insight of using this tutorial for your future context.
- Sentiment Analysis with inner join
- Let’s look at the words with a joy score from the NRC lexicon. What are the most common joy words in Emma?
| library(janeaustenr) |
| library(dplyr) |
| library(stringr) |
| |
| tidy_books <- austen_books() %>% |
| group_by(book) %>% |
| mutate(linenumber = row_number(), |
| chapter = cumsum(str_detect(text, regex("^chapter [\\divxlc]", |
| ignore_case = TRUE)))) %>% |
| ungroup() %>% |
| unnest_tokens(word, text) |
| head(tidy_books) |
- Now that the text is in a tidy format with one word per row, we are ready to do the sentiment analysis. First, let’s use the NRC lexicon and filter() for the joy words. Next, let’s filter() the data frame with the text from the books for the words from Emma and then use inner_join() to perform the sentiment analysis. What are the most common joy words in Emma? Let’s use count() from dplyr.
| nrc_joy <- get_sentiments("nrc") %>% |
| filter(sentiment == "joy") |
| |
| tidy_books %>% |
| filter(book == "Emma") %>% |
| inner_join(nrc_joy) %>% |
| count(word, sort = TRUE) |
We see mostly positive, happy words about hope, friendship, and love here. Now, We can also examine how sentiment changes throughout each novel. + 1. we find a sentiment score for each word using the Bing lexicon and inner_join(). + 2. Next, we count up how many positive and negative words there are in defined sections of each book. We define an index here to keep track of where we are in the narrative; this index (using integer division) counts up sections of 80 lines of text.
The %/% operator does integer division (x %/% y is equivalent to floor(x/y)) so the index keeps track of which 80-line section of text we are counting up negative and positive sentiment in.
- We then use spread() so that we have negative and positive sentiment in separate columns, and lastly calculate a net sentiment (positive - negative).
| library(tidyr) |
| |
| jane_austen_sentiment <- tidy_books %>% |
| inner_join(get_sentiments("bing")) %>% |
| count(book, index = linenumber %/% 80, sentiment) %>% |
| spread(sentiment, n, fill = 0) %>% |
| mutate(sentiment = positive - negative) |
## Joining, by = "word"
- Now we can plot these sentiment scores across the plot trajectory of each novel. Notice that we are plotting against the index on the x-axis that keeps track of narrative time in sections of text.
| library(ggplot2) |
| |
| ggplot(jane_austen_sentiment, aes(index, sentiment, fill = book)) + |
| geom_col(show.legend = FALSE) + |
| facet_wrap(~book, ncol = 2, scales = "free_x") |

Comparing the three sentiment dictionaries
Let’s do it more with other sentiment dictionaries and the narrative arc of Pride and Prejudice.
First, let’s use filter() to choose only the words from the one novel we are interested in.
| pride_prejudice <- tidy_books %>% |
| filter(book == "Pride & Prejudice") |
| |
| head(pride_prejudice) |
- Second, Now, we can use inner_join() to calculate the sentiment in different ways.
Remember from above that the AFINN lexicon measures sentiment with a numeric score between -5 and 5, while the other two lexicons categorize words in a binary fashion, either positive or negative. To find a sentiment score in chunks of text throughout the novel, we will need to use a different pattern for the AFINN lexicon than for the other two.
| afinn <- pride_prejudice %>% |
| inner_join(get_sentiments("afinn")) %>% |
| group_by(index = linenumber %/% 80) %>% |
| summarise(sentiment = sum(score)) %>% |
| mutate(method = "AFINN") |
## Joining, by = "word"
| bing_and_nrc <- bind_rows(pride_prejudice %>% |
| inner_join(get_sentiments("bing")) %>% |
| mutate(method = "Bing et al."), |
| pride_prejudice %>% |
| inner_join(get_sentiments("nrc") %>% |
| filter(sentiment %in% c("positive", "negative"))) %>% |
| mutate(method = "NRC") |
| ) %>% |
| count(method, index = linenumber %/% 80, sentiment) %>% |
| spread(sentiment, n, fill = 0) %>% |
| mutate(sentiment = positive - negative) |
| ## Joining, by = "word" |
| ## Joining, by = "word" |
- Now, we have an estimate of the net sentiment (positive - negative) in each chunk of the novel text for each sentiment lexicon. Let’s visualize all of them.
| bind_rows(afinn, |
| bing_and_nrc) %>% |
| ggplot(aes(index, sentiment, fill = method)) + |
| geom_col(show.legend = FALSE) + |
| facet_wrap(~method, ncol = 1, scales = "free_y") |

- Three plots are saying that the three different lexicons for calculating sentiment give results that are different in an absolute sense but have similar relative trajectories through the novel.
- Question, why is, the result for the NRC lexicon biased so high in sentiment compared to the Bing et al. result? Let’s look briefly at how many positive and negative words are in these lexicon.
| get_sentiments("nrc") %>% |
| filter(sentiment %in% c("positive", |
| "negative")) %>% |
| count(sentiment) |
| get_sentiments("bing") %>% |
| count(sentiment) |
Both lexicons have more negative than positive words, but the ratio of negative to positive words is higher in the Bing lexicon than the NRC lexicon. This will contribute to the effect we see in the plot above, as will any systematic difference in word matches, e.g. if the negative words in the NRC lexicon do not match the words that Jane Austen uses very well. Whatever the source of these differences, we see similar relative trajectories across the narrative arc, with similar changes in slope, but marked differences in absolute sentiment from lexicon to lexicon. This is all important context to keep in mind when choosing a sentiment lexicon for analysis.
point 1. This comment makes me to ponder the direct usage of the given lexicons or packages.
point 2. This comment makes me to motivate develop a specific lexcions for the each different context, using given lexicons
Most common positive and negative words
One advantage of having the data frame with both sentiment and word is that we can analyze word counts that contribute to each sentiment. By implementing count() here with arguments of both word and sentiment, we find out how much each word contributed to each sentiment.
| bing_word_counts <- tidy_books %>% |
| inner_join(get_sentiments("bing")) %>% |
| count(word, sentiment, sort = TRUE) %>% |
| ungroup() |
## Joining, by = "word"
This can be shown visually, and we can pipe straight into ggplot2, if we like, because of the way we are consistently using tools built for handling tidy data frames.
| bing_word_counts %>% |
| group_by(sentiment) %>% |
| top_n(10) %>% |
| ungroup() %>% |
| mutate(word = reorder(word, n)) %>% |
| ggplot(aes(word, n, fill = sentiment)) + |
| geom_col(show.legend = FALSE) + |
| facet_wrap(~sentiment, scales = "free_y") + |
| labs(y = "Contribution to sentiment", |
| x = NULL) + |
| coord_flip() |
#

- Point 1. let us see the word “miss”. The word is coded as negative but it is often used unmarried women n Jane Austen’s works. So, if it were appropriate for our purposes, we could easily add “miss” to a custom stop-words list using bind_rows().
| custom_stop_words <- bind_rows(data_frame(word = c("miss"), |
| lexicon = c("custom")), |
| stop_words) |
| |
| custom_stop_words |
Wordclouds
#
| tidy_books %>% |
| anti_join(stop_words) %>% |
| count(word) %>% |
| with(wordcloud(word, n, max.words = 100)) |
## Joining, by = "word"

| tidy_books %>% |
| inner_join(get_sentiments("bing")) %>% |
| count(word, sentiment, sort = TRUE) %>% |
| acast(word ~ sentiment, value.var = "n", fill = 0) %>% |
| comparison.cloud(colors = c("gray20", "gray80"), |
| max.words = 100) |
## Joining, by = "word"

Looking at units beyond just words
| library(reshape2) |
| |
| PandP_sentences <- data_frame(text = prideprejudice) %>% |
| unnest_tokens(sentence, text, token = "sentences") |
| |
| PandP_sentences$sentence[2] |
#
Bonus: Looking at units beyond just words
- Another option in unnest_tokens() is to split into tokens using a regex pattern. We could use this, for example, to split the text of Jane Austen’s novels into a data frame by chapter.
| austen_chapters <- austen_books() %>% |
| group_by(book) %>% |
| unnest_tokens(chapter, text, token = "regex", |
| pattern = "Chapter|CHAPTER [\\dIVXLC]") %>% |
| ungroup() |
| |
| austen_chapters %>% |
| group_by(book) %>% |
| summarise(chapters = n()) |
- We can use tidy text analysis to ask questions such as what are the most negative chapters in each of Jane Austen’s novels?
- First, let’s get the list of negative words from the Bing lexicon.
- Second, let’s make a data frame of how many words are in each chapter so we can normalize for the length of chapters.
- Then, let’s find the number of negative words in each chapter and divide by the total words in each chapter.
- For each book, which chapter has the highest proportion of negative words?
| bingnegative <- get_sentiments("bing") %>% |
| filter(sentiment == "negative") |
| |
| wordcounts <- tidy_books %>% |
| group_by(book, chapter) %>% |
| summarize(words = n()) |
| |
| tidy_books %>% |
| semi_join(bingnegative) %>% |
| group_by(book, chapter) %>% |
| summarize(negativewords = n()) %>% |
| left_join(wordcounts, by = c("book", "chapter")) %>% |
| mutate(ratio = negativewords/words) %>% |
| filter(chapter != 0) %>% |
| top_n(1) %>% |
| ungroup() |
These are the chapters with the most sad words in each book, normalized for number of words in the chapter. What is happening in these chapters? + In Chapter 43 of Sense and Sensibility Marianne is seriously ill, near death. + In Chapter 34 of Pride and Prejudice Mr. Darcy proposes for the first time (so badly!). + In Chapter 46 of Mansfield Park is almost the end, when everyone learns of Henry’s scandalous adultery + In Chapter 15 of Emma is when horrifying Mr. Elton proposes, and in Chapter 21 of Northanger Abbey Catherine is deep in her Gothic faux fantasy of murder, etc. + In Chapter 4 of Persuasion is when the reader gets the full flashback of Anne refusing Captain Wentworth and how sad she was and what a terrible mistake she realized it to be.