Class: SameSame::CosineDistance

Inherits:
Object
  • Object
show all
Defined in:
lib/same_same/cosine_distance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCosineDistance

Returns a new instance of CosineDistance.



8
9
10
# File 'lib/same_same/cosine_distance.rb', line 8

def initialize
  self.cosin = CosineSimilarity.new
end

Instance Attribute Details

#cosinObject

Returns the value of attribute cosin.



6
7
8
# File 'lib/same_same/cosine_distance.rb', line 6

def cosin
  @cosin
end

Instance Method Details

#distance(x, y) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/same_same/cosine_distance.rb', line 12

def distance(x, y)
  sim = cosin.sim(x, y)

  if sim < 0.0
    throw new ArgumentError(
            "Can't use this value to calculate distance." +
            "x[]=" + x.inspect + 
            ", y[]=" + y.inspect +
            ", cosin.sim(x,y)=" + sim)
  end

  1.0 - sim
end