line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PERLANCAR::Text::Levenshtein; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2015-09-20'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '0.02'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
#use 5.010001; |
7
|
|
|
|
|
|
|
#use strict; |
8
|
|
|
|
|
|
|
#use warnings; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#require Exporter; |
11
|
|
|
|
|
|
|
#our @ISA = qw(Exporter); |
12
|
|
|
|
|
|
|
#our @EXPORT_OK = qw(editdist); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub __min(@) { |
15
|
20
|
|
|
20
|
|
22
|
my $m = $_[0]; |
16
|
20
|
|
|
|
|
26
|
for (@_) { |
17
|
60
|
100
|
|
|
|
112
|
$m = $_ if $_ < $m; |
18
|
|
|
|
|
|
|
} |
19
|
20
|
|
|
|
|
37
|
$m; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# straight copy of Wikipedia's "Levenshtein Distance" |
23
|
|
|
|
|
|
|
sub editdist { |
24
|
3
|
|
|
3
|
1
|
19
|
my @a = split //, shift; |
25
|
3
|
|
|
|
|
9
|
my @b = split //, shift; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# There is an extra row and column in the matrix. This is the distance from |
28
|
|
|
|
|
|
|
# the empty string to a substring of the target. |
29
|
3
|
|
|
|
|
5
|
my @d; |
30
|
3
|
|
|
|
|
20
|
$d[$_][0] = $_ for 0 .. @a; |
31
|
3
|
|
|
|
|
13
|
$d[0][$_] = $_ for 0 .. @b; |
32
|
|
|
|
|
|
|
|
33
|
3
|
|
|
|
|
5
|
for my $i (1 .. @a) { |
34
|
9
|
|
|
|
|
16
|
for my $j (1 .. @b) { |
35
|
30
|
100
|
|
|
|
89
|
$d[$i][$j] = ( |
36
|
|
|
|
|
|
|
$a[$i-1] eq $b[$j-1] |
37
|
|
|
|
|
|
|
? $d[$i-1][$j-1] |
38
|
|
|
|
|
|
|
: 1 + __min( |
39
|
|
|
|
|
|
|
$d[$i-1][$j], |
40
|
|
|
|
|
|
|
$d[$i][$j-1], |
41
|
|
|
|
|
|
|
$d[$i-1][$j-1] |
42
|
|
|
|
|
|
|
) |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
3
|
|
|
|
|
20
|
$d[@a][@b]; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
1; |
51
|
|
|
|
|
|
|
# ABSTRACT: Calculate Levenshtein edit distance |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
__END__ |