User Tools

Site Tools


text_mining_example_with_korean_songs

This is an old revision of the document!


mm.xlsx

library(bitops)
library(RCurl)
library(KoNLP)
library(rJava)
library(tm)
library(wordcloud)
library(XLConnect)

# set your data dir in which the save file is located.
setwd ("D:/Users/Hyo/Clouds/Cs-Ds/CS/MusicStudy")

rm(list=ls())
music<- file.path("mm.xlsx")

music90s <- readWorksheetFromFile(music, sheet="1990s")
# use VectorSource 
lyrics<- Corpus(VectorSource(music90s$lyrics))
result.text <- lyrics

inspect(result.text[1:5])

# removeTwitSign <- function(x) { gsub("@[[:graph:]]*","",x) } # may not be used in this case
# removeURL <- function(x) { gsub("http://[[:graph:]]*","",x) } # may not be used in this case
# removeEnter <- function(x) { gsub("\n","",x) } 
# exNouns <- function(x) { paste(extractNoun(x), collapse=" ")}

# NA -> "" 로 변환
result.text[is.na(result.text)]   <- ""

useSejongDic()
mergeUserDic(data.frame(c(
"개여울", "고운정", "못잊어", "내마음", "가시내", "꽃가마", "내곁", "윙크",
"못잊어서"
), c("ncn")))

result_nouns <- sapply(result.text, removeEnter)
result_nouns <- sapply(result.text, exNouns)

myCorpus <- Corpus(VectorSource(result_nouns))
myCorpus <- tm_map(myCorpus, removePunctuation)
myCorpus <- tm_map(myCorpus, removeNumbers)
myCorpus <- tm_map(myCorpus, tolower)
myStopwords <- c(stopwords('english'), "rt")
myCorpus <-tm_map(myCorpus, removeWords, myStopwords)

inspect(myCorpus[1:5])

myTdm <- TermDocumentMatrix(myCorpus, control=list(wordLengths=c(1,Inf)))
mat <- as.data.frame(as.matrix(myTdm))
write.table(mat, file="_lyrics_90s1.txt", col.names=FALSE, row.names=TRUE,sep="\t")

myTdm2 <- TermDocumentMatrix(myCorpus, control=list(wordLengths=c(2,Inf)))
mat2 <- as.data.frame(as.matrix(myTdm2))
write.table(mat2, file="_lyrics_90s2.txt", col.names=FALSE, row.names=TRUE,sep="\t")

par(mfrow = c(1,2))

pal <- brewer.pal(8,"Dark2")
 
# 폰트 세팅. 띄어쓰기나 대소문자에 민감하다는 점에 주의
# 맑은고딕 : windowsFonts(malgun=windowsFont("맑은 고딕"))
# 나눔고딕 : windowsFonts(malgun=windowsFont("나눔고딕"))
windowsFonts(malgun=windowsFont("맑은 고딕"))
 
m <- as.matrix(myTdm)
v <- sort(rowSums(m), decreasing=TRUE)
myNames <- names(v)
d <- data.frame(word=myNames, freq=v)
wordcloud(d$word, d$freq, scale=c(7,0.8), min.freq=3, random.order=F, rot.per=.1, colors=pal, family="malgun")

m2 <- as.matrix(myTdm2)
v2 <- sort(rowSums(m2), decreasing=TRUE)
myNames2 <- names(v2)
d2 <- data.frame(word=myNames2, freq=v2)
wordcloud(d2$word, d2$freq, scale=c(7,0.8), min.freq=3, random.order=F, rot.per=.1, colors=pal, family="malgun")

90s.jpg

text_mining_example_with_korean_songs.1481074302.txt.gz · Last modified: 2016/12/07 10:01 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki