10A - Sentiment Analysis

Author

Caresse Cross Beard

Published

November 22, 2026

Reproduce Base Example

library(tidytext)

get_sentiments("afinn")
# A tibble: 2,477 × 2
   word       value
   <chr>      <dbl>
 1 abandon       -2
 2 abandoned     -2
 3 abandons      -2
 4 abducted      -2
 5 abduction     -2
 6 abductions    -2
 7 abhor         -3
 8 abhorred      -3
 9 abhorrent     -3
10 abhors        -3
# ℹ 2,467 more rows
get_sentiments("bing")
# A tibble: 6,786 × 2
   word        sentiment
   <chr>       <chr>    
 1 2-faces     negative 
 2 abnormal    negative 
 3 abolish     negative 
 4 abominable  negative 
 5 abominably  negative 
 6 abominate   negative 
 7 abomination negative 
 8 abort       negative 
 9 aborted     negative 
10 aborts      negative 
# ℹ 6,776 more rows
get_sentiments("nrc")
# A tibble: 13,872 × 2
   word        sentiment
   <chr>       <chr>    
 1 abacus      trust    
 2 abandon     fear     
 3 abandon     negative 
 4 abandon     sadness  
 5 abandoned   anger    
 6 abandoned   fear     
 7 abandoned   negative 
 8 abandoned   sadness  
 9 abandonment anger    
10 abandonment fear     
# ℹ 13,862 more rows
library(janeaustenr)
Warning: package 'janeaustenr' was built under R version 4.6.2
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)

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)
nrc_joy <- get_sentiments("nrc") %>% 
  filter(sentiment == "joy")

tidy_books %>%
  filter(book == "Emma") %>%
  inner_join(nrc_joy) %>%
  count(word, sort = TRUE)
# 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
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)
library(ggplot2)

ggplot(jane_austen_sentiment, aes(index, sentiment, fill = book)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~book, ncol = 2, scales = "free_x")

Sentiment analysis approach adapted from:

Silge, J., & Robinson, D. (2017).

Text Mining with R: A Tidy Approach.

Chapter 2: Sentiment Analysis.

https://www.tidytextmining.com/sentiment.html

Extending Analysis - New Data

hundred_books <- read.csv("Top-100 Trending Books.csv")

Tanwar, A. (n.d.). Top 200 trending books with reviews [Data set]. Kaggle. https://www.kaggle.com/datasets/anshtanwar/top-200-trending-books-with-reviews

names(hundred_books)
[1] "Rank"                "book.title"          "book.price"         
[4] "rating"              "author"              "year.of.publication"
[7] "genre"               "url"                
customer_reviews <- read.csv("customer reviews.csv")
names(customer_reviews)
 [1] "Sno"                "book.name"          "review.title"      
 [4] "reviewer"           "reviewer.rating"    "review.description"
 [7] "is_verified"        "date"               "timestamp"         
[10] "ASIN"              
library(janitor)
Warning: package 'janitor' was built under R version 4.6.2
hundred_books <- hundred_books %>%
  clean_names()

customer_reviews <- customer_reviews %>%
  clean_names()
top_books <- hundred_books %>%
  arrange(desc(rating)) %>%
  slice_head(n = 10)

top_books
   rank
1    52
2    19
3    27
4    30
5    31
6    35
7    37
8    40
9    44
10   49
                                                                                                                      book_title
