1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#!/usr/bin/perl -s
use v5.16;
use Test2::V0 '!float';
use PDL;
use experimental 'signatures';
our ($tests, $examples, $verbose);
run_tests() if $tests || $examples; # does not return
say(<<EOS), exit unless @ARGV == 2;
usage: $0 [-tests] [-examples] [-verbose] [--] [source target]
-tests
run some tests
-examples
run the examples from the challenge
-verbose
display edit operations and internal distance matrix
source target
calculate levenshtein distance between source and target words
EOS
### Input and Output
say levenshtein_distance($ARGV[0], $ARGV[1]);
### Implementation
sub cost;
sub min_ind;
# Calculate the Levenshtein distance between two words, i.e. the minimum
# number of insert, delete or replace actions to transform the first
# word into the second. PDL implementation of the Wagner-Fischer
# algorithm, see https://en.wikipedia.org/wiki/Wagner-Fischer_algorithm
sub levenshtein_distance ($source, $target) {
# Split words into arrays and (un)shift by one to match matrix
# dimensions.
my @source = (undef, split //, $source);
my @target = (undef, split //, $target);
# Prepare distance matrix holding BAD values only.
my $dist = zeros(long, @source + 0, @target + 0)->setvaltobad(0);
# Fill first row and column with ascending sequences as the costs of
# removing or inserting a prefix of that length.
$dist->slice(',(0)') .= sequence $dist->dim(0);
$dist->slice('(0),') .= sequence $dist->dim(1);
# To fill the remaining fields, loop over the indices of all BAD
# values. With the index order provided by "whichND" the required
# "flood filling" is achieved.
for my $idx ($dist->isbad->whichND->dog) {
# Character pair for index position.
my $cs = $source[$idx->at(0)];
my $ct = $target[$idx->at(1)];
# 2x2 neighborhood matrix preceeding index.
my $nb = $dist->range($idx - 1, 2);
# 2x2 transition cost matrix.
my $trans = cost $cs ne $ct;
# The cost to reach the new field is the minimum of the sums of
# the previous cost and the transition cost.
$dist->indexND($idx) .= min $nb + $trans;
}
explain_edit(\@source, \@target, $dist) if $verbose;
# The requested Levenshtein distance is the bottom right matrix
# element.
$dist->indexND($dist->shape - 1);
}
# Backtrack the steps that led to the lower right corner of the distance
# matrix and describe the corresponding edit operations.
sub explain_edit ($source, $target, $dist) {
# Collect edit operations.
my @edit;
# Start at bottom right corner.
my $idx = $dist->shape - 1;
# Stop at upper left corner.
while (any $idx) {
# Character pair for index position.
my $cs = $source->[$idx->at(0)];
my $ct = $target->[$idx->at(1)];
# 2x2 neighborhood matrix preceeding index, truncated to BAD
# values across the matrix' borders.
my $nb = $dist->range($idx - 1, 2, 'truncate');
# The field preceeding the current field holds the minimum
# within the neighborhood. Find the minimum and the
# corresponding index. On a non-unique minimum, this will
# prefer the diagonal "replace" move.
my ($min, $pred) = min_ind $nb;
# Record the edit operation.
unshift @edit,
any($pred) ?
$pred->at(0) ?
"ins($ct)" :
"del($cs)" :
$dist->indexND($idx) == $min ?
"keep($cs)" :
"repl($cs,$ct)";
# Move to predecessor field.
$idx = $idx - 1 + $pred;
}
{
no warnings 'uninitialized';
say @$source, ' -> ', @$target, ':';
}
say "@edit";
say "Levenshtein distance = ", $dist->indexND($dist->shape - 1);
say "Wagner-Fischer distance matrix:", $dist;
}
# Cost matrix for a single insert, delete and keep/replace.
sub cost ($repl) {
long([0, 1], [1, 0])->setvaltobad(0)->set(0, 0, $repl);
}
# Find the minimum in an N-dim piddle together with the corresponding
# index.
sub min_ind ($pdl) {
my $min = min $pdl;
my $index = whichND($pdl == $min)->slice(',(0)');
($min, $index);
}
### Examples and tests
sub run_tests {
SKIP: {
skip "examples" unless $examples;
is levenshtein_distance('kitten', 'sitting'), 3, 'example 1';
is levenshtein_distance('sunday', 'monday'), 2, 'example 2';
is levenshtein_distance('Saturday', 'Sunday'), 3,
'example from wikipedia';
is levenshtein_distance('parachute', 'headache'), 5,
'another example';
}
SKIP: {
skip "tests" unless $tests;
is levenshtein_distance('abcdef', 'uvwxyz'), 6, 'replace all';
is levenshtein_distance('def', 'abcdef'), 3, 'insert prefix';
is levenshtein_distance('abcdef', 'def'), 3, 'delete prefix';
is levenshtein_distance('abcdef', 'uvwdef'), 3, 'replace prefix';
is levenshtein_distance('abc', 'abcdef'), 3, 'insert suffix';
is levenshtein_distance('abcdef', 'abc'), 3, 'delete suffix';
is levenshtein_distance('abcdef', 'abcxyz'), 3, 'replace suffix';
is levenshtein_distance('abef', 'abcdef'), 2, 'insert infix';
is levenshtein_distance('abcdef', 'abef'), 2, 'delete infix';
is levenshtein_distance('abcdef', 'abwxef'), 2, 'replace infix';
}
done_testing;
exit;
}
|