Class: Recommendations::User

Inherits:
Object
  • Object
show all
Includes:
Ratings
Defined in:
lib/recommendations/user.rb,
lib/recommendations/user/ratings.rb

Defined Under Namespace

Modules: Ratings

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ratings

#rate, #ratings

Constructor Details

#initialize(id) ⇒ User

Returns a new instance of User.



9
10
11
# File 'lib/recommendations/user.rb', line 9

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/recommendations/user.rb', line 7

def id
  @id
end

Instance Method Details

#recommendationsObject



13
14
15
16
# File 'lib/recommendations/user.rb', line 13

def recommendations
  Recommendations.redis.hgetall("recommendations:user:#{id}")
                       .select {|key, value| Float(value) > 0}
end

#update_recommendationsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/recommendations/user.rb', line 18

def update_recommendations
  ratings = self.ratings

  recommendations = ratings.inject({}) do |buffer, (item_id, score)|
    similars = Item.find(item_id).similars
    similars.reject! {|i| ratings.key?(i)}

    similars.each do |similar|
      buffer[similar] ||= 0
      buffer[similar] += (Float(score) - 3) # 0..5 score => -2..2
    end

    buffer
  end

  Recommendations.redis.hmset("recommendations:user:#{id}", *recommendations.flatten)
end