How to resolve the algorithm Levenshtein distance step by step in the Objeck programming language
How to resolve the algorithm Levenshtein distance step by step in the Objeck 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 Objeck programming language
Source code in the objeck programming language
class Levenshtein {
function : Main(args : String[]) ~ Nil {
if(args->Size() = 2) {
s := args[0]; t := args[1]; d := Distance(s,t);
"{$s} -> {$t} = {$d}"->PrintLine();
};
}
function : native : Distance(s : String,t : String) ~ Int {
d := Int->New[s->Size() + 1, t->Size() + 1];
for(i := 0; i <= s->Size(); i += 1;) {
d[i,0] := i;
};
for(j := 0; j <= t->Size(); j += 1;) {
d[0,j] := j;
};
for(j := 1; j <= t->Size(); j += 1;) {
for(i := 1; i <= s->Size(); i += 1;) {
if(s->Get(i - 1) = t->Get(j - 1)) {
d[i,j] := d[i - 1, j - 1];
}
else {
d[i,j] := (d[i - 1, j] + 1)
->Min(d[i, j - 1] + 1)
->Min(d[i - 1, j - 1] + 1);
};
};
};
return d[s->Size(), t->Size()];
}
}
You may also check:How to resolve the algorithm Topological sort step by step in the E programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the Mercury programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Pascal programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the APL programming language
You may also check:How to resolve the algorithm Create a file on magnetic tape step by step in the D programming language