Skip to content

Commit

Permalink
Change the spell checker algorithm
Browse files Browse the repository at this point in the history
Before: [:read, :rand, ...]
                ^�(J~~~~�(B only select this

After:  [:read, :rand, ...]
         ^�(J~~~~~~~~~~~�(B select :rand and also take elements before :rand

closes #60
  • Loading branch information
yuki24 committed Dec 22, 2015
1 parent ab55825 commit e2f5b24
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/did_you_mean/spell_checkable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ def corrections

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

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

Expand Down
1 change: 1 addition & 0 deletions test/spell_checker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_spell_checker_corrects_mistypes

assert_spell %w(gsub! gsub), input: 'gsuv!', dictionary: %w(sub gsub gsub!)
assert_spell %w(sub! sub gsub!), input: 'ssub!', dictionary: %w(sub sub! gsub gsub!)
assert_spell %i(read rand), input: 'raed', dictionary: File.methods + File.private_methods

group_methods = %w(groups group_url groups_url group_path)
assert_spell 'groups', input: 'group', dictionary: group_methods
Expand Down

0 comments on commit e2f5b24

Please sign in to comment.