[R] 利用Graph API 製作FB朋友文字雲
一.首先要先在Facebook申請 AccessToken
網址如下:
Facebook開發人員網址
(1)進入網址後點Tools&Support => 進入 Graph API Explorer
(2)之後點選 Get AccessToken
(3)勾選這些權限允許
二.確定R的版本與各項packages安裝
(1)我這邊R的版本是R version 3.0.2 (2013-09-25)
(2)有些package沒辦法自動下載完,需自行編譯
(2)有些package沒辦法自動下載完,需自行編譯
install.packages("rJava") install.packages("RCurl") install.packages("rjson") install.packages("tm", repos="http://R-Forge.R-project.org", type = "source") install.packages("tmcn", repos="http://R-Forge.R-project.org", type = "source") install.packages("Rwordseg", repos = "http://R-Forge.R-project.org", type = "source") install.packages("RColorBrewer") install.packages("wordcloud") install.packages("rJava")
三.執行程式碼
(1)程式碼部分如下:require(RCurl) require(rjson) setwd('/home/lock/') #設定工作環境位置1 if(!file.exists('/home/lock/somedir')){dir.create('somedir')} wdname ="/home/lock/somedir" #設定工作環境位置2 setwd(wdname) token = "CAACEdEose0cBAHHZBR0qbBrZC21IVWIz3pheMDTv7YhrJHI4GpzreZCjWwltHxlZBKob4g91i8N48ovVDDYnftLqw4jWQ1YPOeXjZARi2GiSF9iwBZA1CCJRy4zktdUS1fQ2BHZA8s1jCZBhLrsHvD2m7ryC1Kpz8sJZAOX10tSfZAjcOqjKNwZApjTiE7pAvof1JHyxfFofsi3Wzk40NUbLGNsjPtjaIjt6mIZD" ############### #Function start ############### facebook = function( path = "me?fields=posts.limit(200)", access_token = token, options){ if( !missing(options) ){ options = sprintf( "?%s", paste( names(options), "=", unlist(options), collapse = "&", sep = "" ) ) } else { options = "" } data = getURL( sprintf( "https://graph.facebook.com/%s%s&access_token=%s", path, options, access_token ) , .opts = list(ssl.verifypeer = FALSE),encoding='UTF-8') fromJSON( data ) } ################################################################### facebook2 = function(urlcode){ data =getURL (urlcode,.opts = list(ssl.verifypeer = FALSE)) fromJSON( data ) } ################################################################### is.defined = function(x)!is.null(x) ################################################################### fb_like_next = function(tmp_like2, urlcode){ posts2=facebook2( urlcode ) for(i in 1 : length(posts2$data)){ tmp_like2 = c(tmp_like2,posts2$data[i]$name) } if(is.defined(posts2$paging$`next`)){ tmp_like2 = c( fb_like_next(tmp_like2,posts2$paging$`next`),posts2$data[i]$name) } return(tmp_like2) } ################################################################### ############### #Function end ############### posts = facebook() tmp_like = list() for(i in 1:length(posts[1]$posts$data)){ if(is.defined(posts[1]$posts$data[[i]]$likes)){ for(j in 1:length(posts[1]$posts$data[[i]]$likes$data)){ ddname =c(posts[1]$posts$data[[i]]$likes$data[[j]]$name) tmp_like = c( tmp_like,ddname) if(is.defined(posts[1]$posts$data[[i]]$likes$paging$`next`)){ dda = fb_like_next(tmp_like,posts[1]$posts$data[[i]]$likes$paging$`next`) } } } } tmp_like = c(tmp_like,dda) lapply(tmp_like, write, "fb_friends.txt", append=TRUE, ncolumns=1000) library(tm) library(tmcn) library(Rwordseg) d.corpus = Corpus(DirSource(wdname), list(language=NA)) d.corpus = tm_map(d.corpus, stripWhitespace) d.corpus = tm_map(d.corpus, removePunctuation) d.corpus = tm_map(d.corpus, removeWords, stopwordsCN()) d.corpus = tm_map(d.corpus, PlainTextDocument) d.corpus = tm_map(d.corpus,function(x){gsub(" ", "", x)}) #把空白去 tdm = TermDocumentMatrix(d.corpus, control = list(wordLengths = c(2,Inf))) library(RColorBrewer) library(wordcloud) m1 = as.matrix(tdm) v = sort(rowSums(m1), decreasing = TRUE) d = data.frame(word = names(v), freq = v) pal2 = brewer.pal(8,"Dark2") png("wordcloud_fb_like.png", width=60,height=30, units='in', res=300) wordcloud(d$word[1:50], d$freq, scale=c(15,.2),min.freq=2, max.words=Inf, random.order=FALSE, rot.per=.15, colors=pal2) dev.off()
留言
張貼留言