60
66

More than 5 years have passed since last update.

オープンソースのレコメンドエンジン「recommendify」をrailsで使う。

Last updated at Posted at 2013-11-11

recommendifyとは

オープンソースのレコメンドエンジンです。
アイテム間の結びつきをスコア付で返してくれます。

redisのインストール(Admin権限で)

yum install redis
chkconfig redis on
/etc/init.d/redis start

hiredisのインストール(Admin権限で)

yum install hiredis hiredis-devel

Gemfileに記述し、インストール

Gemfile
gem 'redis'
gem 'recommendify'
bundle install

nativeでbuildする。(10倍早いらしい)

# pathは状況による
cd ~/.rvm/gems/ruby-2.0.0-p247/gems/recommendify-0.3.8/
bundle install
bundle exec rake build_native

railsで初期化に組み込む。

config/initializers/recommendify_init.rb
# configure redis
Recommendify.redis = Redis.new(:host => "localhost", :port => 6379)

class PageviewRecommender < Recommendify::Base

  max_neighbors 50

  input_matrix :order_items,  
    :native => true,
    :similarity_func => :jaccard,    
    :weight => 5.0 
end

レコメンドデータを投入する

lib/tasks/recommendify_indexing.rake
task :recommendify_indexing => :environment do |x, args|
  recommender = PageviewRecommender.new

  # 2つ以上を同時に投入しないと意味がない。
  recommender.order_items.add_set("order1", ["product23", "product65", "productm23"])
  recommender.order_items.add_set("order2", ["product14", "product23"])

  # 確定
  recommender.process!

end

rakeを実行

bundle exec rake  recommendify_indexing

recommendifyからデータを取り出す

ruby
recommender = PageviewRecommender.new

# product23と関連付いたitemを取得
p recommender.for('product23')

redis-cliでデータを確認

redis-cli -h localhost
hgetall recommendify:similarities

各種操作

redisからレコメンドデータをリセットする

ruby
Recommendify.redis.del "recommendify:order_items:ccmatrix"
Recommendify.redis.del "recommendify:similarities"
Recommendify.redis.del "recommendify:order_items:items"

アイテムを指定して削除

ruby
recommender.delete_item!("product23")
60
66
1

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
60
66