Class: LSA
- Inherits:
-
Object
- Object
- LSA
- Defined in:
- lib/lsa.rb
Instance Attribute Summary collapse
-
#s ⇒ Object
Returns the value of attribute s.
-
#u ⇒ Object
Returns the value of attribute u.
-
#v ⇒ Object
Returns the value of attribute v.
Instance Method Summary collapse
-
#classify_vector(values) ⇒ Object
Return a distance (cosine similarity) between a new vector, and all the clusters (columns) used in the original matrix.
-
#cosine_similarity(x1, y1, x2, y2) ⇒ Object
Determines the cosine similarity between two 2D points.
-
#initialize(matrix) ⇒ LSA
constructor
A new instance of LSA.
- #inspect ⇒ Object
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
#s ⇒ Object
Returns the value of attribute s.
4 5 6 |
# File 'lib/lsa.rb', line 4 def s @s end |
#u ⇒ Object
Returns the value of attribute u.
4 5 6 |
# File 'lib/lsa.rb', line 4 def u @u end |
#v ⇒ Object
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,
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/lsa.rb', line 17 def classify_vector(values) raise "Unsupported vector length" unless values.size == @u.row_size || values.size == @v.row_size vector = Matrix.row_vector(values) mult_matrix = (values.size == @u.row_size ? @u : @v) comp_matrix = (values.size == @u.row_size ? @v : @u) position = vector * mult_matrix * @s.inverse x = position[0,0] y = position[0,1] results = [] comp_matrix.row_size.times do |index| results << [index, cosine_similarity(x, y, comp_matrix[index, 0], comp_matrix[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
36 37 38 39 40 41 42 |
# File 'lib/lsa.rb', line 36 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 |
#inspect ⇒ Object
10 11 12 |
# File 'lib/lsa.rb', line 10 def inspect "U:\n#{@u.inspect}\n\nS:\n#{@s.inspect}\n\nV:\n#{@v.inspect}" end |