Class: VSS::Engine

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

Instance Method Summary collapse

Constructor Details

#initialize(records, documentizer = proc { |document| document }) ⇒ Engine

‘documentizer` just takes a record and converts it to a string



7
8
9
10
11
# File 'lib/vss/engine.rb', line 7

def initialize(records, documentizer = proc { |document| document })
  @records = records    
  @documents = records.map { |record| documentizer.call(record) }
  @vocab = tokenize(@documents.join(" "))
end

Instance Method Details

#search(query) ⇒ Object



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

def search(query)
  # get ranks
  query_vector = make_query_vector(query)
  ranks = @documents.map do |document|
    document_vector = make_vector(document)
    cosine_rank(query_vector, document_vector)
  end

  # now annotate records and return them
  @records.each_with_index do |record, i|
    # TODO: do this in a sensible way...
    record.instance_eval %{def rank; #{ranks[i]}; end}
  end

  # exclude 0 rank (no match) and sort by rank
  @records.reject { |r| r.rank == 0 }.sort { |a,b| b.rank <=> a.rank }
end