summaryrefslogtreecommitdiff
path: root/lib/did_you_mean/spell_checker.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/did_you_mean/spell_checker.rb')
-rw-r--r--lib/did_you_mean/spell_checker.rb18
1 files changed, 9 insertions, 9 deletions
diff --git a/lib/did_you_mean/spell_checker.rb b/lib/did_you_mean/spell_checker.rb
index e5106abba2..37da2fc7a6 100644
--- a/lib/did_you_mean/spell_checker.rb
+++ b/lib/did_you_mean/spell_checker.rb
@@ -10,25 +10,25 @@ module DidYouMean
end
def correct(input)
- input = normalize(input)
- threshold = input.length > 3 ? 0.834 : 0.77
+ normalized_input = normalize(input)
+ threshold = normalized_input.length > 3 ? 0.834 : 0.77
- words = @dictionary.select { |word| JaroWinkler.distance(normalize(word), input) >= threshold }
- words.reject! { |word| input == word.to_s }
- words.sort_by! { |word| JaroWinkler.distance(word.to_s, input) }
+ words = @dictionary.select { |word| JaroWinkler.distance(normalize(word), normalized_input) >= threshold }
+ words.reject! { |word| input.to_s == word.to_s }
+ words.sort_by! { |word| JaroWinkler.distance(word.to_s, normalized_input) }
words.reverse!
# Correct mistypes
- threshold = (input.length * 0.25).ceil
- corrections = words.select { |c| Levenshtein.distance(normalize(c), input) <= threshold }
+ threshold = (normalized_input.length * 0.25).ceil
+ corrections = words.select { |c| Levenshtein.distance(normalize(c), normalized_input) <= threshold }
# Correct misspells
if corrections.empty?
corrections = words.select do |word|
word = normalize(word)
- length = input.length < word.length ? input.length : word.length
+ length = normalized_input.length < word.length ? normalized_input.length : word.length
- Levenshtein.distance(word, input) < length
+ Levenshtein.distance(word, normalized_input) < length
end.first(1)
end