Module: Suggestor::Algorithms::RecommendationAlgorithm
- Included in:
- EuclideanDistance, PearsonCorrelation
- Defined in:
- lib/suggestor/algorithms/recommendation_algorithm.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
Instance Method Summary collapse
- #initialize(collection) ⇒ Object
-
#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.
-
#recommended_to(main, opts = {}) ⇒ Object
Ex.
-
#similar_related_to(main, opts = {}) ⇒ Object
Ex.
-
#similar_to(main, opts = {}) ⇒ Object
Ex.
Instance Attribute Details
#collection ⇒ Object
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!() suggestions = Hash.new(0.0) set.each do |item| = (item, opts) .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 |
#recommended_to(main, opts = {}) ⇒ Object
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!() @similarities = @totals = Hash.new(0) create_similarities_totals(main) results = generate_rankings sort_results(results,opts[:size]) end |
#similar_related_to(main, opts = {}) ⇒ Object
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 (main, opts={}) opts.merge!() 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!() cleaned = @collection.remove(main) results = order_by_similarity_score(main, cleaned) sort_results(results,opts[:size]) end |