Sha256: eaab5317423c3e920b2637fb42231da0326e8468c6683b94c796b6b3ff57c5f6

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

require File.dirname(__FILE__) + '/../../spec_helper'

module Semantic
  describe Transform::LSA do

    tiny_matrix = Linalg::DMatrix.columns([[0.0, 1.0, 0.0],
                                           [1.0, 0.0, 1.0]])

    u = Linalg::DMatrix.rows([[1,0],
                              [0,1]])

    vt = Linalg::DMatrix.rows([[1,0,0],
                               [1,0,0],
                               [1,0,0]])

    sigma = Linalg::DMatrix.rows([[1,0,0],
                                  [0,1,0]])

    describe "latent semantic analysis transform" do

      it "should use svd on matrix" do
        matrix = Linalg::DMatrix.columns([[0.0, 1.0, 0.0],
                                          [1.0, 0.0, 1.0]])

        matrix.should_receive(:singular_value_decomposition).and_return([u, sigma, vt])

        Linalg::DMatrix.stub!(:columns).and_return(matrix)

        Transform::LSA.transform(matrix)
      end

      it "should reduce the noise in the sigma matrix" do
        matrix = Linalg::DMatrix.columns([[0.0, 1.0, 0.0],
                                          [1.0, 0.0, 1.0]])

        matrix.stub!(:singular_value_decomposition).and_return([u, sigma, vt])
        Linalg::DMatrix.stub!(:columns).and_return(matrix)

        sigma.should_receive(:[]=).with(0,0,0)
        sigma.should_receive(:[]=).with(1,1,0)

        Transform::LSA.transform(matrix, 2)
      end

      it "should prevent reducing dimensions greater than the matrixes own dimensions" do
        lambda { Transform::LSA.transform tiny_matrix, 100 }.should raise_error(Exception)
      end

      it "should transform LSA matrix" do
        transformed_matrix = Transform::LSA.transform tiny_matrix

        #TODO: better way to compare result matrix
        transformed_matrix.to_s.should == Linalg::DMatrix.columns([[0,0,0],[1,0,1]]).to_s
      end

    end

  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
josephwilk-rsemantic-0.1.0 spec/semantic/transform/lsa_transform_spec.rb
josephwilk-rsemantic-0.1.1 spec/semantic/transform/lsa_transform_spec.rb
josephwilk-rsemantic-0.1.2 spec/semantic/transform/lsa_transform_spec.rb
josephwilk-rsemantic-0.1.3 spec/semantic/transform/lsa_transform_spec.rb
josephwilk-semantic-0.1.0 spec/semantic/transform/lsa_transform_spec.rb
rsemantic-0.1.3 spec/semantic/transform/lsa_transform_spec.rb