Module: Suggestor::Algorithms::RecommendationAlgorithm

Included in:
EuclideanDistance, PearsonCorrelation
Defined in:
lib/suggestor/algorithms/recommendation_algorithm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



5
6
7
# File 'lib/suggestor/algorithms/recommendation_algorithm.rb', line 5

def collection
  @collection
end

Instance Method Details

#initialize(collection) ⇒ Object



7
8
9
# File 'lib/suggestor/algorithms/recommendation_algorithm.rb', line 7

def initialize(collection)
  @collection = Records.new(collection)
end

#items_for_set(set, opts = {}) ⇒ Object

Takes a set and returns suggestions based on the weighted average of the similarities to all of the items in it.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/suggestor/algorithms/recommendation_algorithm.rb', line 47

def items_for_set(set, opts={})
  opts.merge!(default_options)

  suggestions = Hash.new(0.0)

  set.each do |item|
    related = similar_related_to(item, opts)
    related.each { |rel| suggestions[rel[0]] += rel[1] unless
      set.include? rel[0] }
  end

  suggestions = suggestions.map { |label, score| [label, score / set.length] }
  suggestions.sort_by { |sug| sug[1] }.reverse
end

Ex. a user will get movie recommendations



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/suggestor/algorithms/recommendation_algorithm.rb', line 23

def recommended_to(main, opts={})
  opts.merge!(default_options)

  @similarities = @totals = Hash.new(0)

  create_similarities_totals(main)

  results = generate_rankings

  sort_results(results,opts[:size])
end

Ex. what other movies are related to a given one



36
37
38
39
40
41
42
43
# File 'lib/suggestor/algorithms/recommendation_algorithm.rb', line 36

def similar_related_to(main, opts={})
  opts.merge!(default_options)

  inverted_collection = @collection.invert
  suggestor     = self.class.new(inverted_collection)
  
  suggestor.similar_to(main,opts)
end

#similar_to(main, opts = {}) ⇒ Object

Ex. Similar users based on their movies reviews



12
13
14
15
16
17
18
19
20
# File 'lib/suggestor/algorithms/recommendation_algorithm.rb', line 12

def similar_to(main, opts={})
  opts.merge!(default_options)
  
  cleaned = @collection.remove(main)

  results = order_by_similarity_score(main, cleaned)

  sort_results(results,opts[:size])
end