R: tidytext Jane Austen Book

From OnnoWiki
Jump to navigation Jump to search
library(janeaustenr)
library(dplyr)
library(stringr)

original_books <- austen_books() %>%
  group_by(book) %>%
  mutate(linenumber = row_number(),
         chapter = cumsum(str_detect(text, regex("^chapter [\\divxlc]",
                                                 ignore_case = TRUE)))) %>%
  ungroup()
original_books
# konversi ke token
#
library(tidytext)
tidy_books <- original_books %>%
  unnest_tokens(word, text)
tidy_books
# buang stopwords
#
data(stop_words)
tidy_books <- tidy_books %>%
  anti_join(stop_words)
# count
#
tidy_books %>%
  count(word, sort = TRUE) 
# visualisasi
#
library(ggplot2)
tidy_books %>%
  count(word, sort = TRUE) %>%
  filter(n > 600) %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(word, n)) +
  geom_col() +
  xlab(NULL) +
  coord_flip()

Pranala Menarik