-1
    if @msg =~ /facebook/i
        puts "Nice job, you got it!"
    end

I want to make a trivia game that would somehow do not care about spelling count if a person typed in facebook as the answer to the game.

if someone accidentally typed in faceboko, fecabook, or ftckbook, it would work and give an output of "Nice job, you got it!"

I don't know how I would start it.

Charles
  • 71
  • 6

1 Answers1

4

Generally Levenshtein distance is used to handle such problems.

You can implement your own solution - but there is a good gem which uses native C bindings.

Define an error rate (e.g. 4) and check the distance between user input and original string.

2.2.2 :001 > require "levenshtein"
 => true 
2.2.2 :002 > Levenshtein.distance("facebook", "ftcebook")
 => 1 

So, if you use this, your code will become,

err = 4
if Levenshtein.distance("original", @msg)>err
  "Nice job"
end

This also may be useful - Edit distance such as Levenshtein taking into account proximity on keyboard

Community
  • 1
  • 1
marmeladze
  • 6,468
  • 3
  • 24
  • 45