Class: Recommendify::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/recommendify/base.rb

Constant Summary collapse

@@max_neighbors =
nil
@@input_matrices =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/recommendify/base.rb', line 21

def initialize    
  @input_matrices = Hash[self.class.input_matrices.map{ |key, opts| 
    opts.merge!(:key => key, :redis_prefix => redis_prefix)
    [ key, Recommendify::InputMatrix.create(opts) ]
  }]
  @similarity_matrix = Recommendify::SimilarityMatrix.new(
    :max_neighbors => max_neighbors,
    :key => :similarities,
    :redis_prefix => redis_prefix
  )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/recommendify/base.rb', line 41

def method_missing(method, *args)
  if @input_matrices.has_key?(method)
    @input_matrices[method]
  else
    raise NoMethodError.new(method.to_s)
  end
end

Instance Attribute Details

#input_matricesObject (readonly)

Returns the value of attribute input_matrices.



3
4
5
# File 'lib/recommendify/base.rb', line 3

def input_matrices
  @input_matrices
end

#similarity_matrixObject (readonly)

Returns the value of attribute similarity_matrix.



3
4
5
# File 'lib/recommendify/base.rb', line 3

def similarity_matrix
  @similarity_matrix
end

Class Method Details

.input_matricesObject



17
18
19
# File 'lib/recommendify/base.rb', line 17

def self.input_matrices
  @@input_matrices
end

.input_matrix(key, opts) ⇒ Object



13
14
15
# File 'lib/recommendify/base.rb', line 13

def self.input_matrix(key, opts)
  @@input_matrices[key] = opts
end

.max_neighbors(n = nil) ⇒ Object



8
9
10
11
# File 'lib/recommendify/base.rb', line 8

def self.max_neighbors(n=nil)    
  return @@max_neighbors unless n
  @@max_neighbors = n
end

Instance Method Details

#all_itemsObject



53
54
55
# File 'lib/recommendify/base.rb', line 53

def all_items
  @input_matrices.map{ |k,m| m.all_items }.flatten.uniq
end

#delete_item!(item_id) ⇒ Object



80
81
82
83
84
# File 'lib/recommendify/base.rb', line 80

def delete_item!(item_id)
  input_matrices.map do |k,m|
    m.delete_item(item_id)
  end
end

#for(item_id) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/recommendify/base.rb', line 57

def for(item_id)
  similarity_matrix[item_id].map do |item_id, similarity|
    Recommendify::Neighbor.new(
      :item_id => item_id, 
      :similarity => similarity
    )
  end.sort
end

#max_neighborsObject



37
38
39
# File 'lib/recommendify/base.rb', line 37

def max_neighbors
  self.class.max_neighbors || Recommendify::DEFAULT_MAX_NEIGHBORS
end

#process!Object



66
67
68
# File 'lib/recommendify/base.rb', line 66

def process!
  all_items.each{ |item_id,n| process_item!(item_id) }
end

#process_item!(item_id) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/recommendify/base.rb', line 70

def process_item!(item_id)
  input_matrices.map do |k,m|
    neighbors = m.similarities_for(item_id).map do |i,w|        
      [i,w*m.weight]
    end
    similarity_matrix.update(item_id, neighbors)
  end
  similarity_matrix.commit_item!(item_id)
end

#redis_prefixObject



33
34
35
# File 'lib/recommendify/base.rb', line 33

def redis_prefix
  "recommendify"
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/recommendify/base.rb', line 49

def respond_to?(method)
  @input_matrices.has_key?(method) ? true : super
end