R/TextMining

AP 뉴스에 있는 긍정/부정 단어들 현황

이부일 2020. 4. 1. 10:53

AP 뉴스에 있는 긍정/부정 단어들 현황

 

install.packages("tm")
install.packages("tidytext")
install.packages("topicmodels")
library(tm)
library(tidytext)
library(topicmodels)

 

 

# topicmodels 패키지에서 제공하는 AP뉴스 데이터 : DTM 형식
data("AssociatedPress")

 

 

# DTM을 tidy 데이터로 변경하기
# Bing 감성사전과 합치기
ad_sentiments <- tidytext::tidy(AssociatedPress) %>%
    dplyr::inner_join(tidytext::get_sentiments("bing"), by = c(term = "word"))

 

 

# AP 뉴스에서 등장하는 긍정/부정 단어들의 현황
ad_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_col() +
     ggplot2::coord_flip()

 

 

 

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