I think you describe the Levenshtein distance. And yes, there are gems for this. If you hit pure Ruby, go to the text stone.
$ gem install text
The docs have more details, but here's the gist:
Text::Levenshtein.distance('test', 'test')
If you are ok with the native extensions ...
$ gem install levenshtein
This usage is similar . Its performance is very good. (It processes ~ 1000 spelling corrections per minute on my systems.)
If you need to know how two words are similar, use the distance along the length of the word.
If you want a simple similarity test, consider something like this:
Unconfirmed, but directly:
String.module_eval do def similar?(other, threshold=2) distance = Text::Levenshtein.distance(self, other) distance <= threshold end end
source share