How to resolve the algorithm Levenshtein distance step by step in the Crystal programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Levenshtein distance step by step in the Crystal programming language

Table of Contents

Problem Statement

In information theory and computer science, the Levenshtein distance is a metric for measuring the amount of difference between two sequences (i.e. an edit distance). The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character.

The Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there isn't a way to do it with fewer than three edits:

The Levenshtein distance between   "rosettacode",   "raisethysword"   is   8. The distance between two strings is same as that when both strings are reversed.

Implements a Levenshtein distance function, or uses a library function, to show the Levenshtein distance between   "kitten"   and   "sitting".

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Levenshtein distance step by step in the Crystal programming language

Source code in the crystal programming language

require "levenshtein"
puts Levenshtein.distance("kitten", "sitting")
puts Levenshtein.distance("rosettacode", "raisethysword")


module Levenshtein
 
  def self.distance(a, b)
    a, b = a.downcase, b.downcase
    costs = (0..b.size).to_a
    (1..a.size).each do |i|
      costs[0], nw = i, i - 1  # j == 0; nw is lev(i-1, j)
      (1..b.size).each do |j|
        costs[j], nw = [costs[j] + 1, costs[j-1] + 1, a[i-1] == b[j-1] ? nw : nw + 1].min, costs[j]
      end
    end
    costs[b.size]
  end
 
  def self.test
    %w{kitten sitting saturday sunday rosettacode raisethysword}.each_slice(2) do |(a, b)| #or do |pair| a, b = pair
      puts "distance(#{a}, #{b}) = #{distance(a, b)}"
    end
  end
 
end
 
Levenshtein.test


def levenshtein_distance(str1, str2)
  n, m = str1.size, str2.size
  max = n / 2
  
  return 0 if n == 0 || m == 0
  return n if (n - m).abs > max
 
  d = (0..m).to_a
  x = 0
 
  str1.each_char_with_index do |char1, i|
    e = i + 1
 
    str2.each_char_with_index do |char2, j|
      cost = (char1 == char2) ? 0 : 1
      x = [ d[j+1] + 1, # insertion
            e + 1,      # deletion
            d[j] + cost # substitution
          ].min
      d[j] = e
      e = x
    end
 
    d[m] = x
  end
  x
end

%w{kitten sitting saturday sunday rosettacode raisethysword}.each_slice(2) do |(a, b)| #or do |pair| a, b = pair
  puts "distance(#{a}, #{b}) = #{levenshtein_distance(a, b)}"
end


  

You may also check:How to resolve the algorithm Check Machin-like formulas step by step in the Perl programming language
You may also check:How to resolve the algorithm Draw a sphere step by step in the Lua programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Fortran programming language
You may also check:How to resolve the algorithm Percentage difference between images step by step in the Phix programming language
You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the XPL0 programming language