Class: Predictor::InputMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/predictor/input_matrix.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ InputMatrix

Returns a new instance of InputMatrix.


3
4
5
# File 'lib/predictor/input_matrix.rb', line 3

def initialize(opts)
  @opts = opts
end

Instance Method Details

#add_set(set, items) ⇒ Object


50
51
52
# File 'lib/predictor/input_matrix.rb', line 50

def add_set(set, items)
  add_to_set(set, *items)
end

#add_single(set, item) ⇒ Object


54
55
56
# File 'lib/predictor/input_matrix.rb', line 54

def add_single(set, item)
  add_to_set(set, item)
end

#add_to_set(set, *items) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/predictor/input_matrix.rb', line 27

def add_to_set(set, *items)
  items = items.flatten if items.count == 1 && items[0].is_a?(Array)
  if items.any?
    Predictor.redis.multi do |redis|
      redis.sadd(parent_redis_key(:all_items), items)
      redis.sadd(redis_key(:items, set), items)

      items.each do |item|
        # add the set to the item's set--inverting the sets
        redis.sadd(redis_key(:sets, item), set)
      end
    end
  end
end

#baseObject


11
12
13
# File 'lib/predictor/input_matrix.rb', line 11

def base
  @opts[:base]
end

#calculate_jaccard(item1, item2) ⇒ Object


90
91
92
93
# File 'lib/predictor/input_matrix.rb', line 90

def calculate_jaccard(item1, item2)
  warn 'InputMatrix#calculate_jaccard is now deprecated. Use InputMatrix#score instead'
  Distance.jaccard_index(redis_key(:sets, item1), redis_key(:sets, item2), Predictor.redis)
end

#delete_item(item) ⇒ Object

delete item from the matrix


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/predictor/input_matrix.rb', line 73

def delete_item(item)
  Predictor.redis.watch(redis_key(:sets, item)) do
    sets = Predictor.redis.smembers(redis_key(:sets, item))
    Predictor.redis.multi do |multi|
      sets.each do |set|
        multi.srem(redis_key(:items, set), item)
      end

      multi.del redis_key(:sets, item)
    end
  end
end

#items_for(set) ⇒ Object


58
59
60
# File 'lib/predictor/input_matrix.rb', line 58

def items_for(set)
  Predictor.redis.smembers redis_key(:items, set)
end

#measure_nameObject


7
8
9
# File 'lib/predictor/input_matrix.rb', line 7

def measure_name
  @opts.fetch(:measure, :jaccard_index)
end

#parent_redis_key(*append) ⇒ Object


15
16
17
# File 'lib/predictor/input_matrix.rb', line 15

def parent_redis_key(*append)
  base.redis_key(*append)
end

#redis_key(*append) ⇒ Object


19
20
21
# File 'lib/predictor/input_matrix.rb', line 19

def redis_key(*append)
  base.redis_key(@opts.fetch(:key), *append)
end

66
67
68
69
70
# File 'lib/predictor/input_matrix.rb', line 66

def related_items(item)
  sets = Predictor.redis.smembers(redis_key(:sets, item))
  keys = sets.map { |set| redis_key(:items, set) }
  keys.length > 0 ? Predictor.redis.sunion(keys) - [item.to_s] : []
end

#remove_from_set(set, item) ⇒ Object

Delete a specific relationship


43
44
45
46
47
48
# File 'lib/predictor/input_matrix.rb', line 43

def remove_from_set(set, item)
  Predictor.redis.multi do |redis|
    redis.srem(redis_key(:items, set), item)
    redis.srem(redis_key(:sets, item), set)
  end
end

#score(item1, item2) ⇒ Object


86
87
88
# File 'lib/predictor/input_matrix.rb', line 86

def score(item1, item2)
  Distance.send(measure_name, redis_key(:sets, item1), redis_key(:sets, item2), Predictor.redis)
end

#sets_for(item) ⇒ Object


62
63
64
# File 'lib/predictor/input_matrix.rb', line 62

def sets_for(item)
  Predictor.redis.sunion redis_key(:sets, item)
end

#weightObject


23
24
25
# File 'lib/predictor/input_matrix.rb', line 23

def weight
  (@opts[:weight] || 1).to_f
end