1                                                                                             Resurrection Walk (Lincoln Lawyer)
2                                                                                   Taylor Swift: A Little Golden Book Biography
3                                        Goodnight, Goodnight Construction Site (Board Book for Toddlers, Children's Board Book)
4                                                                                       Brown Bear, Brown Bear, What Do You See?
5  Balloons over Broadway: The True Story of the Puppeteer of Macy's Parade (Bank Street College of Education Flora Stieglitz St
6                                                                                                    The Very Hungry Caterpillar
7                                                                                          Pookie's Thanksgiving (Little Pookie)
8                                                                                                I Love You to the Moon and Back
9                                                                                                                 Goodnight Moon
10                                                                                              The Wonderful Things You Will Be
   book_price rating                author year_of_publication
1       20.98    5.0      Michael Connelly                2023
2        4.78    4.9          Wendy Loggia                2023
3        3.78    4.9  Sherri Duskey Rinker                2011
4        4.94    4.9       Bill Martin Jr.                1967
5       12.19    4.9       Bill Martin Jr.                1967
6        5.90    4.9          David Brooks                2023
7        6.99    4.9        Alice Walstead                2022
8        4.76    4.9   Taylor Jenkins Reid                2017
9        5.36    4.9   Margaret Wise Brown                1947
10       9.40    4.9 Emily Winfield Martin                2015
                                                         genre
1  Fiction, Mystery, Thriller, Suspense, Crime, Legal Thriller
2                                     Picture Books, Biography
3                                Picture Book, Cars and Trucks
4                           Picture Book, Colors, Wild Animals
5                                        Childrens, literature
6                                                   Nonfiction
7                                        Childrens, literature
8                                           Historical Fiction
9                                        Childrens, literature
10                   Picture Books, Childrens, Fiction, Poetry
                                                                                                                        url
1            amazon.com/Resurrection-Walk-Michael-Connelly/dp/0316563765/ref=zg_bs_g_books_sccl_2/142-7825522-0098658?psc=1
2         amazon.com/Taylor-Swift-Little-Golden-Biography/dp/0593566718/ref=zg_bs_g_books_sccl_19/143-9831347-1043253?psc=1
3  amazon.com/Goodnight-Construction-Sherri-Duskey-Rinker/dp/1452111731/ref=zg_bs_g_books_sccl_27/143-9831347-1043253?psc=1
4                      amazon.com/Brown-Bear-What-You-See/dp/0805047905/ref=zg_bs_g_books_sccl_30/143-9831347-1043253?psc=1
5   amazon.com/Balloons-over-Broadway-Puppeteer-Education/dp/0547199457/ref=zg_bs_g_books_sccl_31/143-9831347-1043253?psc=1
6           amazon.com/Very-Hungry-Caterpillar-Eric-Carle/dp/0399226907/ref=zg_bs_g_books_sccl_35/143-9831347-1043253?psc=1
7   amazon.com/Pookies-Thanksgiving-Little-Pookie-Boynton/dp/166592263X/ref=zg_bs_g_books_sccl_37/143-9831347-1043253?psc=1
8                         amazon.com/I-Love-You-Moon-Back/dp/1589255518/ref=zg_bs_g_books_sccl_40/143-9831347-1043253?psc=1
9           amazon.com/Goodnight-Moon-Margaret-Wise-Brown/dp/0694003611/ref=zg_bs_g_books_sccl_44/143-9831347-1043253?psc=1
10                amazon.com/Wonderful-Things-You-Will-Be/dp/0385376715/ref=zg_bs_g_books_sccl_49/143-9831347-1043253?psc=1
top_reviews <- customer_reviews %>%
  filter(book_name %in% top_books$book_title)
review_words <- top_reviews %>%
  mutate(index = row_number()) %>%
  unnest_tokens(word, review_description)

bing Analysis

bing_results <- review_words %>%
  inner_join(get_sentiments("bing"), by = "word")

bing_results %>%
  count(book_name, sentiment)
                                                                                 book_name
1                                                 Brown Bear, Brown Bear, What Do You See?
2                                                 Brown Bear, Brown Bear, What Do You See?
3                                                                           Goodnight Moon
4                                                                           Goodnight Moon
5  Goodnight, Goodnight Construction Site (Board Book for Toddlers, Children's Board Book)
6  Goodnight, Goodnight Construction Site (Board Book for Toddlers, Children's Board Book)
7                                                          I Love You to the Moon and Back
8                                                    Pookie's Thanksgiving (Little Pookie)
9                                                    Pookie's Thanksgiving (Little Pookie)
10                                            Taylor Swift: A Little Golden Book Biography
11                                            Taylor Swift: A Little Golden Book Biography
12                                                             The Very Hungry Caterpillar
13                                                        The Wonderful Things You Will Be
   sentiment  n
1   negative  4
2   positive 17
3   negative  1
4   positive 20
5   negative  2
6   positive 19
7   positive 23
8   negative  3
9   positive 31
10  negative  1
11  positive 46
12  positive 40
13  positive 20
library(ggplot2)

bing_results %>%
  count(book_name, sentiment) %>%
  ggplot(aes(x = sentiment, y = n, fill = sentiment)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~book_name, ncol = 2) +
  theme_minimal() +
  theme(
    strip.text = element_text(size = 7),
    axis.text = element_text(size = 9))

216 words were classified as positive by the Bing lexicon. 11 words were classified as negative.

Vader Analysis

library(syuzhet)
Warning: package 'syuzhet' was built under R version 4.6.2
syuzhet_results <- top_reviews %>%
  filter(!is.na(review_description)) %>%
  mutate(
    sentiment_score = get_sentiment(
      review_description,
      method = "syuzhet"
    )
  )
syuzhet_summary <- syuzhet_results %>%
  group_by(book_name) %>%
  summarise(
    average_sentiment = mean(sentiment_score),
    total_reviews = n()
  ) %>%
  arrange(desc(average_sentiment))

syuzhet_summary
# A tibble: 8 × 3
  book_name                                      average_sentiment total_reviews
  <chr>                                                      <dbl>         <int>
1 Taylor Swift: A Little Golden Book Biography                4.00            10
2 The Very Hungry Caterpillar                                 3.76            10
3 Pookie's Thanksgiving (Little Pookie)                       2.77            10
4 I Love You to the Moon and Back                             2.42            10
5 The Wonderful Things You Will Be                            2.31            10
6 Goodnight Moon                                              1.78            10
7 Goodnight, Goodnight Construction Site (Board…              1.52            10
8 Brown Bear, Brown Bear, What Do You See?                    1.42            10

There are two titles with the highest average sentiment rating, “Taylor Swift: A Little Golden Book Biography” and “The Very Hungry Caterpiller.”

ggplot(syuzhet_summary,
       aes(x = reorder(book_name, average_sentiment),
           y = average_sentiment)) +
  scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 35)) +
  
  geom_col() +
  coord_flip() +
  theme_minimal()

Conclusion

Comparison of Sentiment Analysis Results

The original analysis used the Bing sentiment lexicon to identify positive and negative words in the book reviews. The results showed that positive words appeared much more often than negative words. Some books did not have any words classified as negative.

For the extended analysis, I used the Syuzhet lexicon to calculate sentiment scores for each review and then found the average score for each book. All eight books had positive average sentiment scores. Taylor Swift: A Little Golden Book Biography had the highest average score of approximately 3.99, while Brown Bear, Brown Bear, What Do You See? had the lowest score of approximately 1.42.

The main difference is that Bing counts individual positive and negative words, while Syuzhet gives each review a numerical sentiment score. This allowed me to look at the reviews in a different way and compare the overall sentiment between books.

Overall, both methods showed that readers generally used positive language when reviewing the books. However, Syuzhet provided another way to measure and compare the sentiment of the reviews.