class Sentino::Distance

Public Class Methods

new(reference) click to toggle source
# File lib/sentino/distance.rb, line 4
def initialize(reference)
  @reference = reference
end

Public Instance Methods

match(string) click to toggle source
# File lib/sentino/distance.rb, line 8
def match(string)
  check(string)
end

Private Instance Methods

check(string) click to toggle source
# File lib/sentino/distance.rb, line 14
def check(string)
  result = levenshtein(@reference, string)
  return result
end
levenshtein(first, second) click to toggle source
# File lib/sentino/distance.rb, line 20
def levenshtein(first, second)
  matrix = [(0..first.length).to_a]
  (1..second.length).each do |j|
    matrix << [j] + [0] * (first.length)
  end

  (1..second.length).each do |i|
    (1..first.length).each do |j|
      if first[j-1] == second[i-1]
        matrix[i][j] = matrix[i-1][j-1]
      else
        matrix[i][j] = [
          matrix[i-1][j],
          matrix[i][j-1],
          matrix[i-1][j-1],
        ].min + 1
      end
    end
  end
  return matrix.last.last
end