blib/lib/Text/Fuzzy.pm

filter: Statementstmt 83.3% 75 / 90 Branchbran 67.3% 31 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 4 / 6 Podpod 100% 4 / 4 totaltotal 78.0% 114 / 146 CC 31 SCAR 32.2
CC
31
Cov
78%
nearestv
30.0
fuzzy_index
27.7
distance_edits
2.0
CRAP 25.1
↑ Summary Fuzzy.xs »
1 package Text::Fuzzy;
2 12 require Exporter;
3 12 require DynaLoader;
4  
5 12 @ISA = qw(Exporter DynaLoader);
6  
7 12 @EXPORT_OK = qw/fuzzy_index distance_edits/;
8 12 %EXPORT_TAGS = (
9 all => \@EXPORT_OK,
10 );
11  
12 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 our $VERSION = '0.29';
15  
16 12 __PACKAGE__->bootstrap ($VERSION);
17  
18 # The following routine exports the C routines for the benefit of
19 # "CPAN::Nearest".
20  
21 sub dl_load_flags
22 12 ▶{
Subroutine dl_load_flags: called CC=1 SCAR=0.0
Pod documented
23 12 return 0x01;
24 }
25  
26 # This is a Perl-based edit distance routine which also returns the
27 # edit steps necessary to convert one string into the other. $distance
28 # is a boolean. If true it switches on
29  
30 12 our $verbose;
31  
32 sub distance_edits
33 0 ▶{
Subroutine distance_edits: not called CC=1 SCAR=6.9
Pod documented
34 0 return fuzzy_index (@_, 1);
35 }
36  
37 sub fuzzy_index
38 2 ▶{
Subroutine fuzzy_index: called CC=27 SCAR=33.2
Pod documented
39 # $distance is usually 0 or undefined here.
40  
41 2 my ($needle, $haystack, $distance) = @_;
42  
43 # Test whether the inputs make any sense here.
44  
45 2 my $m = length ($needle);
46 2 my $n = length ($haystack);
47 2 my $longer;
48 2 ▶ if ($distance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distance) 1 1 2
49 1 ▶ $longer = $m > $n ? $m : $n;
Branch: 1 branch
BranchTrueFalseTotal
$m > $n ? : 0 1 1
50 }
51 2 my @haystack = split '', $haystack;
52 2 my @needle = split '', $needle;
53 2 ▶ print " ", join (" ",@haystack), "\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $Text::Fuzzy::verbose 0 2 2
54 2 my @row1;
55 2 ▶ print " ", join (" ",@row1), "\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $Text::Fuzzy::verbose 0 2 2
56 2 my @row2;
57 my @way;
58 2 ▶ if ($distance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distance) { } 1 1 2
59 1 for (0..$n) {
60 5 $way[0][$_] = "i" x $_;
61 }
62 1 @row1 = map {$_} (0..$n);
63 }
64 else {
65 1 @row1 = (0) x ($n + 1);
66 1 for (0..$n) {
67 84 $way[0][$_] = '';
68 }
69 }
70 2 for (0..$m) {
71 9 $way[$_][0] = "d" x $_;
72 }
73 2 for my $i (1..$m) {
74 7 $row2[0] = $i;
75 7 ▶ print "[", $needle[$i - 1], "] " if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $Text::Fuzzy::verbose 0 7 7
76 7 ▶ print $row2[0]," " if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $Text::Fuzzy::verbose 0 7 7
77 7 for my $j (1..$n) {
78 344 my $cost = ($needle[$i-1] ne $haystack[$j-1]);
79 344 my $deletion = $row1[$j] + 1;
80 344 my $insertion = $row2[$j-1] + 1;
81 344 my $substitution = $row1[$j-1] + $cost;
82 344 my $min;
83 my $way;
84 344 $min = $deletion;
85 344 $way = 'd';
86 344 ▶ if ($min > $insertion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min > $insertion) 15 329 344
87 15 $min = $insertion;
88 15 $way = 'i';
89 }
90 344 ▶ if ($min > $substitution) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min > $substitution) 41 303 344
91 41 ▶ if ($cost) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cost) { } 16 25 41
92 16 $way = 'r';
93 }
94 else {
95 25 $way = 'k';
96 }
97 41 $min = $substitution;
98 }
99 344 ▶ if ($way eq 'd') {
Branch: 3 branches
BranchTrueFalseTotal
if ($way eq 'd') { } 289 55 344
elsif ($way eq 'i') { } 14 41 55
elsif ($way =~ /[kr]/) { } 41 0 41
100 289 ▶ $way[$i][$j] = ($way[$i-1][$j] ? $way[$i-1][$j]:'') . $way;
Branch: 1 branch
BranchTrueFalseTotal
$way[$i - 1][$j] ? : 209 80 289
101 }
102 elsif ($way eq 'i') {
103 14 ▶ $way[$i][$j] = ($way[$i][$j-1] ? $way[$i][$j-1]:'') . $way;
Branch: 1 branch
BranchTrueFalseTotal
$way[$i][$j - 1] ? : 14 0 14
104 }
105 elsif ($way =~ /[kr]/) {
106 41 ▶ $way[$i][$j] = ($way[$i-1][$j-1] ? $way[$i-1][$j-1]:'') . $way;
Branch: 1 branch
BranchTrueFalseTotal
$way[$i - 1][$j - 1] ? : 37 4 41
107 }
108 else {
109 0 die "Internal bug: unrecognized path";
110 }
111 344 $row2[$j] = $min;
112 344 ▶ print $row2[$j],$way[$i][$j]," " if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $Text::Fuzzy::verbose 0 344 344
113 }
114 7 @row1 = @row2;
115 7 ▶ print "\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $Text::Fuzzy::verbose 0 7 7
116 }
117 2 ▶ if ($distance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distance) { } 1 1 2
118 1 return ($row1[$n], $way[$m][$n]);
119 }
120 else {
121 # Windows doesn't like "inf" apparently.
122 1 my $mindistance = 1_000_000_000;
123 1 my $bestmatch;
124
125 1 for my $j (1..$n) {
126  
127 # The best distance we have found so far.
128  
129 83 ▶ if ($row2[$j] < $mindistance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row2[$j] < $mindistance) 4 79 83
130 4 $bestmatch = $j;
131 4 $mindistance = $row2[$j];
132 }
133 }
134 1 return ($bestmatch, $way[$m][$bestmatch], $mindistance);
135 }
136 }
137  
138 sub nearestv
139 0 ▶{
Subroutine nearestv: not called CC=5 SCAR=34.0
Pod documented
140 0 my ($tf, $array_ref) = @_;
141 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
142 0 my @values;
143 0 my @offsets = $tf->nearest ($array_ref);
144 0 ▶ if (@offsets) {
Branch: 1 branch
BranchTrueFalseTotal
if (@offsets) { } 0 0 0
145 0 for (@offsets) {
146 0 push @values, $array_ref->[$_];
147 }
148 0 return @values;
149 }
150 else {
151 0 return ();
152 }
153 }
154 else {
155 0 my $offset = $tf->nearest ($array_ref);
156 0 ▶ if (defined $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset) { } 0 0 0
157 0 return $array_ref->[$offset];
158 }
159 else {
160 0 return undef;
161 }
162 }
163 }
164  
165  
166 12 1;
↑ Summary Fuzzy.xs »