2.36. Calculating the Levenshtein Distance Between Two Strings
The concept of distance between strings is important in inductive learning (AI), cryptography, proteins research, and in other areas.
The Levenshtein distance is the minimum number of modifications needed to change one string into another, using three basic modification operations: del(-etion), ins(-ertion), and sub(-stitution). A substitution is also considered to be a combination of a deletion and insertion (indel).
There are various approaches to this, but we will avoid getting too technical. Suffice it to say that this Ruby implementation (in Listing 2.2) allows you to provide optional parameters to set the cost for the three types of modification operations and defaults to a single indel cost basis (cost of insertion = cost of deletion).
Listing 2.2. The Levenshtein Distance
Now that we have the Levenshtein distance defined, it's conceivable that we could define a similar? method, giving it a threshold for similarity. For example:
class String def similar?(other, thresh=2) if self.levenshtein(other) < thresh true else false end end end if "polarity".similar?("hilarity") puts "Electricity is funny!" end
Of course, it would also be possible to pass in the three weighted costs to the similar? method so that they could in turn be passed into the levenshtein method. We have omitted these for simplicity.