역대 대통령 취임식 연설 텍스트 마이닝

데이터 준비하기

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readxl)
library(stringr)
speech <- read_excel("PREVIOUS PRESIDENTS SPEECH IN KOREA.xlsx")
speech_copy <- speech
summary(speech_copy)
##   president            title             context         
##  Length:19          Length:19          Length:19         
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character
speech_copy$context <- str_replace_all(speech_copy$context, "\\W", " ")

단어 빈도표 만들기

library(KoNLP)
## Checking user defined dictionary!
library(dplyr)
nouns <- extractNoun(speech_copy$context)
wordcount <- table(unlist(nouns))
df_word <- as.data.frame(wordcount, stringsAsFactors = F)
df_word <- dplyr::rename(df_word,
                  word = Var1,
                  freq = Freq)

두 글자 이상으로 된 단어를 추출하고, 빈도 순으로 정렬해 가장많이 사용된 단어 20개를 추출

df_word <- filter(df_word, nchar(word) >= 2)
top30 <- df_word %>% 
  arrange(desc(freq)) %>% 
  head(30)
top30
##        word freq
## 1      우리  670
## 2      국민  409
## 3    여러분  185
## 4      사회  137
## 5      경제  116
## 6      시대  116
## 7      세계  113
## 8      정부  113
## 9      민족  105
## 10     역사   97
## 11     평화   91
## 12     오늘   89
## 13     발전   87
## 14     나라   81
## 15   대통령   80
## 16     국가   77
## 17     사람   73
## 18     문화   72
## 19     자유   63
## 20     본인   60
## 21 민주주의   59
## 22     정치   59
## 23     들이   58
## 24     문제   53
## 25     민국   52
## 26     조국   51
## 27     대한   50
## 28     노력   48
## 29     통일   46
## 30     번영   44

단어 빈도 막대 그래프 만들기

library(ggplot2)
order1 <- arrange(top30, freq)$word
ggplot(data = top30, aes(x = word, y = freq)) +
  ylim(0, 700) +
  geom_col() +
  coord_flip() +
  scale_x_discrete(limit = order1) +
  geom_text(aes(label = freq), hjust = -0.3)

워드 클라우드 만들기

library(wordcloud)
## Loading required package: RColorBrewer
library(RColorBrewer)
pal <- brewer.pal(9, "Blues")[5:9]
set.seed(1234)

wordcloud(word = df_word$word,
          freq = df_word$freq,
          min.freq = 2,
          max.words = 200,
          random.order = F,
          rot.per = .1,
          scale = c(5, 0.5),
          colors = pal)