Class: FastFuzzy::Percolator

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_fuzzy/percolator.rb

Constant Summary collapse

N =

>= 3 is necessary to wheight consecutiveness of words (ex “this car” => thi, his, is_, s_c, _ca, car)

4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Percolator

Returns a new instance of Percolator.



10
11
12
13
14
15
16
17
18
# File 'lib/fast_fuzzy/percolator.rb', line 10

def initialize(options = {})
  @queries = options.delete(:queries) || []
  @analyzer_chain = options.delete(:analyzer_chain) || Analyzer::STANDARD_CHAIN

  @index = Hash.new{|h, k| h[k] = []}
  @query_terms_count = []

  build_index unless queries.empty?
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/fast_fuzzy/percolator.rb', line 7

def index
  @index
end

#queriesObject (readonly)

Returns the value of attribute queries.



7
8
9
# File 'lib/fast_fuzzy/percolator.rb', line 7

def queries
  @queries
end

Instance Method Details

#<<(query) ⇒ Object



20
21
22
23
# File 'lib/fast_fuzzy/percolator.rb', line 20

def <<(query)
  @queries << query
  build_index
end

#percolate(str) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/fast_fuzzy/percolator.rb', line 25

def percolate(str)
  analyser = Analyzer.new(str, @analyzer_chain)

  terms_phrase = analyser.select{|term| term[1] == Lucene::ALPHANUM_TYPE}.map{|term| term[0]}.join(' ')
  terms_phrase = " #{terms_phrase} "
  grams = Ngram.generate(terms_phrase, N).uniq
  score(lookup(grams))
end