text <- c("Because I could not stop for Death -",
"He kindly stopped for me -",
"The Carriage held but just Ourselves -",
"and Immortality")
text
## [1] "Because I could not stop for Death -"
## [2] "He kindly stopped for me -"
## [3] "The Carriage held but just Ourselves -"
## [4] "and Immortality"
text_df <- tibble(line = 1:4, text = text)
text_df
## # A tibble: 4 × 2
## line text
## <int> <chr>
## 1 1 Because I could not stop for Death -
## 2 2 He kindly stopped for me -
## 3 3 The Carriage held but just Ourselves -
## 4 4 and Immortality
tidy_text <- text_df %>%
unnest_tokens(word, text)
tidy_text
## # A tibble: 20 × 2
## line word
## <int> <chr>
## 1 1 because
## 2 1 i
## 3 1 could
## 4 1 not
## 5 1 stop
## 6 1 for
## 7 1 death
## 8 2 he
## 9 2 kindly
## 10 2 stopped
## 11 2 for
## 12 2 me
## 13 3 the
## 14 3 carriage
## 15 3 held
## 16 3 but
## 17 3 just
## 18 3 ourselves
## 19 4 and
## 20 4 immortality
data(stop_words)
stop_words
## # A tibble: 1,149 × 2
## word lexicon
## <chr> <chr>
## 1 a SMART
## 2 a's SMART
## 3 able SMART
## 4 about SMART
## 5 above SMART
## 6 according SMART
## 7 accordingly SMART
## 8 across SMART
## 9 actually SMART
## 10 after SMART
## # ℹ 1,139 more rows
tidy_text_no_stop <- tidy_text %>%
anti_join(stop_words, by = "word")
tidy_text_no_stop
## # A tibble: 7 × 2
## line word
## <int> <chr>
## 1 1 stop
## 2 1 death
## 3 2 kindly
## 4 2 stopped
## 5 3 carriage
## 6 3 held
## 7 4 immortality
word_counts <- tidy_text_no_stop %>%
count(word, sort = TRUE)
word_counts
## # A tibble: 7 × 2
## word n
## <chr> <int>
## 1 carriage 1
## 2 death 1
## 3 held 1
## 4 immortality 1
## 5 kindly 1
## 6 stop 1
## 7 stopped 1
original_books <- austen_books()
original_books
## # A tibble: 73,422 × 2
## text book
## * <chr> <fct>
## 1 "SENSE AND SENSIBILITY" Sense & Sensibility
## 2 "" Sense & Sensibility
## 3 "by Jane Austen" Sense & Sensibility
## 4 "" Sense & Sensibility
## 5 "(1811)" Sense & Sensibility
## 6 "" Sense & Sensibility
## 7 "" Sense & Sensibility
## 8 "" Sense & Sensibility
## 9 "" Sense & Sensibility
## 10 "CHAPTER 1" Sense & Sensibility
## # ℹ 73,412 more rows
tidy_books <- original_books %>%
unnest_tokens(word, text)
tidy_books
## # A tibble: 725,064 × 2
## book word
## <fct> <chr>
## 1 Sense & Sensibility sense
## 2 Sense & Sensibility and
## 3 Sense & Sensibility sensibility
## 4 Sense & Sensibility by
## 5 Sense & Sensibility jane
## 6 Sense & Sensibility austen
## 7 Sense & Sensibility 1811
## 8 Sense & Sensibility chapter
## 9 Sense & Sensibility 1
## 10 Sense & Sensibility the
## # ℹ 725,054 more rows
tidy_books_no_stop <- tidy_books %>%
anti_join(stop_words, by = "word")
tidy_books_no_stop
## # A tibble: 217,602 × 2
## book word
## <fct> <chr>
## 1 Sense & Sensibility sense
## 2 Sense & Sensibility sensibility
## 3 Sense & Sensibility jane
## 4 Sense & Sensibility austen
## 5 Sense & Sensibility 1811
## 6 Sense & Sensibility chapter
## 7 Sense & Sensibility 1
## 8 Sense & Sensibility family
## 9 Sense & Sensibility dashwood
## 10 Sense & Sensibility settled
## # ℹ 217,592 more rows
top_words <- tidy_books_no_stop %>%
count(word, sort = TRUE) %>%
slice_head(n = 20)
top_words
## # A tibble: 20 × 2
## word n
## <chr> <int>
## 1 miss 1855
## 2 time 1337
## 3 fanny 862
## 4 dear 822
## 5 lady 817
## 6 sir 806
## 7 day 797
## 8 emma 787
## 9 sister 727
## 10 house 699
## 11 elizabeth 687
## 12 elinor 623
## 13 hope 601
## 14 friend 593
## 15 family 578
## 16 mind 563
## 17 father 558
## 18 home 550
## 19 jane 550
## 20 mother 541
top_words %>%
ggplot(aes(x = reorder(word, n), y = n)) +
geom_col() +
coord_flip() +
labs(
title = "Most Common Words in Jane Austen's Novels",
x = "Word",
y = "Frequency"
)
## Interpretation The word frequency analysis shows which words appear
most often after common stop words are removed. Tokenization made the
text easier to analyze by separating it into one word per row. The graph
makes it easy to identify the most frequently used words and recognize
patterns in the text.
Text mining can also be useful in marketing because businesses can analyze customer reviews, social media posts, and survey responses to see what customers are talking about most. This can help marketers understand customer interests, opinions, and common topics and use that information to make better marketing decisions.