Sha256: fde6fb26f0c089b810a085f244bff1d1b322d9ac2806a217be1a3289d61fc878

Contents?: true

Size: 724 Bytes

Versions: 234

Compression:

Stored size: 724 Bytes

Contents

module RailsGuides
  module Levenshtein
    # Based on the pseudocode in http://en.wikipedia.org/wiki/Levenshtein_distance.
    def self.distance(s1, s2)
      s = s1.unpack('U*')
      t = s2.unpack('U*')
      m = s.length
      n = t.length

      # matrix initialization
      d = []
      0.upto(m) { |i| d << [i] }
      0.upto(n) { |j| d[0][j] = j }

      # distance computation
      1.upto(m) do |i|
        1.upto(n) do |j|
          cost = s[i] == t[j] ? 0 : 1
          d[i][j] = [
            d[i-1][j] + 1,      # deletion
            d[i][j-1] + 1,      # insertion
            d[i-1][j-1] + cost, # substitution
          ].min
        end
      end

      # all done
      return d[m][n]
    end
  end
end

Version data entries

234 entries across 195 versions & 21 rubygems

Version Path
railties-3.2.14.rc1 guides/rails_guides/levenshtein.rb
swipe-rails-0.0.5 vendor/bundle/gems/railties-3.2.13/guides/rails_guides/levenshtein.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/guides/rails_guides/levenshtein.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/guides/rails_guides/levenshtein.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/guides/rails_guides/levenshtein.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/guides/rails_guides/levenshtein.rb
font-awesome-rails-3.1.1.2 vendor/ruby/2.0.0/gems/railties-3.2.12/guides/rails_guides/levenshtein.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/railties-3.2.13/guides/rails_guides/levenshtein.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/railties-3.2.12/guides/rails_guides/levenshtein.rb
font-awesome-rails-3.1.1.1 vendor/ruby/2.0.0/gems/railties-3.2.12/guides/rails_guides/levenshtein.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/railties-3.2.12/guides/rails_guides/levenshtein.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/railties-3.2.13/guides/rails_guides/levenshtein.rb
challah-1.0.0.beta3 vendor/bundle/gems/railties-3.2.13/guides/rails_guides/levenshtein.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/guides/rails_guides/levenshtein.rb
challah-1.0.0.beta2 vendor/bundle/gems/railties-3.2.13/guides/rails_guides/levenshtein.rb
challah-1.0.0.beta vendor/bundle/gems/railties-3.2.13/guides/rails_guides/levenshtein.rb
challah-1.0.0.beta vendor/bundle/gems/railties-3.2.11/guides/rails_guides/levenshtein.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/guides/rails_guides/levenshtein.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/guides/rails_guides/levenshtein.rb
fc-webicons-0.0.1 vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/guides/rails_guides/levenshtein.rb