lib/pearson.rb
def recommendations(scores, entity, opts={})
      totals = {}
      similarity_sums = {}

      totals.default = 0
      similarity_sums.default = 0

      fail EntityNotFound unless scores[entity]

      scores.each do |other_entity|
        next if other_entity.first == entity

        similarity = coefficient(scores, entity, other_entity.first)

        next if similarity <= 0

        scores[other_entity.first].each do |item, score|
          if !scores[entity].keys.include?(item) || scores[entity][item] == 0
            totals[item] += score * similarity
            similarity_sums[item] += similarity
          end
        end
      end

      sort_desc(totals, opts) {|h, k, v| h.merge(k => v/similarity_sums[k]) }
    end
# Returns the best recommended items for a given entity
#
# @param [Hash] Hash containing entity-item scores
# @param [String] Entity
# @param [Hash] Options (limit)
#
# @return [Array] Top matches [item, score]
Returns the best recommended items for a given entity
Returns the best recommended items for a given entity
def recommendations(scores, entity, opts={})
      totals = {}
      similarity_sums = {}
      totals.default = 0
      similarity_sums.default = 0
      fail EntityNotFound unless scores[entity]
      scores.each do |other_entity|
        next if other_entity.first == entity
        similarity = coefficient(scores, entity, other_entity.first)
        next if similarity <= 0
        scores[other_entity.first].each do |item, score|
          if !scores[entity].keys.include?(item) || scores[entity][item] == 0
            totals[item] += score * similarity
            similarity_sums[item] += similarity
          end
        end
      end
      sort_desc(totals, opts) {|h, k, v| h.merge(k => v/similarity_sums[k]) }
    end
def recommendations ( scores , entity , opts = { } ) totals = { } similarity_sums = { } totals . default = 0 similarity_sums . default = 0 fail EntityNotFound unless scores [ entity ] scores . each do | other_entity | next if other_entity . first == entity similarity = coefficient ( scores , entity , other_entity . first ) next if similarity <= 0 scores [ other_entity . first ] . each do | item , score | if ! scores [ entity ] . keys . include? ( item ) || scores [ entity ] [ item ] == 0 totals [ item ] += score * similarity similarity_sums [ item ] += similarity end end end sort_desc ( totals , opts ) { | h , k , v | h . merge ( k => v / similarity_sums [ k ] ) } end
Returns the best recommended items for a given entity
Returns the best recommended items for a given entity
Top matches [item, score]
Top matches [ item score ]
Array
scores
entity
opts
None