<힙합 가사 텍스트 마이닝 Exercise>
[ ⅰ. 데이터 마이닝 준비하기 ]
데이터 마이닝 작업을 하기 위해서는 한국어 자연어 패키지(KoNLP)를 활용해야 한다. R에서 이 패키지를 다운로드하는 방법이 제법 까다로워졌는데 교재에 기재된 다운로드 순서는 다음과 같다. * JDK가 이미 설치되어 있다는 가정하에 진행하였다.
-
Rtools를 설치한다
-
아래 코드를 R Studio에서 차례대로 실행한다
더보기
install.packages("rJava")
install.packages("memoise")
install.packages("multilinguer")
install.packages(c("stringr", "hash", "tau", "Sejong", "RSQLite", "devtools"), type = "binary")
install.packages("remotes")
remotes::install_github("haven-jeon/KoNLP",
upgrade = "never",
INSTALL_opts=c("--no-multiarch"))
[ ⅱ. 가장 많이 사용된 단어 알아보기 ]
- Package Load → 사전 설정하기 → 특수문자 제거
- 단어 빈도표 생성 → 가장 많이 사용된 단어 알아보기
더보기
### Step 1:
library(KoNLP)
library(dplyr)
# KoNPL에서 지원하는 NIA 사전 → 형태소를 분석하는 데 이용
useNIADic()
# 데이터 불러오기
txt <- readLines("hiphop.txt")
head(txt)
# 특수문자 제거
library(stringr)
txt <- str_replace_all(txt, "\\W"," ")
### Step 2:
#명사 추출하기
nouns <- extractNoun(txt)
wordcount <- table(unlist(nouns)) # 단어별 빈도표
df_word <- as.data.frame(wordcount, stringsAsFactors = F)
df_word <- rename(df_word, word=Var1, freq=Freq)
head(df_word)
# 2글자 이상인 단어만 활용
df_word <- filter(df_word, nchar(word)>=2)
# 상위 20개 단어 추출
top20 <- df_word %>% arrange(desc(freq)) %>% head(20)
top20
[ ⅲ. 워드 클라우드 만들기 ]
- Package Load → 단어 색상 목록 → 난수 고정하기(for 동일한 모양의 워드클라우드 생성)
- wordcloud( )
words | freq | min.freq | max.words | random.order | rot.per | scale | colors |
출력할 단어 목록 |
참조 빈도수 | 최소 단어 빈도 |
표현 단어 수 | 고빈도 단어 중앙 배치 |
단어 회전 비율 |
단어 크기 | 색상 목록 |
더보기
install.packages("wordcloud")
library(wordcloud)
library(RColorBrewer)
pal <- brewer.pal(8, "Dark2") # 색상 팔레트
set.seed(1234) # 난수 고정
wordcloud(words = df_word$word,
freq = df_word$freq,
min.freq = 2,
max.words = 200,
random.order = F,
rot.per = 0.1,
scale = c(4, 0.5),
colors = pal)
<국정원 트위터 텍스트 마이닝 Exercise>
- 데이터 준비
- 단어 빈도표 작성 → 막대 그래프로 시각화
- 워드 클라우드
더보기
library(KoNLP)
library(dplyr)
### Step 1:
twitter <- read.csv("twitter.csv", header = T, stringsAsFactors = F, fileEncoding = "UTF-8")
str(twitter)
# 한글 변수 → 영문 변수
twitter <- rename(twitter, no='번호', id='계정이름' , date='작성일' , tw='내용')
# 특수문자 제거
twitter$tw <- str_replace_all(twitter$tw, "\\W", " ")
### Step 2:
nouns <- extractNoun(twitter$tw)
wordcnt <- table(unlist(nouns))
df_words <- as.data.frame(wordcnt, stringsAsFactors = F)
df_words <- rename(df_words, word=Var1, freq=Freq)
df_words <- filter(df_words, nchar(word) >= 2)
top20 <- df_words %>% arrange(desc(freq)) %>% head(20)
top20
order <- arrange(top20, freq)$word
library(ggplot2)
ggplot(data = top20, aes(x=word, y=freq)) +
ylim(0,1500) +
geom_col() +
coord_flip() +
scale_x_discrete(limit=order)+
geom_text(aes(label=freq, hjust=-0.5))
### Step 3:
library(wordcloud)
library(RColorBrewer)
pal <- brewer.pal(8, "Dark2")
set.seed(1234)
wordcloud(words = df_words$word,
freq = df_words$freq,
min.freq = 10,
max.words = 200,
random.order = F,
rot.per = 0.1,
scale = c(5, 0.5),
colors = pal
)
'데이터 분석 > R' 카테고리의 다른 글
Part ⅩⅠ: 지도 시각화 (0) | 2021.01.11 |
---|---|
Part Ⅸ: 한국 복지 패널 데이터 분석 (0) | 2021.01.07 |
Part Ⅷ: 데이터 시각화 (0) | 2021.01.07 |
Part Ⅶ: 데이터 정제(결측치 / 이상치) (0) | 2021.01.07 |
Part Ⅵ: 데이터 전처리 (0) | 2021.01.06 |
Part Ⅴ: 데이터 분석 기초 (0) | 2021.01.05 |