R/TextMining

긍정 정서나 부정 정서에 가장 큰 기여를 한 단어들

이부일 2019. 10. 30. 00:05

# 패키지 설치하기와 로딩하기
install.packages("tm")
install.packages("topicmodels")
install.packages("tidyverse")
install.packages("tidytext")
library(tm)
library(topicmodels)
library(tidyverse)
library(tidytext)

 

 

# 데이터 불러오기
data("AssociatedPress")
AssociatedPress

 

 

# tidy data 만들기
ap_td <- tidytext::tidy(AssociatedPress)

 

 

# 정서분석 데이터 만들기
ap_td %>%
dplyr::inner_join(tidytext::get_sentiments("bing"), by = c(term = "word")) -> ap_sentiments

 

 

# 정서에 기여하는 단어들
ap_sentiments %>%
dplyr::count(sentiment, term, wt = count) %>%
dplyr::ungroup() %>%
dplyr::filter(n >= 200) %>%
dplyr::mutate(n = ifelse(sentiment == "negative", -n, n)) %>%
dplyr::mutate(term = reorder(term, n)) %>%
ggplot2::ggplot(mapping = aes(x = term, y = n, fill = sentiment)) +
ggplot2::geom_bar(stat = "identity") +
ggplot2::ylab("Contribution to sentiment") +
ggplot2::coord_flip()

 

[출처] R로 배우는 텍스트마이닝, 줄리아 실기/데이비드 로빈슨 지음, 박진수 옮김, 제이펍, p85~88