Module: Spellr::Suggester

Defined in:
lib/spellr/suggester.rb

Class Method Summary collapse

Class Method Details

.fast_suggestions(token, limit = 5) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/spellr/suggester.rb', line 33

def fast_suggestions(token, limit = 5)
  if slow?
    []
  else
    suggestions(token, limit)
  end
end

.slow?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/spellr/suggester.rb', line 27

def slow?
  return @slow if defined?(@slow)

  @slow = ::JaroWinkler.method(:similarity).source_location
end

.suggestions(token, limit = 5) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spellr/suggester.rb', line 12

def suggestions(token, limit = 5) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  wordlists = token.location.file.wordlists
  term = token.spellr_normalize
  jaro_winkler_similarity_threshold = term.length > 4 ? 0.834 : 0.77
  suggestions = wordlists.flat_map do |wordlist|
    all_suggestions(term, jaro_winkler_similarity_threshold, wordlist)
  end
  suggestions.uniq!(&:word)
  suggestions.sort_by! { |suggestion| [-suggestion.jaro_winkler_similarity, suggestion.word] }

  suggestions = reduce_suggestions(suggestions, term, limit)

  suggestions.map { |suggestion| suggestion.word.send(token.case_method) }
end