Skip to content

Loading…

Added Suggestor#items_for_set #1

Merged
merged 1 commit into from

2 participants

@ptn

Given a set of items, recommend similar items to add to the set.

@ptn ptn Added Suggestor#items_for_set
Given a set of items, recommend similar items to add to the set.
ce3730f
@xenda xenda merged commit cbbb39b into xenda:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 10, 2012
  1. @ptn

    Added Suggestor#items_for_set

    ptn committed
    Given a set of items, recommend similar items to add to the set.
Showing with 52 additions and 2 deletions.
  1. +15 −0 README.md
  2. +9 −0 examples/playing_around.rb
  3. +4 −0 lib/suggestor.rb
  4. +18 −1 lib/suggestor/algorithms/recommendation_algorithm.rb
  5. +6 −1 test/suggestor_test.rb
View
15 README.md
@@ -81,4 +81,19 @@ want to check which other movies are similar to it:
suggestor.similar_related_to("Batman Begins ", :size => 5)
+### Suggested items for a set
+
+Say that you have the list of the movies that you liked the most and you'd like to know what you should watch next. Suggestor has you covered:
+
+ suggestor.items_for_set ['Batman Begins', 'Cyrus']
+ # => [["Kung Fu Hustle", 0.5], ["Super 8 ", 0.5], ["Celda 211 ", 0.5], ... ]
+
+Suggestor of course considers the whole set when recommending new items, so different sets will yield different results, even if they differ in only one item. Say you picked Super 8 from the previous query:
+ suggestor.items_for_set ['Batman Begins', 'Cyrus', 'Super 8 ']
+ # => [["Rapidos y Furiosos 5", 0.3333333333333333], ["Agente Salt", 0.3333333333333333], ["Mary and Max ", 0.3333333333333333], ... ]
+
+But if you had picked Kung Fu Hustle:
+ suggestor.items_for_set ['Batman Begins', 'Cyrus', 'Kung Fu Hustle']
+ # => [["El mensajero ", 0.6666666666666666], ["El Club de la Pelea", 0.3333333333333333], ["Un tonto en el amor", 0.3333333333333333], ... ]
+
Now you can go and build your awesome recommendations web site :)
View
9 examples/playing_around.rb
@@ -43,3 +43,12 @@
ops = {size: 10}
results = suggestor.similar_related_to(name, opts)
puts results.inspect
+
+puts
+puts
+
+set = ['Batman Begins', 'Cyrus']
+puts "Great! One more thing: if I've watched all these movies: #{set.inspect},
+which should I watch next?"
+results = suggestor.items_for_set set
+puts results.inspect
View
4 lib/suggestor.rb
@@ -26,6 +26,10 @@ def similar_related_to(item, opts={})
@algorithm.similar_related_to(item, opts)
end
+ def items_for_set(set, opts={})
+ @algorithm.items_for_set(set, opts)
+ end
+
private
def parse_from_json(json)
View
19 lib/suggestor/algorithms/recommendation_algorithm.rb
@@ -42,6 +42,23 @@ def similar_related_to(main, opts={})
suggestor.similar_to(main,opts)
end
+ # Takes a set and returns suggestions based on the weighted average of
+ # the similarities to all of the items in it.
+ 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
+
private
def default_options
@@ -104,4 +121,4 @@ def create_similarities_totals(main)
end
end
-end
+end
View
7 test/suggestor_test.rb
@@ -39,5 +39,10 @@
@suggestor.similar_related_to("2").must_be :==, expected
end
+ it "must return items that might follow a sequence" do
+ expected = [["5", 0.29166666666666663], ["3", 0.29166666666666663], ["4", 0.16666666666666666]]
+ @suggestor.items_for_set(['1', '2']).must_be :==, expected
+ end
+
end
- end
+ end
Something went wrong with that request. Please try again.