class FuzzyString::Levenshtein

Public Class Methods

distance(first,second) click to toggle source
# File lib/fuzzy_string/levenshtein.rb, line 2
def self.distance(first,second) new(first,second).distance end
new(first,second) click to toggle source
# File lib/fuzzy_string/levenshtein.rb, line 3
def initialize(first,second)
  @first = first
  @second = second
end

Public Instance Methods

distance() click to toggle source
# File lib/fuzzy_string/levenshtein.rb, line 7
def distance
  v0 = (0..@second.length).map{|a| a}
  @first.length.times {|i| v0 = new_row(v0,i) }
  return v0[@second.length]
end

Private Instance Methods

cost(old,i,j) click to toggle source
# File lib/fuzzy_string/levenshtein.rb, line 18
def cost(old,i,j) cost = @first[i] == @second[j] ? 0 : @first[i].downcase == @second[j].downcase ? 0.75 : 1 end
new_row(old,i) click to toggle source
# File lib/fuzzy_string/levenshtein.rb, line 13
def new_row(old,i)
  row = [i + 1]
  @second.length.times {|j| row[j + 1] = [row[j] + 1, old[j + 1] + 1, old[j] + cost(old,i,j)].min}
  return row
end