Class: Spellr::Suggester

Inherits:
Object
  • Object
show all
Defined in:
lib/spellr/suggester.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wordlist) ⇒ Suggester

Returns a new instance of Suggester.



48
49
50
51
# File 'lib/spellr/suggester.rb', line 48

def initialize(wordlist)
  @did_you_mean = ::DidYouMean::SpellChecker.new(dictionary: wordlist.to_a)
  @suggestions = {}
end

Class Method Details

.fast_suggestions(token) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/spellr/suggester.rb', line 28

def fast_suggestions(token)
  if slow?
    []
  else
    suggestions(token)
  end
end

.slow?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/spellr/suggester.rb', line 22

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

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

.suggestions(token) ⇒ Object



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

def suggestions(token)
  wordlists = token.location.file.wordlists
  term = token.spellr_normalize.chomp
  words = wordlists.flat_map { |wordlist| wordlist.suggestions(token) }.uniq
  words = ::DidYouMean::SpellChecker.new(dictionary: words).correct(term)
  words = reduce_suggestions(words, term)

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

Instance Method Details

#suggestions(term) ⇒ Object



53
54
55
56
57
58
# File 'lib/spellr/suggester.rb', line 53

def suggestions(term)
  term = term.spellr_normalize
  @suggestions.fetch(term) do
    @suggestions[term] = @did_you_mean.correct(term).map(&:chomp)
  end
end