Assignment 10A Approach

Author

Mubin Ejaz

Objective

The objective of this assignment is to reproduce and extend the sentiment analysis example presented in Chapter 2 of Text Mining with R: A Tidy Approach by Julia Silge and David Robinson (2017).

Approach: Sentiment Analysis with Text Mining with R

I will start out by reproducing the example given in Chapter 2, Sentiment Analysis, in the book Text Mining With R. I’ll make sure that it runs without error.

Extend the example:

For the extended analysis, I plan to use the IMDb Large Movie Review Dataset, accessed through the R package textdata. I will use a balanced sample of 500 movie reviews, consisting of 250 positive and 250 negative reviews. For my assignment:

  • Bing will tell me whether a word is positive or negative.

  • AFINN will tell us how strongly positive or negative a word is.

That’s the comparison we’ll make using the movie reviews.

My additional package is textdata, and additional lexicon is AFINN

Code From the textbook:

library(janeaustenr)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(stringr)
library(tidytext)
library(ggplot2)
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)

You can add options to executable code like this

library(tidyr)
jane_austen_sentiment <- tidy_books %>%
  inner_join(get_sentiments("bing")) %>%
  count(book, index = linenumber %/% 80, sentiment) %>%
  pivot_wider(names_from = sentiment, values_from = n, values_fill = 0) %>% 
  mutate(sentiment = positive - negative)
Joining with `by = join_by(word)`
Warning in inner_join(., get_sentiments("bing")): Detected an unexpected many-to-many relationship between `x` and `y`.
ℹ Row 435434 of `x` matches multiple rows in `y`.
ℹ Row 5051 of `y` matches multiple rows in `x`.
ℹ If a many-to-many relationship is expected, set `relationship =
  "many-to-many"` to silence this warning.
ggplot(
  jane_austen_sentiment,
  aes(index, sentiment, fill = book)
) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~book, ncol = 2, scales = "free_x")

The echo: false option disables the printing of code (only output is displayed).