1. 트위터 접근

#if(!require(twitteR)) {install.packages("twitteR"); library(twitteR)}
 
library(twitteR)

#api_key <- "..."
#api_secret <- "..."
#access_token <- "..."
#access_secret <- "..."
#setup_twitter_oauth(api_key,api_secret,access_token,access_secret)

2. 멘션 수집 및 정제

str <- "잼라이브"
num <- 300

keyword <- enc2utf8(str)

# 키워드를 통한 트위터 멘션 수입
tweets <- searchTwitter(keyword, n=num, lang="ko") # 

# 내용 확인
temp <- tweets[[1]]
#temp$getScreenName() # 화면상에 표시되는 사용자 이름
temp$getText()

# plyr::laply 를 활용하여 트위터 멘션만을 저장
if(!require(plyr)) {install.packages("plyr"); library(plyr)}
tweets.text <- laply(tweets, function(t) t$getText())
length(tweets.text)
head(tweets.text,3)


require(stringr)

text <- tweets.text
x <- gsub("[^A-Za-z가-힣[:space:][:digit:][:punct:]]", "", text)
x <- gsub("@|\n|RT", "", x)
x <- gsub("[[:punct:]]", " ", x)
x <- gsub("[[:digit:]]", "", x)
x <- tolower(x)
x <- gsub("[a-z]", "", x)
x <- str_trim(x)
x

3. 명사 추출

library(KoNLP)
x1 <- lapply(x, extractNoun)
x2 <- lapply(x1, function(x) x[nchar(x)>1])
x3 <- do.call(c, x2)
o <- table(x3)

4. 워드 클라우드 그리기

library(wordcloud)
pal <- brewer.pal(8,"Dark2")
wordcloud(names(o), o, min.freq=3, random.order=F,random.color=T,colors=pal,family="AppleGothic")