Skip to content

Instantly share code, notes, and snippets.

@tetlabo
Created March 5, 2023 09:13
Show Gist options
  • Save tetlabo/cce98eddcb26d482e50dd6bd686251fc to your computer and use it in GitHub Desktop.
Save tetlabo/cce98eddcb26d482e50dd6bd686251fc to your computer and use it in GitHub Desktop.
Twitterのフォロワーのプロフィールを取得するプログラム
### 自分のフォロワーのプロフィール情報を取得してワードクラウドを作る
library(twitteR)
library(RMeCab)
#library(wordcloud)
library(wordcloud2)
library(Cairo)
library(extrafont)
loadfonts(quiet = TRUE)
setup_twitter_oauth(consumer_key = "fAlW58d11EJ0UnSiWxUQydtSN",
consumer_secret = "hI0tbR3SkJGf3dDA3scH6FcU0qN2Dt1wAmvTo8JliAuq8Av7Vr",
access_token = "2471210569-VZRDUabAqh5zTDUrvG4OojIxaTeONwtMzYT6hGl",
access_secret = "Kp2NnYqQJeTnupBU5PqwyHBC4QGKU49qOt5iAKdSSIuYX")
me <- getUser("data_sciesotist")
follower_list <- me$getFollowerIDs()
who_all <- NULL
for (i in 1:length(follower_list)){
print(paste("now processing", i))
status <- try(who <- lookupUsers(follower_list[i]))
if(inherits(status, "try-error")){
next
}
who_all <- c(who_all, who)
Sys.sleep(10)
}
profile_text <- NULL
for (i in 1:length(follower_list)){
tmp <- eval(parse(text=paste0("who_all$`",follower_list[i],"`$description")))
profile_text <- c(profile_text, tmp)
}
#profile_text<-iconv(profile_text,from="UTF-8",to="CP932")
write.table(profile_text, "my_follower_profile.txt", quote = FALSE, row.names = FALSE)
# いろいろ前処理をする
wc<-RMeCabFreq("my_follower_profile.txt")
part_wc<-wc[(wc$Info1=="名詞"|wc$Info1=="感動詞"|wc$Info1=="形容詞"|wc$Info1=="動詞")&(!wc$Info2=="数"&!wc$Info2=="接尾"&!wc$Info2=="非自立"&!wc$Info2=="アルファベット"&!wc$Info2=="ナイ形容詞語幹")&(!wc$Term=="RT"&!wc$Term=="co"&!wc$Term=="http"&!wc$Term=="HTTP"&!wc$Term=="https"&!wc$Term=="HTTPS"&!wc$Term=="http://"&!wc$Term=="/"&!wc$Term=="t.co"&!wc$Term==":"&!wc$Term=="..."&!wc$Term=="_"&!wc$Term=="_:"&!wc$Term=="rzzp"&!wc$Term=="."&!wc$Term=="・"&!wc$Term=="://"&!wc$Term=="www"&!wc$Term=="ww"&!wc$Term=="bot"&!wc$Term=="BOT"&!wc$Term=="null"&!wc$Term=="tweet"&!wc$Term=="Twitter"&!wc$Term=="ReTweet"&!wc$Term=="する"&!wc$Term=="フォロー"&!wc$Term=="フォロワー"&!wc$Term=="フォロ"&!wc$Term=="フォロバ"&!wc$Term=="相互フォロー"&!wc$Term=="リプ"&!wc$Term=="リプライ"&!wc$Term=="人"&!wc$Term=="一"&!wc$Term=="―"&!wc$Term=="一一"&!wc$Term=="一一一"&!wc$Term=="一一"&!wc$Term=="ーー"&!wc$Term=="ーーー"&!wc$Term=="笙"&!wc$Term=="縺"&!wc$Term=="塵"&!wc$Term=="繝"&!wc$Term=="繧"&!wc$Term=="なる"&!wc$Term=="ある"&!wc$Term=="いる"&!wc$Term=="やる"&!wc$Term=="できる"&!wc$Term=="繋がる"&!wc$Term=="来る"&!wc$Term=="出る"&!wc$Term=="あと"&!wc$Term=="無料"&!wc$Term=="相互"&!wc$Term=="これ"&!wc$Term=="それ"&!wc$Term=="こちら"&!wc$Term=="なん"&!wc$Term=="ここ"&!wc$Term=="なに"&!wc$Term=="やつ"&!wc$Term=="そこ"&!wc$Term=="どこ"&!wc$Term=="いつ"&!wc$Term=="だれ"&!wc$Term=="気"&!wc$Term=="ありがとうございます"&!wc$Term=="よろしくお願いします"&!wc$Term=="見る"&!wc$Term=="思う"&!wc$Term=="言う"&!wc$Term=="いう"&!wc$Term=="ない"&!wc$Term=="私"&!wc$Term=="わたし"&!wc$Term=="俺"&!wc$Term=="おれ"&!wc$Term=="僕"&!wc$Term=="ぼく"&!wc$Term=="ω"&!wc$Term=="無い"&!wc$Term=="良い"&!wc$Term=="いい"&!wc$Term=="よい"&!wc$Term=="ちる"&!wc$Term=="WWW"&!wc$Term=="笑"&!wc$Term=="ありがとう"&!wc$Term=="行く"&!wc$Term=="いく"&!wc$Term=="とこ"&!wc$Term=="いる"&!wc$Term=="おる"&!wc$Term=="みんな"&!wc$Term=="定期"&!wc$Term=="今"&!wc$Term=="何"&!wc$Term=="前"&!wc$Term=="後"&!wc$Term=="後ろ"&!wc$Term=="ふる"&!wc$Term=="くる"&!wc$Term=="わかる"&!wc$Term=="ああ"&!wc$Term=="そー"&!wc$Term=="うん"&!wc$Term=="あれ"&!wc$Term=="今日"&!wc$Term=="仕手"),]
part_wc<-part_wc[(!part_wc$Term=="a"&!part_wc$Term=="b"&!part_wc$Term=="c"&!part_wc$Term=="d"&!part_wc$Term=="e"&!part_wc$Term=="f"&!part_wc$Term=="g"&!part_wc$Term=="h"&!part_wc$Term=="i"&!part_wc$Term=="j"&!part_wc$Term=="k"&!part_wc$Term=="l"&!part_wc$Term=="m"&!part_wc$Term=="n"&!part_wc$Term=="o"&!part_wc$Term=="p"&!part_wc$Term=="q"&!part_wc$Term=="r"&!part_wc$Term=="s"&!part_wc$Term=="t"&!part_wc$Term=="u"&!part_wc$Term=="v"&!part_wc$Term=="w"&!part_wc$Term=="x"&!part_wc$Term=="y"&!part_wc$Term=="z"&!part_wc$Term=="A"&!part_wc$Term=="B"&!part_wc$Term=="C"&!part_wc$Term=="D"&!part_wc$Term=="E"&!part_wc$Term=="F"&!part_wc$Term=="G"&!part_wc$Term=="H"&!part_wc$Term=="I"&!part_wc$Term=="J"&!part_wc$Term=="K"&!part_wc$Term=="L"&!part_wc$Term=="M"&!part_wc$Term=="N"&!part_wc$Term=="O"&!part_wc$Term=="P"&!part_wc$Term=="Q"&!part_wc$Term=="R"&!part_wc$Term=="S"&!part_wc$Term=="T"&!part_wc$Term=="U"&!part_wc$Term=="V"&!part_wc$Term=="W"&!part_wc$Term=="X"&!part_wc$Term=="Y"&!part_wc$Term=="Z"),]
part_wc<-part_wc[(!part_wc$Term=="あ"&!part_wc$Term=="い"&!part_wc$Term=="う"&!part_wc$Term=="え"&!part_wc$Term=="お"&!part_wc$Term=="か"&!part_wc$Term=="き"&!part_wc$Term=="く"&!part_wc$Term=="け"&!part_wc$Term=="こ"&!part_wc$Term=="さ"&!part_wc$Term=="し"&!part_wc$Term=="す"&!part_wc$Term=="せ"&!part_wc$Term=="そ"&!part_wc$Term=="た"&!part_wc$Term=="ち"&!part_wc$Term=="つ"&!part_wc$Term=="て"&!part_wc$Term=="と"&!part_wc$Term=="な"&!part_wc$Term=="に"&!part_wc$Term=="ぬ"&!part_wc$Term=="ね"&!part_wc$Term=="の"&!part_wc$Term=="は"&!part_wc$Term=="ひ"&!part_wc$Term=="ふ"&!part_wc$Term=="へ"&!part_wc$Term=="ほ"&!part_wc$Term=="ま"&!part_wc$Term=="み"&!part_wc$Term=="む"&!part_wc$Term=="め"&!part_wc$Term=="も"&!part_wc$Term=="や"&!part_wc$Term=="ゆ"&!part_wc$Term=="よ"&!part_wc$Term=="わ"&!part_wc$Term=="を"&!part_wc$Term=="ん"&!part_wc$Term=="ぁ"&!part_wc$Term=="ぃ"&!part_wc$Term=="ぅ"&!part_wc$Term=="ぇ"&!part_wc$Term=="ぉ"&!part_wc$Term=="ゃ"&!part_wc$Term=="ゅ"&!part_wc$Term=="ょ"&!part_wc$Term=="ア"&!part_wc$Term=="イ"&!part_wc$Term=="ウ"&!part_wc$Term=="エ"&!part_wc$Term=="オ"&!part_wc$Term=="カ"&!part_wc$Term=="キ"&!part_wc$Term=="ク"&!part_wc$Term=="ケ"&!part_wc$Term=="コ"&!part_wc$Term=="サ"&!part_wc$Term=="シ"&!part_wc$Term=="ス"&!part_wc$Term=="セ"&!part_wc$Term=="ソ"&!part_wc$Term=="タ"&!part_wc$Term=="チ"&!part_wc$Term=="ツ"&!part_wc$Term=="テ"&!part_wc$Term=="ト"&!part_wc$Term=="ナ"&!part_wc$Term=="ニ"&!part_wc$Term=="ヌ"&!part_wc$Term=="ネ"&!part_wc$Term=="ノ"&!part_wc$Term=="ハ"&!part_wc$Term=="ヒ"&!part_wc$Term=="フ"&!part_wc$Term=="ヘ"&!part_wc$Term=="ホ"&!part_wc$Term=="マ"&!part_wc$Term=="ミ"&!part_wc$Term=="ム"&!part_wc$Term=="メ"&!part_wc$Term=="モ"&!part_wc$Term=="ヤ"&!part_wc$Term=="ユ"&!part_wc$Term=="ヨ"&!part_wc$Term=="ワ"&!part_wc$Term=="ヲ"&!part_wc$Term=="ン"&!part_wc$Term=="ァ"&!part_wc$Term=="ィ"&!part_wc$Term=="ゥ"&!part_wc$Term=="ェ"&!part_wc$Term=="ォ"&!part_wc$Term=="ャ"&!part_wc$Term=="ュ"&!part_wc$Term=="ョ"&!part_wc$Term=="ヴ"&!part_wc$Term=="ー"&!part_wc$Term=="Д"),]
part_wc<-head(part_wc[order(part_wc$Freq,decreasing=T),],n=100)
part_wc <- part_wc[, c("Term", "Freq")]
#set1<-brewer.pal(9,"Set1")
#dark2<-brewer.pal(8,"Dark2")
#mycolor<-rev(mycolor<-c(dark2[4],"#00008B",dark2[1],set1[4],set1[7],dark2[5],dark2[2],dark2[8]))
#mycolor<-rev(mycolor<-c(dark2[4],"#00008B","#008B00",set1[4],set1[7],dark2[5],dark2[2],dark2[8]))
#CairoPNG(file="twitter_text_mining.png",width=1280,height=1280)
#png("twitter_text_mining.png",type = "cairo", width=1280,height=1280)
#par(family="M+2P+IPAG")
#wordcloud(part_wc$Term,part_wc$Freq,scale=c(8,4),max.words=Inf,random.order=T,random.color=F,colors=mycolor)
#dev.off()
wordcloud2(part_wc, size = 1, minSize = 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment