Class: Levenshtein

Inherits:
Object
  • Object
show all
Defined in:
lib/levenshteinish.rb

Class Method Summary collapse

Class Method Details

.distance(s1, s2, threshold = nil) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/levenshteinish.rb', line 5

def self.distance(s1, s2, threshold = nil)
  
  # If the arguments is nil
  s1 = s1.to_s unless s1
  s2 = s2.to_s unless s2
  
  s1, s2  = s2, s1  if s1.length > s2.length  # s1 is the short one; s2 is the long one.

  if s2.length == 0
    0.0 # Since s1.length < s2.length, s1 must be empty as well.
  else
    if threshold
      if (d = HintableLevenshtein.new.distance(s1, s2).to_f/s2.length) <= threshold
        d
      else
        nil
      end
    else
      HintableLevenshtein.new.distance(s1, s2).to_f/s2.length
    end
  end
end