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).
Initial Approach: Sentiment Analysis with Text Mining with R
I started out by reproducing the example given in Chapter 2, Sentiment Analysis, in the book Text Mining With R.
Extend the example:
For my extended analysis, I downloaded the IMDb Large Movie Review Dataset from Zenodo. I selected a balanced sample of 50 movie reviews, consisting of 25 positive and 25 negative reviews. I used the Bing and AFINN sentiment lexicons to compare how each method evaluates the reviews.
Bing showed whether a word is positive or negative.
AFINN showed 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
Reproducing the 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
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.
# Preparing data for the graphimdb_long <- imdb_results %>%select( review_id, actual_sentiment, bing_per_100, afinn_per_100 ) %>%pivot_longer(cols =c(bing_per_100, afinn_per_100),names_to ="lexicon",values_to ="score" ) %>%mutate(lexicon =recode( lexicon,bing_per_100 ="Bing",afinn_per_100 ="AFINN" ) )# Creating the graphggplot( imdb_long,aes(x = actual_sentiment,y = score,fill = actual_sentiment )) +geom_boxplot(show.legend =FALSE) +facet_wrap(~lexicon, scales ="free_y") +labs(title ="Sentiment Analysis of IMDb Movie Reviews",subtitle ="Comparing Bing and AFINN",x ="Original IMDb Review Label",y ="Sentiment Score per 100 Words" ) +theme_minimal()
Conclusion
Overall, this assignment helped me understand how sentiment analysis works and how different lexicons can produce different results. I found it interesting that Bing and AFINN did not always agree, even though they were analyzing the same movie reviews. Both showed higher sentiment scores for positive reviews, but AFINN actually gave the negative reviews a slightly positive average score. This made me realize that sentiment analysis isn’t always as straightforward as counting positive and negative words because context and how words are used also matter.