Module: Knitsearch::FuzzyCorrector

Extended by:
FuzzyCorrector
Included in:
FuzzyCorrector
Defined in:
lib/knitsearch/fuzzy_corrector.rb

Constant Summary collapse

PREFIX_LENGTH =
3

Instance Method Summary collapse

Instance Method Details

#correct(query, vocab_table:, connection:, threshold:, skip_last: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/knitsearch/fuzzy_corrector.rb', line 9

def correct(query, vocab_table:, connection:, threshold:, skip_last: false)
  return query if query.nil?

  str = query.to_s
  return query if str.strip.empty?
  return query unless vocab_table_available?(connection, vocab_table)

  tokens = str.split(/\s+/)
  last_index = tokens.length - 1

  corrected = tokens.each_with_index.map do |token, i|
    if skip_last && i == last_index
      token
    else
      correct_token(token, vocab_table: vocab_table, connection: connection, threshold: threshold)
    end
  end

  corrected.join(" ")
end