Introduction

This exercise analyzes five sample customer comments. I convert the comments into individual words, remove stop words, and identify the most frequently mentioned words.

Original Comments

The comments begin as sentences containing multiple words. I give each comment an ID so I can track where each word came from.

reviews <- tibble(
  review_id = 1:5,
  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."
  )
)

reviews
## # A tibble: 5 × 2
##   review_id text                                                                
##       <int> <chr>                                                               
## 1         1 I love the quality of this product. The price is reasonable, and de…
## 2         2 The product quality is excellent, but the shipping took too long.   
## 3         3 Great customer service! I would definitely recommend this product t…
## 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 pu…

Tokenization

Tokenization separates text into smaller units. The unnest_tokens() function creates one word per row, converts words to lowercase, and removes punctuation.

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

head(tidy_reviews, 20)
## # A tibble: 20 × 2
##    review_id word      
##        <int> <chr>     
##  1         1 i         
##  2         1 love      
##  3         1 the       
##  4         1 quality   
##  5         1 of        
##  6         1 this      
##  7         1 product   
##  8         1 the       
##  9         1 price     
## 10         1 is        
## 11         1 reasonable
## 12         1 and       
## 13         1 delivery  
## 14         1 was       
## 15         1 fast      
## 16         2 the       
## 17         2 product   
## 18         2 quality   
## 19         2 is        
## 20         2 excellent

Removing Stop Words

Stop words are common words such as “the,” “and,” and “is” that often provide little information for a word frequency analysis. I remove them using anti_join().

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

clean_reviews
## # A tibble: 26 × 2
##    review_id word      
##        <int> <chr>     
##  1         1 love      
##  2         1 quality   
##  3         1 product   
##  4         1 price     
##  5         1 reasonable
##  6         1 delivery  
##  7         1 fast      
##  8         2 product   
##  9         2 quality   
## 10         2 excellent 
## # ℹ 16 more rows

Word Frequencies

I count how often each remaining word appears across the five comments.

word_counts <- clean_reviews %>%
  count(word, sort = TRUE)

word_counts
## # 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

Word Frequency Graph

This graph shows words that appear at least twice in the sample.

word_counts %>%
  filter(n >= 2) %>%
  ggplot(aes(x = n, y = reorder(word, n))) +
  geom_col(fill = "steelblue") +
  scale_x_continuous(breaks = 0:4) +
  labs(
    title = "Frequently Mentioned Words in Customer Comments",
    x = "Number of Occurrences",
    y = NULL
  ) +
  theme_minimal()

Source

Method adapted from Julia Silge and David Robinson’s Text Mining with R: A Tidy Approach, Sections 1.1–1.3. The five sample comments were provided in the assignment.

https://www.tidytextmining.com/tidytext