Class: RSemantic::Search

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(documents, options = {}) ⇒ Search

Returns a new instance of Search.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rsemantic/search.rb', line 5

def initialize(documents, options = {})
  options = {
    :transforms => [:TFIDF, :LSA],
    :verbose    => false,
    :filter_stop_words => true,
    :stem_words => true,
  }.merge(options)
  RSemantic.logger.level = options[:verbose] ? Logger::INFO : Logger::ERROR


  @builder = VectorSpace::Builder.new(
    :filter_stop_words => options[:filter_stop_words],
    :stem_words => options[:stem_words],
    :locale => options[:locale]
  )
  @matrix_transformer = MatrixTransformer.new(options[:transforms])

  @vector_space_model = @builder.build_document_matrix(documents)

  RSemantic.logger.info(@vector_space_model)

  @vector_space_model = @matrix_transformer.apply_transforms(@vector_space_model)
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



3
4
5
# File 'lib/rsemantic/search.rb', line 3

def builder
  @builder
end

Instance Method Details



29
30
31
32
33
34
35
# File 'lib/rsemantic/search.rb', line 29

def related(document_id)
  ratings = []
  @vector_space_model.each_column do |column|
    ratings << Compare.similarity(@vector_space_model.column(document_id), column)
  end
  ratings
end

#search(search_list) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rsemantic/search.rb', line 37

def search(search_list)
  ratings = []
  query_vector = @builder.build_query_vector(search_list)
  @vector_space_model.each_column do |column|
    ratings << Compare.similarity(query_vector.col, column)
  end
  ratings
end