How to resolve the algorithm Levenshtein distance step by step in the COBOL programming language
How to resolve the algorithm Levenshtein distance step by step in the COBOL 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 COBOL programming language
Source code in the cobol programming language
identification division.
program-id. Levenshtein.
environment division.
configuration section.
repository.
function all intrinsic.
data division.
working-storage section.
77 string-a pic x(255).
77 string-b pic x(255).
77 length-a pic 9(3).
77 length-b pic 9(3).
77 distance pic z(3).
77 i pic 9(3).
77 j pic 9(3).
01 tab.
05 filler occurs 256.
10 filler occurs 256.
15 costs pic 9(3).
procedure division.
main.
move "kitten" to string-a
move "sitting" to string-b
perform levenshtein-dist
move "rosettacode" to string-a
move "raisethysword" to string-b
perform levenshtein-dist
stop run
.
levenshtein-dist.
move length(trim(string-a)) to length-a
move length(trim(string-b)) to length-b
initialize tab
perform varying i from 0 by 1 until i > length-a
move i to costs(i + 1, 1)
end-perform
perform varying j from 0 by 1 until j > length-b
move j to costs(1, j + 1)
end-perform
perform with test after varying i from 2 by 1 until i > length-a
perform with test after varying j from 2 by 1 until j > length-b
if string-a(i - 1:1) = string-b(j - 1:1)
move costs(i - 1, j - 1) to costs(i, j)
else
move min(min(costs(i - 1, j) + 1, *> a deletion
costs(i, j - 1) + 1), *> an insertion
costs(i - 1, j - 1) + 1) *> a substitution
to costs(i, j)
end-if
end-perform
end-perform
move costs(length-a + 1, length-b + 1) to distance
display trim(string-a) " -> " trim(string-b) " = " trim(distance)
.
You may also check:How to resolve the algorithm ABC problem step by step in the C++ programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the MINIL programming language
You may also check:How to resolve the algorithm Atomic updates step by step in the Go programming language
You may also check:How to resolve the algorithm Best shuffle step by step in the ALGOL 68 programming language