Module: Text::Levenshtein

Extended by:
Levenshtein
Included in:
Levenshtein
Defined in:
lib/text/levenshtein.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#distance(str1, str2, max_distance = nil) ⇒ Object

Calculate the Levenshtein distance between two strings str1 and str2.

The optional argument max_distance can reduce the number of iterations by stopping if the Levenshtein distance exceeds this value. This increases performance where it is only necessary to compare the distance with a reference value instead of calculating the exact distance.

The distance is calculated in terms of Unicode codepoints. Be aware that this algorithm does not perform normalisation: if there is a possibility of different normalised forms being used, normalisation should be performed beforehand.



29
30
31
32
33
34
35
# File 'lib/text/levenshtein.rb', line 29

def distance(str1, str2, max_distance = nil)
  if max_distance
    distance_with_maximum(str1, str2, max_distance)
  else
    distance_without_maximum(str1, str2)
  end
end