class Octo::Recommender

Constants

TIME_HHMM

Time format to convert a Time into HHMM format

Public Class Methods

new() click to toggle source

Initializes the Recommender.

# File lib/octorecommender/recommenders.rb, line 16
def initialize
  @product_recommenders = {}
  @time_recommenders = {}

  # This option chosen as ruby seems to take a LOOOOOOOOOOT of time
  # in processing.
  Octo::Recommenders::ProductRecommender.processing_technique(:union)
  Octo::Recommenders::ProductRecommender.processing_technique :union

  Octo::Enterprise.each do |enterprise|
    @product_recommenders[enterprise.id] = Octo::Recommenders::ProductRecommender.new(enterprise.id)
    @time_recommenders[enterprise.id] = Octo::Recommenders::TimeRecommender.new(enterprise.id)
  end
end
perform() click to toggle source

Callback method for resque worker

# File lib/octorecommender/recommenders.rb, line 138
def self.perform
  self.new.process!
end

Public Instance Methods

convert_to_future(ts) click to toggle source

Converts a time from past to a similar time in future.

This is necessary as CF would return one of the dates
from past. We need sometime from future.
# File lib/octorecommender/recommenders.rb, line 133
def convert_to_future(ts)
  ts + (Time.now.beginning_of_day - ts.beginning_of_day) + 7.day
end
process!(opts = {}) click to toggle source

Creates a delayed job to process all the recommenders for all the

enterprises or can provide specific options as well
# File lib/octorecommender/recommenders.rb, line 121
def process!(opts = {})
  @product_recommenders.values.each do |recomm|
    recomm.process!
  end
  @time_recommenders.values.each do |recomm|
    recomm.process!
  end
end
register_product(product) click to toggle source

Register a Product for recommendation @param [Octo::Product] product The product object

# File lib/octorecommender/recommenders.rb, line 45
def register_product(product)
  eid = product.enterprise_id
  product.categories.each do |cat_text|
    @product_recommenders[eid].add_to_matrix(:categories,
        cat_text,
        product.id
    )
  end
  product.tags.each do |tag_text|
    @product_recommenders[eid].add_to_matrix(:tags,
        tag_text,
        product.id
    )
  end
end
register_user_action_time(user, ts = Time.now.floor) click to toggle source

Register a user, action-time view relation. @param [Octo::User] user The user object @param [Time] ts The time at which user takes some action

# File lib/octorecommender/recommenders.rb, line 64
def register_user_action_time(user, ts = Time.now.floor)
  eid = user.enterprise_id
  @time_recommenders[eid].add_to_matrix(:users,
      user.id,
      ts.to_i
  )

  @time_recommenders[eid].add_to_matrix(:hourminutes,
      ts.strftime(TIME_HHMM),
      ts.to_i
  )

  @time_recommenders[eid].add_to_matrix(:days,
      ts.strftime('%A'),
      ts.to_i
  )
end
register_user_product_view(user, product) click to toggle source

Register a user, product view relation. @param [Octo::User] user The user object @param [Octo::Product] product The product object

# File lib/octorecommender/recommenders.rb, line 34
def register_user_product_view(user, product)
  @product_recommenders[user.enterprise_id].add_to_matrix(:users,
      user.id,
      product.id
  )

  register_product product
end
similar_products(product, opts={}) click to toggle source

Get similar products for products @param [Octo::Product] product The product for which similarities

have to be found

@return [Array<Octo::Product>] An array containing similar

products
# File lib/octorecommender/recommenders.rb, line 103
def similar_products(product, opts={})
  eid = product.enterprise.id
  @product_recommenders[eid].similarities_for(product.id, opts)
end