개발자입니다
섹션 8. Chapter 08 KoNLP 패키지 (Youtube HelloJany) 본문
■ KoNLP 패키지
한글을 분석한다
형태소(Morpheme)는 언어에서 뜻을 가진 가장 작은 단위. '사랑'은 뜻이 있으나 '사'와 '랑'은 뜻이 없음
-R 버전 문제로 설치 안될때 KoNLP 패키지 설치 방법
1. 자바 설치
2. rtools40 설치
https://cran.yu.ac.kr/bin/windows/Rtools/
3. R studio에서 이제 순서대로 설치 및 시행
## KoNLP 깔기 전에 필요한 패키지들 먼저 설치해줍시다
install.packages("rJava")
install.packages("memoise")
install.packages("multilinguer")
library(rJava)
library(memoise)
library(multilinguer)
install_jdk()
## 의존성 패키지 설치
install.packages(c('stringr', 'hash', 'tau', 'Sejong', 'RSQLite', 'devtools'), type = "binary")
install.packages("remotes")
## 깃허브 통해 KoNLP 다운로드
remotes::install_github('haven-jeon/KoNLP', upgrade = "never", INSTALL_opts=c("--no-multiarch"))
또는
remotes::install_github('haven-jeon/KoNLP', upgrade = "never", force = TRUE, INSTALL_opts=c("--no-multiarch"))
## 패키지 불러오기
library(KoNLP)
## NIADic 설치 및 불러오기
devtools::install_github('haven-jeon/NIADic/NIADic', build_vignettes = TRUE)
useNIADic()
니아에서 만들었다던 한글사전? 패키지.. 라고 한다 이것도 깃허브 통해 다운로드
wordcloud2는 GUI로 install
wordcloud2 아래와 같이 viewer에 나타남
library(KoNLP)
library(wordcloud2)
wordcloud2(demoFreq)
-애국가로 형태소 분석하기
형태소 분석 과정: 텍스트 수집 → 분해 → 단어 추출 → 정제 → 정형 데이터 생성 → 분석 → 시각화
행정안전부 애국가 가사 다운로드: https://www.mois.go.kr/cmm/fms/FileDown.do?atchFileId=FILE_000000000048240&fileSn=0
아쉽게도 한글 깨짐 등 에러가 다발해서 아래 코드는 실행되지 않는다.
#8장 애국가 분석하여 워드클라우드 만들기
#wordcloud2 패키지 버그가 있는 경우는 이걸 쓰세요.
#install.packages("devtools")
#library(devtools)
#devtools::install_github("lchiffon/wordcloud2")
#KoNLP 패키지 설치
install.packages("KoNLP")
#wordcloud2 패키지 설치
install.packages("wordcloud2")
library(KoNLP)
library(wordcloud2)
#useSystemDic()
#세종사전
useSejongDic()
#useNIADic()
# 텍스트파일 데이터를 word_data에 할당
word_data <- readLines("애국가(가사).txt")
# word_data 변수의 각 행에서 명사를 추출하여 word_data2 변수에 할당
word_data2 <- sapply(word_data, extractNoun, USE.NAMES = F) # extractNoun : 명사 추출 함수
# 추가할 단어를 add_words 변수에 할당
add_words <-c("백두산", "남산", "철갑", "가을", "하늘", "달")
# add_words 변수의 데이터를 사용자 정의 단어(user_dic)로 추가
buildDictionary(user_dic = data.frame(add_words,rep("ncn",length(add_words))),replace_usr_dic = T)
get_dictionary('user_dic')
# word_data 변수의 각 행에서 명사를 word_data2 변수에 할당
word_data2 <- sapply(word_data, extractNoun, USE.NAMES = F)
# word_data2를 벡터로 변환한 후 undata 변수에 할당
undata <- unlist(word_data2)
# undata의 빈도 수 확인 후 word_table에 할당
word_table <- table(undata)
# undata에서 두 글자 이상인 단어만 선별한 후 undata2 변수에 할당
Sys.setlocale(category = "LC_ALL", locale = "us") # 아래 코드 Error in nchar(x) : invalid multibyte string, element 1 에러 발생하여 해당 코드 추가
undata2 <- Filter(function(x){nchar(x)>=2}, undata)
# undata2의 빈도 확인 후 word_table2 변수에 할당
word_table2 <- table(undata2)
# word_tabel2를 내림차순 정렬
sort(word_table2, decreasing = T)
# word_table2의 워드클라우드 생성
wordcloud2(word_table2)
# color(색상), backgroundColor(배경색) 옵션 사용
wordcloud2(word_table2, color = "random-light", backgroundColor = "black")
# fontFamily(글꼴), size(크기), shape(모양) 옵션 사용
wordcloud2(word_table2, fontFamily = "맑은 고딕", size = 1.2, color = "random-light", backgroundColor = "black", shape="star")
'R' 카테고리의 다른 글
섹션 10. Chapter 10 ggplot2 패키지 (Youtube HelloJany) (31) | 2022.09.08 |
---|---|
섹션 9. Chapter 09 dplyr 패키지 (Youtube HelloJany) (0) | 2022.09.08 |
섹션 7. Chapter 07 reshape2 패키지 (Youtube HelloJany) (0) | 2022.09.08 |
섹션 6. Chapter 06 패키지 (Youtube HelloJany) (0) | 2022.09.07 |
섹션 5. Chapter 05 데이터 가공 (Youtube HelloJany) (0) | 2022.09.07 |