GETA-Ruby プラグイン公開
趣旨
GETA を Ruby を使って操作する GETA-Ruby ブリッジを作ってみた、というのが昨日までの話。そこで今度は Rails プラグインを作ってみた。Tar ball でインストールするよりはるかに簡単だ。Rails 使いの方は是非試してみてほしい。
前提条件
インストール方法
% cd $RAILS_ROOT % ruby script/plugin install http://softculture.com/rails/plugins/geta_ruby/
拡張ライブラリの C のプログラムは自動的にコンパイル・インストールされる。簡単でよいね。
サンプルコード
具体例を通じてプラグインを動かしてみよう。まずは、Rails プロジェクトを作ろう。
% cd $SOMEDIR % rails -d sqlite3 geta_test
% cd geta_test % ruby script/plugin install http://softculture.com/rails/plugins/geta_ruby/
テスト用のコードを格納するコントローラを作る。
% ruby script/generate controller site build_index calc_assoc
コントローラにサンプルコードを書き込む。
# RAILS_ROOT/app/controllers/site_controller.rb class SiteController < ApplicationController include Geta def build_index builder = WamBuilder.new("mytest") builder.add("X", "A", 2) builder.add("X", "B", 1) builder.add("Y", "B", 2) builder.add("Y", "C", 3) builder.build_index render :text => "The Wam has been built successfully." end def calc_assoc wam = Wam.new("mytest") query_elem_set = ["A", "B"] result_elem_set = wam.assoc(query_elem_set, WAM_COL) render :text => "The column elements #{query_elem_set.join(", ")} are associated with column elements #{result_elem_set.join(", ")}." wam.close end end
$GETAROOT/etc/ci.conf には "mytest" というハンドルを追加して、dataroot も適当に設定しておくこと。GETAROOT 環境変数を設定して、Rails アプリを起動。
% export GETAROOT=/usr/local/geta % ruby script/server
正常な起動を確認したら、Rails アプリが localhost のポート 3000 番で動いているものと仮定して http://localhost:3000/site/build_index にブラウザ上からアクセスしてみよう。
The Wam has been built successfully.
と画面に表示されただろうか?これで WAM インデックスは生成された。次に http://localhost:3000/site/calc_assoc にアクセス。
The column elements A, B are associated with column elements B, A, C.
と画面に表示されたら、連想検索に成功だ。