In this project we will be starting off by replicating the code from
(Silge and Robinson 2021).
Once that is done we will b e repuding the results using a different
sentiment analysis package called sentimentr, and instead of doing
sentiment by word it will be done by chapter.
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)
books_by_chapter <- tidy_books %>%
group_by(book, chapter) %>%
summarise(text = paste(word, collapse = " ")) %>%
ungroup()
## `summarise()` has grouped output by 'book'. You can override using the
## `.groups` argument.
nrc_joy <- get_sentiments("nrc") %>%
filter(sentiment == "joy")
tidy_books %>%
filter(book == "Emma") %>%
inner_join(nrc_joy) %>%
count(word, sort = TRUE)
## Joining with `by = join_by(word)`
## # A tibble: 301 × 2
## word n
## <chr> <int>
## 1 good 359
## 2 friend 166
## 3 hope 143
## 4 happy 125
## 5 love 117
## 6 deal 92
## 7 found 92
## 8 present 89
## 9 kind 82
## 10 happiness 76
## # ℹ 291 more rows
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 above code was from (Silge and Robinson
2021).
All code below was my own work for continuing the analysis.
sentiments <- sentimentr::sentiment_by(books_by_chapter$text)
## Warning: Each time `sentiment_by` is run it has to do sentence boundary disambiguation when a
## raw `character` vector is passed to `text.var`. This may be costly of time and
## memory. It is highly recommended that the user first runs the raw `character`
## vector through the `get_sentences` function.
sentiments <- sentiments %>%
mutate(sentiment = ifelse(ave_sentiment > 0, "Positive", "Negative"))
books_by_chapter$element_id <- 1:nrow(books_by_chapter)
sentiment_by_chapter <- merge(books_by_chapter, sentiments, by = "element_id")
ggplot(sentiment_by_chapter, aes(x=chapter, y=ave_sentiment, group=book, fill=book)) +
geom_col(show.legend = FALSE) +
facet_wrap(~book, ncol = 2, scales = 'free_x') +
labs(x='Chapter', y='Average Sentiment')
From both sentiment analysis results we can see that the majority of Jane Austin’s work can be interpreted as having a positive sentiment, with Persuasion not having a single negative chapter, and all of the others having very few wqhen compared to the number of postive chapters. This aligns with what we found when looking at the books by work, with the vast majority of words in each book having a positive sentiment.