library(dplyr)
library(tidyr)
library(ggplot2)
library(tidytext)

The data

text <- c("I love the quality of this product. The price is reasonable, and delivery was fast.",
          "The product quality is excellent, but the shipping took too long.",
          "Great customer service! I would definitely recommend this product to my friends.",
          "The price is too expensive. However, the product looks beautiful.",
          "Fast delivery and excellent service. I am very satisfied with my purchase.")

text_df <- tibble(review = 1:5, text = text)
text_df
## # A tibble: 5 × 2
##   review text                                                                   
##    <int> <chr>                                                                  
## 1      1 I love the quality of this product. The price is reasonable, and deliv…
## 2      2 The product quality is excellent, but the shipping took too long.      
## 3      3 Great customer service! I would definitely recommend this product to m…
## 4      4 The price is too expensive. However, the product looks beautiful.      
## 5      5 Fast delivery and excellent service. I am very satisfied with my purch…

1. Tidy text format

tidy_reviews <- text_df %>%
  unnest_tokens(word, text)

data(stop_words)

tidy_clean <- tidy_reviews %>%
  anti_join(stop_words, by = "word")

tidy_clean %>%
  count(word, sort = TRUE)
## # A tibble: 17 × 2
##    word           n
##    <chr>      <int>
##  1 product        4
##  2 delivery       2
##  3 excellent      2
##  4 fast           2
##  5 price          2
##  6 quality        2
##  7 service        2
##  8 beautiful      1
##  9 customer       1
## 10 expensive      1
## 11 friends        1
## 12 love           1
## 13 purchase       1
## 14 reasonable     1
## 15 recommend      1
## 16 satisfied      1
## 17 shipping       1
tidy_clean %>%
  count(word, sort = TRUE) %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(n, word)) +
  geom_col() +
  labs(x = "Count", y = NULL)

2. Sentiment analysis

review_sentiment <- tidy_reviews %>%
  inner_join(get_sentiments("bing"), by = "word") %>%
  count(review, sentiment) %>%
  pivot_wider(names_from = sentiment, values_from = n, values_fill = 0) %>%
  mutate(sentiment = positive - negative)

review_sentiment
## # A tibble: 5 × 4
##   review positive negative sentiment
##    <int>    <int>    <int>     <int>
## 1      1        3        0         3
## 2      2        1        0         1
## 3      3        2        0         2
## 4      4        1        1         0
## 5      5        3        0         3
ggplot(review_sentiment, aes(factor(review), sentiment)) +
  geom_col() +
  labs(x = "Review", y = "Net sentiment")

3. tf-idf

review_tf_idf <- tidy_clean %>%
  count(review, word) %>%
  bind_tf_idf(word, review, n) %>%
  arrange(desc(tf_idf))

review_tf_idf
## # A tibble: 26 × 6
##    review word           n    tf   idf tf_idf
##     <int> <chr>      <int> <dbl> <dbl>  <dbl>
##  1      2 shipping       1 0.25   1.61  0.402
##  2      4 beautiful      1 0.25   1.61  0.402
##  3      4 expensive      1 0.25   1.61  0.402
##  4      3 customer       1 0.2    1.61  0.322
##  5      3 friends        1 0.2    1.61  0.322
##  6      3 recommend      1 0.2    1.61  0.322
##  7      5 purchase       1 0.167  1.61  0.268
##  8      5 satisfied      1 0.167  1.61  0.268
##  9      1 love           1 0.143  1.61  0.230
## 10      1 reasonable     1 0.143  1.61  0.230
## # ℹ 16 more rows

Findings

The reviews are mostly positive (10 positive words vs. 1 negative). Customers like the product quality, fast delivery and service, but price (“expensive”) and slow shipping are the main complaints.

Challenge

When I removed stop words before the sentiment analysis, the word “great” was deleted because it is on the stop word list. I fixed this by running sentiment on all the words and only removing stop words for the word counts and tf-idf.