module Recommendify::CCMatrix

Public Instance Methods

add_set(set_id, item_ids) click to toggle source
# File lib/recommendify/cc_matrix.rb, line 10
def add_set(set_id, item_ids)
  # FIXPAUL: forbid | and : in item_ids
  item_ids.each do |item_id|
    item_count_incr(item_id)
  end
  all_pairs(item_ids).map do |pair| 
    i1, i2 = pair.split(":") 
    ccmatrix.incr(i1, i2)
  end
end
add_single(set_id, item_id, other_item_ids) click to toggle source
# File lib/recommendify/cc_matrix.rb, line 21
def add_single(set_id, item_id, other_item_ids)
  item_count_incr(item_id)
  other_item_ids.each do |other_item|
    ccmatrix.incr(item_id, other_item)
  end
end
all_items() click to toggle source
# File lib/recommendify/cc_matrix.rb, line 28
def all_items
  Recommendify.redis.hkeys(redis_key(:items))
end
ccmatrix() click to toggle source
# File lib/recommendify/cc_matrix.rb, line 3
def ccmatrix
  @ccmatrix ||= Recommendify::SparseMatrix.new(
    :redis_prefix => @opts.fetch(:redis_prefix),
    :key => [@opts.fetch(:key), :ccmatrix].join(":")
  )
end
delete_item(item_id) click to toggle source
# File lib/recommendify/cc_matrix.rb, line 32
def delete_item(item_id)    
  Recommendify.redis.hdel(redis_key(:items), item_id)
  ccmatrix.send(:k_delall, item_id)
end

Private Instance Methods

all_pairs(keys) click to toggle source
# File lib/recommendify/cc_matrix.rb, line 39
def all_pairs(keys)
  keys.map{ |k1| (keys-[k1]).map{ |k2| [k1,k2].sort.join(":") } }.flatten.uniq
end
item_count(key) click to toggle source
# File lib/recommendify/cc_matrix.rb, line 47
def item_count(key)
  Recommendify.redis.hget(redis_key(:items), key).to_i
end
item_count_incr(key) click to toggle source
# File lib/recommendify/cc_matrix.rb, line 43
def item_count_incr(key)
  Recommendify.redis.hincrby(redis_key(:items), key, 1)
end