LoginSignup
6
3

More than 5 years have passed since last update.

RでPixivAPIを叩き、艦これの百合関連タグを探す

Last updated at Posted at 2014-03-18

概要

Rの練習がてら書いた糞コードを晒す。
「百合タグと共起するタグをPixivAPI経由で艦これ人気作品内から探す」コードを書いた。
 ※LinuxMint13(Ubuntu12.04)のR3.0.3で実行確認した。

コード

端末
sudo apt-get install libcurl4-openssl-dev
PxvTagColle.R
#install.packages("RCurl")
# ←失敗。最新版ライブラリを直接ダウンロードし読み込んだら成功。
library("RCurl")

my.keyword = "艦隊これくしょん 百合"
my.hit.mincount = 7

my.keyword.base = paste(my.keyword," 0Users入り", sep="")
my.keyword.hit = my.keyword

my.keyword.hit.c <- unlist(strsplit(my.keyword.hit," "))

escapeURI <- function(uri){
  paste(sapply(unlist(strsplit(enc2utf8(uri), "")),
               function(chr){ifelse((regexpr("\\W", chr, perl=TRUE)[1] > 0),
                                    paste("%", charToRaw(chr), collapse="", sep=""), chr)}), collapse="")
}

pixiv.search.bytag <- function(keyword){
  data.df <- NULL
  keyword.esc <- escapeURI(keyword)
  for(i in 1:4){
    uri <- paste(paste(paste("http://spapi.pixiv.net/iphone/search.php?s_mode=s_tag&word=", keyword.esc, sep=""),"&PHPSESSID=0&p=", sep=""), i, sep="")
    Sys.sleep(1)
    data.text <- getURI(uri)
    if(0 < nchar(data.text)){
      data.csv <- gsub("\\\"","\"",data.text)
      tmp <- tempfile()
      cat(file = tmp,data.csv)
      if(typeof(data.df) == "NULL"){
        data.df <- read.csv(tmp,header=FALSE,stringsAsFactors=FALSE)
      }else{
        data.df <- rbind(data.df,read.csv(tmp,header=FALSE,stringsAsFactors=FALSE))
      }
    }
  }
  data.df
}

pixiv.tag.2c <- function(value){
  strsplit(as.character(value)," ")
}

pixiv.search.usedtag <- function(keyword){
  data.raw.list <- pixiv.search.bytag(keyword)
  if(class(data.raw.list) == "NULL"){
    NULL
  }else{
    sapply(data.raw.list[,14],pixiv.tag.2c)
  }
}

basetag.raw.list <- pixiv.search.usedtag(my.keyword.base)

basetag.c <- c()
for(x in basetag.raw.list){
  basetag.c <- append(basetag.c,x)
}
basetag.c.factor <- as.factor(basetag.c)
basetag.count <- tapply(basetag.c.factor, basetag.c.factor, length)
basetag.c.target <- names(basetag.count[basetag.count > my.hit.mincount])

basetag.c.rate <- rep(0,length(basetag.c.target))
names(basetag.c.rate) <- basetag.c.target
for(x in basetag.c.target){
  tag.list <- pixiv.search.usedtag(x)
  if(class(tag.list) == "NULL"){
    #処理なし
  }else{
    tag.list.count <- length(tag.list)
    targettag.count <- 0
    for(y in tag.list){
      if(all(sapply(my.keyword.hit.c,function(z){any(y == z)}))){
        targettag.count <- targettag.count + 1
      }
    }
    basetag.c.rate[x] <- (targettag.count/tag.list.count)
  }
}

basetag.c.rate[order(basetag.c.rate, decreasing = TRUE)]

結果

                  愛高                   赤賀               かげぬい                   鈴熊 
             0.5890411              0.4250000              0.1789474              0.1700000 
                  大北                 長陸奥                 天龍田 加賀(艦隊これくしょん) 
             0.1300000              0.0850000              0.0750000              0.0600000 
   艦これ1000users入り                   山城                   赤城                   大井 
             0.0400000              0.0300000              0.0300000              0.0300000 
    艦これ500users入り                   北上                   陸奥                   加賀 
             0.0250000              0.0250000              0.0250000              0.0150000 
    艦これ100users入り                   長門                   龍田                   天龍 
             0.0150000              0.0150000              0.0150000              0.0100000 
        艦これかわいい                 艦これ       艦隊これくしょん                   百合 
             0.0050000              0.0000000              0.0000000              0.0000000 
                  漫画 
             0.0000000 
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3