Levenshtein distance/Alignment: Difference between revisions
Content deleted Content added
m whitespace cleanup |
+ D entry |
||
Line 11:
You can either implement an algorithm, or use a dedicated library (thus showing us how it is named in your language).
=={{header|D}}==
Using the standard library.
<lang d>void main() {
import std.stdio, std.algorithm;
immutable s1 = "rosettacode";
immutable s2 = "raisethysword";
const dd = levenshteinDistanceAndPath(s1, s2);
// EditOp Key:
// n = none. Current items are equal, no editing is necessary.
// s = substitute current target item with current source item.
// i = insert current item from the source into the target.
// r = remove current item from the target.
writeln("Levenshtein distance and edit operations:\n", dd, "\n");
string s1b, s2b;
size_t pos1, pos2;
foreach (immutable c; dd[1]) {
final switch (c) with (EditOp) {
case none, substitute:
s1b ~= s1[pos1++];
s2b ~= s2[pos2++];
break;
case insert:
s1b ~= "_";
s2b ~= s2[pos2++];
break;
case remove:
s1b ~= s1[pos1++];
s2b ~= "_";
break;
}
}
writeln("Alignments:\n", s1b, "\n", s2b);
}</lang>
{{out}}
<pre>Levenshtein distance and edit operations:
const(Tuple!(uint, EditOp[]))(8, nisnnnisssnss)
Alignments:
r_oset_tacode
raisethysword</pre>
=={{header|Perl}}==
|