Module: Co2Filter::ContentBased

Defined in:
lib/co2_filter/content_based.rb

Defined Under Namespace

Classes: Results, UserProfile

Class Method Summary collapse

Class Method Details

.boost_ratings(users: nil, items: nil) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
# File 'lib/co2_filter/content_based.rb', line 56

def self.boost_ratings(users: nil, items: nil)
  raise ArgumentError.new("A 'users' argument must be provided.") unless users
  raise ArgumentError.new("An 'items' argument must be provided.") unless items

  users.inject({}) do |content_boosted_users, (user_id, ratings)|
    content_boosted_users[user_id] = ratings.merge(filter(user: ratings, items: items))
    content_boosted_users
  end
end

.filter(user: nil, items: nil) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/co2_filter/content_based.rb', line 5

def self.filter(user: nil, items: nil)
  raise ArgumentError.new("A 'user' argument must be provided.") unless user
  raise ArgumentError.new("An 'items' argument must be provided.") unless items

  if(user.is_a?(UserProfile))
     = user
    new_items = items
  elsif(user.is_a?(Hash) || user.is_a?(Co2Filter::RatingSet))
    user = Co2Filter::RatingSet.new(user) if user.is_a?(Hash)
     = ratings_to_profile(user_ratings: user, items: items)
    new_items = items.reject{|item_id, v| user[item_id]}
  end
  results = new_items.inject({}) do |hash, (item_id, item)|
    strength_normalizer = 0
    hash[item_id] = 0
    item.each do |attr_id, strength|
      hash[item_id] += [attr_id].to_f * strength
      strength_normalizer += strength.abs if [attr_id]
    end
    hash[item_id] /= strength_normalizer if strength_normalizer != 0
    hash[item_id] += .mean
    hash
  end
  Results.new(results)
end

.ratings_to_profile(user_ratings: nil, items: nil) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/co2_filter/content_based.rb', line 31

def self.ratings_to_profile(user_ratings: nil, items: nil)
  raise ArgumentError.new("A 'user_ratings' argument must be provided.") unless user_ratings
  raise ArgumentError.new("An 'items' argument must be provided.") unless items

  user_ratings = Co2Filter::RatingSet.new(user_ratings) unless user_ratings.is_a? Co2Filter::RatingSet
  user_prefs = {}
  strength_normalizers = {}
  user_ratings.each do |item_id, score|
    deviation = score - user_ratings.mean

    items[item_id].each do |attr_id, strength|
      user_prefs[attr_id] ||= 0
      user_prefs[attr_id] += strength * deviation
      strength_normalizers[attr_id] ||= 0
      strength_normalizers[attr_id] += strength.abs
    end
  end

  user_prefs.each do |attr_id, score|
    user_prefs[attr_id] /= strength_normalizers[attr_id].to_f
  end

  UserProfile.new(user_prefs, user_ratings.mean)
end