Charles Charles - 8 months ago 19
Ruby Question

How to make spelling does not count in Ruby?

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

I want to make a trivia game that would somehow do not care about spelling count if a person typed in
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.


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"

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