Module: DidYouMean::BaseFinder

Included in:
ClassFinder, MethodFinder, NameFinder
Defined in:
lib/did_you_mean/finders.rb

Constant Summary collapse

AT =
"@".freeze
EMPTY =
"".freeze

Instance Method Summary collapse

Instance Method Details

#searchesObject

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/did_you_mean/finders.rb', line 36

def searches
  raise NotImplementedError
end

#suggestionsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/did_you_mean/finders.rb', line 9

def suggestions
  @suggestions ||= searches.flat_map do |input, candidates|
    input     = normalize(input)
    threshold = input.length > 3 ? 0.834 : 0.77

    seed = candidates.select {|candidate| JaroWinkler.distance(normalize(candidate), input) >= threshold }
      .sort_by! {|candidate| JaroWinkler.distance(candidate.to_s, input) }
      .reverse!

    # Correct mistypes
    threshold   = (input.length * 0.25).ceil
    corrections = seed.select {|c| Levenshtein.distance(normalize(c), input) <= threshold }

    # Correct misspells
    if corrections.empty?
      corrections = seed.select do |candidate|
        candidate = normalize(candidate)
        length    = input.length < candidate.length ? input.length : candidate.length

        Levenshtein.distance(candidate, input) < length
      end.first(1)
    end

    corrections
  end
end