Class: LSA

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matrix) ⇒ LSA

Returns a new instance of LSA.



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

def initialize(matrix)
  @u, @s, @v = matrix.decompose(2)
end

Instance Attribute Details

#sObject

Returns the value of attribute s.



4
5
6
# File 'lib/lsa.rb', line 4

def s
  @s
end

#uObject

Returns the value of attribute u.



4
5
6
# File 'lib/lsa.rb', line 4

def u
  @u
end

#vObject

Returns the value of attribute v.



4
5
6
# File 'lib/lsa.rb', line 4

def v
  @v
end

Instance Method Details

#classify_vector(values) ⇒ Object

Return a distance (cosine similarity) between a new vector, and all the clusters (columns) used in the original matrix. Returns a sorted list of indexes and distances,



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lsa.rb', line 13

def classify_vector(values)
  raise "Unsupported vector length" unless values.size == @u.row_size
  vector = Matrix.row_vector(values)
  position = vector * @u * @s.inverse
  puts position
  x = position[0,0]
  y = position[0,1]
  results = []
  
  @v.row_size.times do |index|
    results << [index, cosine_similarity(x, y, @v[index, 0], @v[index, 1])]
  end
  
  results.sort {|a, b| b[1] <=> a[1]}
end

#cosine_similarity(x1, y1, x2, y2) ⇒ Object

Determines the cosine similarity between two 2D points



30
31
32
33
34
35
36
# File 'lib/lsa.rb', line 30

def cosine_similarity(x1, y1, x2, y2)
  dp = (x1 * x2) + (y1 * y2)
  mag1 = Math.sqrt((x1 ** 2) + (y1 ** 2))
  mag2 = Math.sqrt((x2 ** 2) + (y2 ** 2))
  return 0 if mag1 == 0 || mag2 == 0
  return (dp / (mag1 * mag2))
end