LoginSignup
4
4

More than 5 years have passed since last update.

Rubyで前処理。Pixivのタグ検索結果から作品ID別タグリストを作る

Last updated at Posted at 2014-04-16

概要

Pixivタグ検索APIの結果をDBに格納する事を考える。
第一正規化の為に、
Pixivタグ検索結果(A)から作品ID別タグリスト(B)を出力するRubyコードを書いた。

(A)
00000001,……,X Y Z
00000002,……,X
00000003,……,Y Z

(B)
00000001,X
00000001,Y
00000001,Z
00000002,X
00000003,Y
00000003,Z

データ

-> Pixiv検索結果をCSVダウンロードするHTMLファイルを用意した

コード

pxcsv2taglist.rb
# coding: UTF-8

require 'csv'

CSV.open("OUTPUT.csv", "wb") do |csv|
    CSV.foreach('INPUT.csv', encoding: "UTF-8") do |row|
        row[13].split(' ').each do |item|
            csv << [row[0], item]
        end
    end
end

puts 'end!'

出力イメージ

OUTPUT.csv
00000001,龍田
00000001,天龍
00000001,艦隊これくしょん
00000001,艦これ
00000001,百合

テーブル生成用SQL(SQLite)

CreateIllusttagTable.sql
CREATE TABLE illust_tag(illust_id TEXT, tag TEXT,PRIMARY KEY(illust_id, tag));

関連ページ

Pixivイラスト検索APIの取得結果のデータ構造を調べる

4
4
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
4
4