트위터 글로는 Word Cloud 그리는건 많지만 페이스북은 잘 안보여서 뚝딱뚝닥 해봤다.
(일단 트위터는 Oauth가 바껴서 귀찮.... 페이스북은 Access Token만 있으면 돼서..)
내가 좋아하는 골프존의 페이스북에 올라온 포스트에 대해서 Word Cloud를 만들었다.
만드는 김에 데이터 가져올때 likes 카운트도 가지고 왔는데 나중에 쓸모가 있겠지...
역시 ..... 골프존이라 골프가 제일커....
# facebook 데이터를 가져오기 위해서는 permission이 필요하고 전체 newsfeed를 가져오려면 권한 중 read_stream 권한이 필요하다. access_token <- "[Access Token]" require(RCurl) require(rjson) require(tm) options(RCurlOptions = list(capath = system.file("CurlSSL", "cacert.pem", package = "RCurl"), ssl.verifypeer = FALSE)) # Facebook Graph API 용 쿼리 함수 FQLQuery.facebook <- function(script="",access_token, path) { fromJSON(getURL(sprintf( "https://graph.facebook.com/%s?%s&access_token=%s", path, script, access_token))) } path = "golfzon" myposts <- FQLQuery.facebook("fields=posts.fields(message,likes)&limit=100&offset=0", access_token, path) post.id <- sapply(myposts$posts$data, function(x) x$id) post.messages <- sapply(myposts$posts$data, function(x) x$message) post.likes <- sapply(myposts$posts$data, function(x) x$likes$count) post.createdtime <- sapply(myposts$post$data, function(x) x$created_time) # 문자열 정리 post.messages <- gsub("\n","",post.messages) post.messages <- gsub("\r","",post.messages) post.messages <- removePunctuation(post.messages) # result <- data.frame( post.id = post.id, message = post.messages, likes_count = post.likes, createdTime = post.createdtime) # 한글 형태소 분석 실시 require(KoNLP) require(tm) require(wordcloud) # 텍스트 마이닝 정제 및 wordcloud 생성용 함수 wordcloud2 <- function (data, min.count, pal) { # text mining을 위한 형태로 정제 data.dt <- data data.dt <- gsub("\n","",data.dt) data.dt <- gsub("\r","",data.dt) data.dt <- gsub("RT","",data.dt) # 명사 분리 result_nouns <- Map(extractNoun,data.dt) result_word <- unlist(result_nouns,use.name=F) # 글자수가 2 이상인 것만 남김 result_word <- Filter(function(x) { nchar(x) >=2 }, result_word) # 글자수가 과도하게 긴 것은 제외 result_word <- Filter(function(x) { nchar(x) <=10 }, result_word) # 문자 Counting result_word.count <- table(result_word) # 일정 Count 이상 되는 것으로 제한 result_word.count <- result_word.count[result_word.count >= min.count] # 폰트 세팅 windowsFonts(malgun=windowsFont("맑은 고딕")) # Wordcloud wordcloud(names(result_word.count), freq=result_word.count, scale=c(5,.5), min.freq=5, random.order=F, family="malgun", colors=pal) } pal <- brewer.pal(12, "Paired") wordcloud2(result[,'message'], 2,pal)
'Data Analysis > R' 카테고리의 다른 글
R : odbc를 사용하여 MSSQL 접속 (0) | 2013.11.18 |
---|---|
R : XML 데이터를 Parsing하여 데이터로 처리하기 (2) | 2013.11.15 |
R : R script에서 다른 R script 가지고 오기(Source) (0) | 2013.09.10 |
R : 문자로 된 날짜를 Date Type으로 변경하기(as.Date(), strptime()) (0) | 2013.09.09 |
R : get 함수 (0) | 2013.09.03 |
댓글