ComputerLanguage_Program/R
[R] 워드 클라우드 생성
pbj0812
2020. 3. 26. 02:48
0. 목표
- xlsx 파일 내용을 읽은 후 해당 내용의 빈도수를 통한 워드클라우드 생성
- 데이터 형태(네이트 기사 복붙)
1. 코드 작성
1) 라이브러리 install & import
- NLP4kec는 제대로 설치되지 않아 여기서 다운받고 위치 지정
# install
#install.packages("tidyverse")
#install.packages("rJava")
#install.packages("C:/Users/user/Desktop/NLP4kec_1.3.0.zip", repos = NULL)
#install.packages("wordcloud2")
#install.packages("RColorBrewer")
# import
library(tidyverse) # 데이터 전처리(stringr)와 그래프(ggplot2)
library(rJava) # NLP4kec 사용 목적
library(NLP4kec) # 자연어 처리
library(wordcloud) # 워드클라우드
library(wordcloud2) # 워드클라우드2
2) xlsx 파일 읽기
# 파일 읽기
data <- read_excel(path = "C:/Users/user/Desktop/sample.xlsx",
sheet = "Sheet1",
col_names = TRUE)
3) 문장 합산
- 문장들을 하나로 묶음
# 문자열 합치기
sentense <- ""
for(i in 1:nrow(data)){
sentense <- paste(sentense, data[i,1])
}
4) 한글 제외 문자 삭제
# 한글 제외 문자 삭제
sentense <- sentense %>% stringr::str_remove_all(pattern = "[^가-힣]")
5) 단어 구분
# 단어 구분
data2 <- NLP4kec::r_parser_r(contentVector = sentense, language = "ko")
6) 단어 쪼개기
# 리스트 형태로 나누기
data3 <- strsplit(data2, split=" ")
7) 테이블화
# 테이블화
wordcount <- table(data3)
8) 탑50 선정
wordcount_top <- head(sort(wordcount, decreasing = T), 50)
9) wordcloud
wordcloud(names(wordcount_top), wordcount_top)
10) wordcloud2
wordcloud2::wordcloud2(wordcount_top,
color = "random-light",
fontFamily = "NanumGothic")
2. 참고
1) 코드 참조1
2) 코드 참조2
3) xlsx 읽기
4) NPL4kec