Personalized recommendations in Ruby

Pavel Kordík
May 15, 2016 · 1 min read

For those of you, who develop in Ruby, we prepared a simple client application enabling you to benefit from our personalized recommendations.

Ruby is getting increasingly popular programming language.

Here is how you can integrate in into your website or application:

require 'recombee_api_client'
include RecombeeApiClient

# Prepare some items and users
NUM = 100
my_users = (1..NUM).map { |i| "user-#{i}" }
my_items = (1..NUM).map { |i| "item-#{i}" }

#Generate some random purchases of items by users
PROBABILITY_PURCHASED = 0.1
my_purchases = []
my_users.each do |user|
p = my_items.select { |_| rand(0.0..1.0) < PROBABILITY_PURCHASED }
p.each { |item| my_purchases.push('userId' => user, 'itemId' => item) }
end

# Use Recombee recommender
client = RecombeeClient.new('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L')
begin
# Send the data to Recombee, use Batch for faster processing
puts 'Send users'
client.send(Batch.new(my_users.map { |userId| AddUser.new(userId) }))
puts 'Send items'
client.send(Batch.new(my_items.map { |itemId| AddItem.new(itemId) }))
puts 'Send purchases'
client.send(Batch.new(my_purchases.map { |p| AddPurchase.new(p['userId'], p['itemId'], 0) }))
# Get recommendations for user 'user-25'
puts 'Recommend for a user'
recommended = client.send(UserBasedRecommendation.new('user-25', 5, 'rotationRate' => 0))
puts "Recommended items: #{recommended}"
rescue ResponseError => e
puts e
end

Get access token by requesting free trial option at our website and evaluate quality of our service now.