Jane Austen 소설 속 Term Frequency
install.packages("tidytext")
install.packages("tidyverse")
install.packages("janeaustenr")
library(tidytext)
library(tidyverse)
library(janeaustenr)
# 단어의 빈도를 구함
janeaustenr::austen_books() %>%
tidytext::unnest_tokens(output = word, input = text) %>%
dplyr::count(book, word, sort = TRUE) %>%
dplyr::ungroup() -> book_words
# 소설별로 단어 빈도의 합계를 구함
book_words %>%
dplyr::group_by(book) %>%
dplyr::summarise(total = sum(n)) -> total_words
# 데이터 합치기 : left join
book_words <- dplyr::left_join(book_words, total_words)
# 단어를 합계로 나눈 값을 기준으로 히스토그램 작성
book_words %>%
ggplot2::ggplot(mapping = aes(x = n/total, fill = book)) +
ggplot2::geom_histogram(show.legend = FALSE) +
ggplot2::xlim(NA, 0.0009) +
ggplot2::facet_wrap(~book, ncol = 2, scales = "free_y")
[ 출처] R로 배우는 텍스트마이닝, 줄리아 실기/데이비드 로빈슨 지음, 박진수 옮김, Jpub, p38~39