ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 문재인 대통령 취임사의 워드 클라우드
    R/TextMining 2019. 11. 8. 12:27

    # 패키지 설치하기와 로딩하기

    install.packages("tidyverse")
    install.packages("tidytext")
    install.packages("KoNLP")
    install.packages("reshape2")
    library(tidyverse)
    library(tidytext)
    library(KoNLP)
    library(reshape2)

     

     

    # 작업공간 설정하기

    setwd("d:/president/")

     

     

    # 연설문 읽어오기 및 명사 추출하기

    readr::read_lines(file = "19문재인.txt") %>%
    KoNLP::SimplePos22() %>%
    reshape2::melt() %>%
    tibble::as_tibble() %>%
    dplyr::select(L1, value) %>%
    dplyr::mutate(noun = stringr::str_match(string = value, pattern = "([가-힣]+)/NC")[ , 2]) %>%
    na.omit() %>%
    dplyr::mutate(length = stringr::str_length(noun)) %>%
    dplyr::filter(length >= 2) -> moon_df

     

     

    # 불완전하게 추출된 명사들을 수정하기

    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "대통령의", replacement = "대통령")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "국민들", replacement = "국민")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "친절한", replacement = "친절")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "사랑하", replacement = "사랑")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "존경하", replacement = "존경")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "진정한", replacement = "진정")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "소통하", replacement = "소통")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "불행한", replacement = "불행")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "일하", replacement = "일")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "가능할", replacement = "가능")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "강력한", replacement = "강력")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "겸손한", replacement = "겸손")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "겸허한", replacement = "겸허")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "경험하지", replacement = "경험")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "계속되", replacement = "계속")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "국민이", replacement = "국민")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "권위적", replacement = "권위")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "노심초사하", replacement = "노심초사")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "막강한", replacement = "막강")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "막중한", replacement = "막중")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "불가능한", replacement = "불가능")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "불리한", replacement = "불리")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "삼고초려해서", replacement = "삼고초려")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "위대한", replacement = "위대")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "선대들이", replacement = "선대")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "일관되", replacement = "일관")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "후보들", replacement = "후보")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "젊은이들", replacement = "젊은이")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "약속드립니", replacement = "약속")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "선택해주", replacement = "선택")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "행사할", replacement = "행사")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "해결할", replacement = "해결")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "필요하면", replacement = "필요")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "평범한", replacement = "평범")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "평등할", replacement = "평등")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "평가하", replacement = "평가")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "퇴임하", replacement = "퇴임")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "통치하", replacement = "통치")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "토론하", replacement = "토론")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "큰소리치", replacement = "큰소리")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "참모들", replacement = "참모")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "좌절하지", replacement = "좌절")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "종식되", replacement = "종식")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "조성되", replacement = "조성")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "정치적", replacement = "정치")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "강화하기", replacement = "강화")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "공정한", replacement = "공정")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "공정할", replacement = "공정")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "솔직한", replacement = "솔직")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "소명감", replacement = "소명")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "시민들", replacement = "시민")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "약속들", replacement = "약속")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "외면하지", replacement = "외면")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "유능한", replacement = "유능")
    moon_df$noun <- stringr::str_replace(string = moon_df$noun, pattern = "전국적", replacement = "전국")

     

     

    # 워드 클라우드 작성하기

    moon_df %>%
    dplyr::count(noun, sort = TRUE) %>%
    wordcloud2::wordcloud2()

     

     

    댓글 0

Designed by Tistory.