Wikipedia のデータをデータベースに格納する Ruby スクリプト
書いた人: noriaki 2007,07月22日(日) 14:23
はてなダイアリーで,拙作のスクリプトを利用してくださった方のコメントを遅ればせながら発見しました.
RailsのActiveRecordを利用している方がいたので動かしてみる。
- document.rbの先頭に以下を追加
- み込むXMLファイルのパスを変更
+ require 'rubygems' + require_gem 'activerecord' + require 'rexml/streamlistener' + + ActiveRecord::Base.establish_connection( + :adapter => 'postgresql' + :host => 'localhost' + :username => 'postgres' + :password => 'hogehoge' + :database => 'wikipedia_ja' + )とりあえずスクリプトは動き出したようだ。データベースのテーブル作成って勝手やってくれるのかな?
bluedemioの日記: wikipediaのデータを全文検索用データとして利用
おそらく,Rails用に書いたスクリプトをRubyスクリプトとして実行しようとされたのでエラーがいっぱい出てしまったのではないかと思います.
そこで,単独で動作するWikipediaデータをRDBに格納するRubyスクリプト(useing ActiveRecord)を書きました.
wikipedia-import.rb
Wikipedia:データベースダウンロードページからダウンロードしたXMLファイルを対象に,XMLファイルを読み込んで,ページごとのデータをRDBに格納するRubyスクリプトです.
想定環境
- LinuxまたはMac(Windowsでは動かないかも:FileTestあたりで)
- ruby 1.8.5以上
- rubygems 0.9.0以上
- ActiveRecordとHpricotがインストールされている
- RDBはMySQL,PostgreSQLなどActiveRecordが扱うことのできるものならなんでも(多分
使い方
ダウンロード
wikipedia-import.rb をダウンロードします.
$ wget http://blog.fulltext-search.biz/files/wikipedia-import.rbデータベースの作成とテーブル定義
データを格納するデータベースの作成と,テーブル定義を行います.
$ createdb -U postgres -T template0 wikipedia_ja必要なテーブル定義は以下のとおりです(PostgreSQLの場合を例として挙げます)
wikipedia_ja=# \d documents
Table "public.documents"
Column | Type | Modifiers
--------------+-----------------------------+--------------------------------------------------------
id | integer | not null default nextval('documents_id_seq'::regclass)
url | character varying(1024) |
title | character varying(255) |
author | character varying(255) |
plain_text | text |
wiki_text | text |
created_time | timestamp without time zone |
updated_on | timestamp without time zone |
entity_id | integer |
Indexes:
"documents_pkey" PRIMARY KEY, btree (id)
"documents_entity_id_index" btree (entity_id)テーブルを作成するためのSQLの例も挙げておきます(PostgreSQL).
CREATE TABLE documents (
"id" serial primary key,
"url" character varying(1024) DEFAULT NULL,
"title" character varying(255) DEFAULT NULL,
"author" character varying(255) DEFAULT NULL,
"plain_text" text DEFAULT NULL,
"wiki_text" text DEFAULT NULL,
"created_time" timestamp DEFAULT NULL,
"updated_on" timestamp DEFAULT NULL,
"entity_id" integer DEFAULT NULL
);
CREATE INDEX "index_documents_on_entity_id" ON documents ("entity_id");
wikipedia-import.rb 内の設定修正
上記のデータベースに接続するための設定を wikipedia-import.rbの最初の部分を修正することによって行います.
#################################################
## Rewite the following to your configurations
#################################################
ActiveRecord::Base.establish_connection(
:adapter => 'postgresql', # mysql, postgresql, sqlite ...
:host => 'localhost',
:username => 'USERNAME',
:password => 'PASSWORD',
:database => 'wikipedia_ja'
):adapterや:username,:passwordなどの項目をご利用の環境に合わせて適切に修正してください.
実行
引数に読み込むXMLファイルのパスを与えて実行します.
$ ruby wikipedia-import.rb data/jawiki-latest-pages-articles.xmljawiki-latest-pages-articles.xml ファイルが wikipedia-import.rb と同じディレクトリにある場合は引数を省略することができます.
免責
このスクリプトは無保証です.いかなる(以下略
Let's enjoy Ruby life!


あぁ,スパム処理と一緒に誤ってコメントを消してしまいました・・