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!
threshold = (input.length * 0.25).ceil
corrections = seed.select {|c| Levenshtein.distance(normalize(c), input) <= threshold }
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
|