Module: Predictor::Distance

Extended by:
Distance
Included in:
Distance
Defined in:
lib/predictor/distance.rb

Instance Method Summary collapse

Instance Method Details

#jaccard_index(key_1, key_2, redis = Predictor.redis) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/predictor/distance.rb', line 5

def jaccard_index(key_1, key_2, redis = Predictor.redis)
  x, y = nil

  redis.multi do |multi|
    x = multi.sinterstore 'temp', [key_1, key_2]
    y = multi.sunionstore 'temp', [key_1, key_2]
    multi.del 'temp'
  end

  y.value > 0 ? (x.value.to_f/y.value.to_f) : 0.0
end

#sorensen_coefficient(key_1, key_2, redis = Predictor.redis) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/predictor/distance.rb', line 17

def sorensen_coefficient(key_1, key_2, redis = Predictor.redis)
  x, y, z = nil

  redis.multi do |multi|
    x = multi.sinterstore 'temp', [key_1, key_2]
    y = multi.scard key_1
    z = multi.scard key_2
    multi.del 'temp'
  end

  denom = (y.value + z.value)
  denom > 0 ? (2 * (x.value) / denom.to_f) : 0.0
end