Class: FuzzyTools::Index

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

Direct Known Subclasses

TfIdfIndex

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Index

Returns a new instance of Index.



8
9
10
11
12
# File 'lib/fuzzy_tools/index.rb', line 8

def initialize(options = {})
  @source            = options[:source]
  @indexed_attribute = options[:attribute] || :to_s
  build_index
end

Instance Attribute Details

#indexed_attributeObject (readonly)

Returns the value of attribute indexed_attribute.



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

def indexed_attribute
  @indexed_attribute
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#all(query) ⇒ Object



19
20
21
# File 'lib/fuzzy_tools/index.rb', line 19

def all(query)
  all_with_scores(query).map(&:first)
end

#all_with_scores(query) ⇒ Object



23
24
25
# File 'lib/fuzzy_tools/index.rb', line 23

def all_with_scores(query)
  unsorted_scored_results(query).sort_by { |doc, score| [-score, document_attribute(doc)] }
end

#find(query) ⇒ Object



14
15
16
17
# File 'lib/fuzzy_tools/index.rb', line 14

def find(query)
  result, score = unsorted_scored_results(query).max_by { |doc, score| [score, document_attribute(doc)] }
  result
end