blib/lib/PERLANCAR/Text/Levenshtein.pm

filter: Statementstmt 100% 16 / 16 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod 100% 1 / 1 totaltotal 100% 23 / 23 CC 8 SCAR 16.1
CC
8
Cov
100%
editdist
6.0
__min
3.0
CRAP 5.0
↑ Summary
1 package PERLANCAR::Text::Levenshtein;
2  
3 2 our $DATE = '2015-09-20'; # DATE
4 2 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 20 ▶sub __min(@) {
Subroutine __min: called CC=3 SCAR=11.0
15 20 my $m = $_[0];
16 20 for (@_) {
17 60 ▶ $m = $_ if $_ < $m;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $m 13 47 60
18 }
19 20 $m;
20 }
21  
22 # straight copy of Wikipedia's "Levenshtein Distance"
23 3 ▶sub editdist {
Subroutine editdist: called CC=6 SCAR=17.9
Pod documented
24 3 my @a = split //, shift;
25 3 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 my @d;
30 3 $d[$_][0] = $_ for 0 .. @a;
31 3 $d[0][$_] = $_ for 0 .. @b;
32  
33 3 for my $i (1 .. @a) {
34 9 for my $j (1 .. @b) {
35 30 ▶ $d[$i][$j] = (
Branch: 1 branch
BranchTrueFalseTotal
$a[$i - 1] eq $b[$j - 1] ? : 10 20 30
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 $d[@a][@b];
48 }
49  
50 2 1;
51 # ABSTRACT: Calculate Levenshtein edit distance
52  
53 __END__
↑ Summary