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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Levenshtein distance step by step in the Dyalect 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 Dyalect programming language

Source code in the dyalect programming language

func levenshtein(s, t) {
    var n = s.Length()
    var m = t.Length()
    var d = Array.Empty(n + 1, () => Array.Empty(m + 1))
 
    if n == 0 {
        return m
    }
 
    if (m == 0) {
        return n
    }
 
    for i in 0..n {
        d[i][0] = i
    }
 
    for j in 0..m {
        d[0][j] = j
    }
 
    for j in 1..m {
        for i in 1..n {
            if s[i - 1] == t[j - 1] {
                d[i][j] = d[i - 1][j - 1] //no operation
            }
            else {
                d[i][j] = min(min(
                    d[i - 1][j] + 1,    //a deletion
                    d[i][j - 1] + 1),   //an insertion
                    d[i - 1][j - 1] + 1 //a substitution
                    )
            }
        }
    }
 
    d[n][m]
}
 
func run(x, y) {
    print("\(x) -> \(y) = \(levenshtein(x, y))")
}
 
run("rosettacode", "raisethysword")

  

You may also check:How to resolve the algorithm Factorial step by step in the FALSE programming language
You may also check:How to resolve the algorithm Tau function step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Tau function step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Create a file step by step in the Lingo programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Asymptote programming language