Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 23 lines (19 sloc) 0.583 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
require 'amatch'
require 'unicode'

module Distance
  include Amatch
  
  def self.edit_distance(s1, s2)
    s1,s2 = s1.to_s.downcase,s2.to_s.downcase
    Levenshtein.new(s1).match(s2).to_f / [s1.size, s2.size].max
  end
  
  def self.edit_distance_initial(s1, s2)
    s1,s2 = Unicode.downcase(s1), Unicode.downcase(s2)
    if s1 =~ /^[a-zäöüÄÖÜ]\.*/u || s2 =~ /^[a-zäöüÄÖÜ]\.*/u
      i1 = s1 =~ /^[äöüÄÖÜ]/u ? s1[0,2] : s1[0,1]
      i2 = s2 =~ /^[äöüÄÖÜ]/u ? s2[0,2] : s2[0,1]
      i1 == i2 ? 0 : 1
    else
      edit_distance(s1,s2)
    end
  end
  
end
Something went wrong with that request. Please try again.