Sha256: 837a58f8dbb977eba92087183d52644fa6bd4ff0775c909eb93cadcfbfc74ce7

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

module Recommendations
  class User
    module Similarity
      def similars(count = Recommendations.configuration.similar_items_count)
        Recommendations.redis.zrevrange("recommendations:similars:user:#{id}", 0, count - 1)
      end

      def update_similars
        Recommendations.redis.del("recommendations:similars:user:#{id}")

        similar_users = {}

        ratings.each do |item_id, rating|
          similars = Item.find(item_id).ratings(rating)

          similars.reject {|uid| uid == id}.each do |user_id|
            user_rating = Recommendations.redis.zscore("recommendations:ratings:item:#{item_id}", user_id)
            difference = (Float(user_rating) - Float(rating)).abs

            similar_users[user_id] ||= [0,0]
            similar_users[user_id][0] += difference
            similar_users[user_id][1] += 1
          end
        end

        similar_users.each do |user_id, (difference, common_items)|
          difference_factor = Float(Recommendations.configuration.user_tolerance) - Float(difference)/Float(common_items)
          common_items_factor = 0.2 * Float(common_items)
          similar_users[user_id] = difference_factor + common_items_factor
        end

        similar_users.each do |user_id, score|
          Recommendations.redis.zincrby("recommendations:similars:user:#{id}", score, user_id)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
recommendations2-0.0.5 lib/recommendations/user/similarity.rb
recommendations2-0.0.4 lib/recommendations/user/similarity.rb