cicindelaのめも
cicindelaインストールめも。
参考サイト
本家:http://code.google.com/p/cicindela2/wiki/Install
インストール
概要
- cicindela構成
WebAPI、レコメンデーション生成バッチ、MySQLで構成されています。
WebAPI・・・データの投入と、生成済みのレコメンドデータの取得を行います。
バッチ・・・レコメンドデータの生成を行います。daemontoolsで、起動の管理をします。
MySQL・・・投入データの保存、レコメンドデータの計算・保存を行います。
上記のような構成なので、以下のインストールが必要です。
また、バッチを実行するために以下の設定を行います。
- ログ設定
- cicindela設定(レコメンド設定例)
その他
- WebAPIの戻り値
プレーンテキストのデータとしておすすめするアイテムのIDを返します。データが準備されていなかったり、おすすめ情報が無いときは、空文字列が返ります。
データを簡単に扱えるメリットがありますが、ちゃんと動作しているのかわかりづらい感じがします。
- レコメンドバッチ
設定ファイルを編集することで、レコメンデーションデータを柔軟に設定できます。
ただ、daemontoolsのインストールがめんどい。cronに設定できれば手軽だったのでは・・・。
cicindela インストール
ここでは例として、/usr/local/cicindelaにインストールします。
チェックアウト
cd /usr/local/cicindela svn checkout http://cicindela2.googlecode.com/svn/trunk/ .
プログラムはワーキングディレクトリ上で直接動作します。そのため、「/home/cicindela」にチェックアウトした場合は、そのまま使用できます。
チェックアウトディレクトリが「/home/cicindela」でない場合は、以下のスクリプトを流す必要があります。
(UTF-8環境でないとスクリプトを正常に実行できないようです。)
cd misc # perlのパス、cicindelaのチェックアウトディレクトリを変更する perl substitute_project_paths.pl --perl_path=/usr/local/bin/perl --cicindela_home=/usr/local/cicindela
sedのエラーが出る場合は、以下の点を修正して実行してください。
(違いは、「-i "" 」→「-i"" 」)
substitute_project_paths.plの差分
< system (qq{sed -e 's!$default_perl_path!$opts{perl_path}!g' -i "" $file}) if ($opts{perl_path}); < system (qq{sed -e 's!$default_cicindela_home!$opts{cicindela_home}!g' -i "" $file}) if ($opts{cicindela_home}); --- > system (qq{sed -e 's!$default_perl_path!$opts{perl_path}!g' -i"" $file}) if ($opts{perl_path}); > system (qq{sed -e 's!$default_cicindela_home!$opts{cicindela_home}!g' -i"" $file}) if ($opts{cicindela_home});
mysql (>=5.0) インストール
http://dev.mysql.com/downloads/mysql/5.1.htmlを参考にmysqlをインストール
perl (>=5.8) モジュールのインストール
・DBI
・DBD::mysql
・Ima::DBI
・Time::Piece
・Log::Log4perl
・Module::Pluggable
・Class::Singleton
sudo perl -MCPAN -e "install DBI; install DBD::mysql; install Ima::DBI; install Time::Piece; install Log::Log4perl; install Module::Pluggable; install Class::Singleton;"
modperl インストール
http://perl.apache.org/download/index.htmlを参考にmodperlをインストール
以下の行を apache の設定ファイル httpd.conf の最後に追加します。
Include /usr/local/cicindela/etc/httpd/modperl.conf
deamontools インストール
↓を参考にdaemontoolsをインストール
http://www.emaillab.org/djb/tools/daemontools/install.html
http://cr.yp.to/daemontools/install.html
バッチ登録
# cicindela 分析用メインバッチを登録 # /usr/local/cicindela/etc/service/cicindela_batch sudo ln -s /usr/local/cicindela/etc/service/cicindela_batch /service/cicindela_batch # データ挿入用バッファのフラッシュを行うバッチを登録 # /usr/local/cicindela/etc/service/cicindela_flush_buffers sudo ln -s /usr/local/cicindela/etc/service/cicindela_flush_buffers /service/cicindela_flush_buffers # 起動状態を確認 svok /service/cicindela_batch; echo "$?" svstat /service/cicindela_batch
ログ設定
touch /usr/local/cicindela/var/logs/log.txt chmod a+rw /usr/local/cicindela/var/logs/log.txt
ローテート設定
vi /usr/local/cicindela/etc/log.conf
/usr/local/cicindela/var/logs/log.txt { daily create 0666 (user) (group) rotate 2 }
cicindelaを使う
概要
・DB、テーブル作成
・設定ファイル編集
・WebAPI データ投入/レコメンデーション取得
・その他
DB,テーブル作成
DB
cicindela_test01_db
DB user pass
test_user/test
cd /usr/local/cicindela/misc perl create_init_sql.pl --db_name=cicindela_clip_db | mysql -utest_user
設定ファイル編集
本家のhttp://code.google.com/p/cicindela2/wiki/FilterChain、http://code.google.com/p/cicindela2/wiki/Examplesを参考にするといいです。
シンプルな設定例
vi /usrlocal/cicindela/lib/Cicindela/Config/_common.pm
# 「$C{SETTINGS}」に追加 # 'test01' => { datasource => [ 'dbi:mysql:cicindela_test01_db;host=localhost', 'test_user', 'test' ], filters => [ 'PicksExtractor', 'InverseUserFrequency', 'ItemSimilarities', ], recommender => 'ItemSimilarities', refresh_interval => 60 * 60 * 1, },
ざっとまとめると。。。
設定キー | 意味 |
datasource | 接続DB設定。create_init_sql.plのdb_nameで指定したもの |
filters | 解析の心臓部にあたる「filter chain」設定 |
recommender | filterのxxxSimilaritiesの出力を元にレコメンドを計算する |
refresh_interval | 実行間隔 |
WebAPI
・データ投入
http://[base_url]/record?set=[セット名]&op=insert_pick&user_id=[ユーザid]&item_id=[アイテムid]
# user_id: 100、item_id: 1001のデータを投入 http://xxx.xxx.xxx.xxx/cicindela/record?set=test01&op=insert_pick&user_id=100&item_id=1001
・レコメンデーション取得
http://[base_url]/recommend?set=[セット名]&op=for_item&item_id=[アイテムid]
# 特定アイテムに対するレコメンデーション(関連アイテム)取得 http://xxx.xxx.xxx.xxx/cicindela/recommend?set=test01&op=for_item&item_id=1001 # 特定ユーザに対するレコメンデーション取得 http://xxx.xxx.xxx.xxx/cicindela/recommend?set=test01&op=for_user&user_id=100
その他
複数のセッティングを同居させる
・cicindela設定
vi /usrlocal/cicindela/lib/Cicindela/Config/_common.pm
# 「$C{SETTINGS}」に追加 # UserSimilarities # ユーザ類似度。batch.plを--track=1(calculation_trackの値)で起動する 'test01_user' => { datasource => [ 'dbi:mysql:cicindela_test01_db;host=localhost', 'test_user', 'test' ], filters => [ [ 'PicksExtractor', { interval => '20 year', in_table => 'picks', out_table => 'extracted_picks1', } ], [ 'InverseUserFrequency', { in_table => 'extracted_picks1', out_table => 'iuf1', } ], [ 'UserSimilarities', { in_table => 'extracted_picks1', in_table_iuf => 'iuf1', } ], ], recommender => 'UserSimilarities', calculation_track => 1, refresh_interval => 1, }, # ItemSimilarities # アイテム類似度。batch.plを--track=2(calculation_trackの値)で起動する 'test01_item' => { datasource => [ 'dbi:mysql:cicindela_test01_db;host=localhost', 'test_user', 'test' ], filters => [ [ 'PicksExtractor', { interval => '20 year', in_table => 'picks', out_table => 'extracted_picks2', } ], [ 'InverseUserFrequency', { in_table => 'extracted_picks2', out_table => 'iuf2', } ], [ 'ItemSimilarities', { in_table => 'extracted_picks2', in_table_iuf => 'iuf2', } ], ], recommender => 'ItemSimilarities', calculation_track => 2, refresh_interval => 1, }, # Hybrid # test01_userとtest01_itemのレコメンデーション結果をfactorの比率で合成して返す。 # factorの合計が1以上のとき、上位のfactor優先となる 'test01_hyb' => { datasource => [ 'dbi:mysql:cicindela_test01_db;host=localhost', 'test_user', 'test' ], recommender => [ 'Hybrid', { settings => [ { factor => 0.5, set_name => 'test01_user', }, { factor => 0.5, set_name => 'test01_item', }, ], } ], },
・daemontools登録
# calculation_trackに応じてbatch.plを--track=?で実行するバッチを登録する sudo ln -s /usr/local/cicindela/etc/service/cicindela_batch /service/cicindela_batch_track1 sudo ln -s /usr/local/cicindela/etc/service/cicindela_batch /service/cicindela_batch_track2 # これはそのまま sudo ln -s /usr/local/cicindela/etc/service/cicindela_flush_buffers /service/cicindela_flush_buffers
・データ投入
# user_id: 100、item_id: 1001のデータを投入 http://xxx.xxx.xxx.xxx/cicindela/record?set=test01_user&op=insert_pick&user_id=100&item_id=1001 http://xxx.xxx.xxx.xxx/cicindela/record?set=test01_item&op=insert_pick&user_id=100&item_id=1001
・レコメンデーション取得
# 特定ユーザに対するレコメンデーション取得 http://xxx.xxx.xxx.xxx/cicindela/recommend?set=test01_user&op=for_user&user_id=100 # 特定アイテムに対するレコメンデーション(関連アイテム)取得 http://xxx.xxx.xxx.xxx/cicindela/recommend?set=test01_item&op=for_item&item_id=1001 # Hybrid取得(for_item) http://xxx.xxx.xxx.xxx/cicindela/recommend?set=test01_hyb&op=for_item&item_id=1001