| 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
| |||||||||||||||||||
| 49 | 1 | ▶ | $longer = $m > $n ? $m : $n; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 50 | } | ||||||||||||||||||
| 51 | 2 | my @haystack = split '', $haystack; | |||||||||||||||||
| 52 | 2 | my @needle = split '', $needle; | |||||||||||||||||
| 53 | 2 | ▶ | print " ", join (" ",@haystack), "\n" if $verbose; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 54 | 2 | my @row1; | |||||||||||||||||
| 55 | 2 | ▶ | print " ", join (" ",@row1), "\n" if $verbose; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 56 | 2 | my @row2; | |||||||||||||||||
| 57 | my @way; | ||||||||||||||||||
| 58 | 2 | ▶ | if ($distance) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 76 | 7 | ▶ | print $row2[0]," " if $verbose; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 87 | 15 | $min = $insertion; | |||||||||||||||||
| 88 | 15 | $way = 'i'; | |||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | 344 | ▶ | if ($min > $substitution) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 91 | 41 | ▶ | if ($cost) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 100 | 289 | ▶ | $way[$i][$j] = ($way[$i-1][$j] ? $way[$i-1][$j]:'') . $way; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 101 | } | ||||||||||||||||||
| 102 | elsif ($way eq 'i') { | ||||||||||||||||||
| 103 | 14 | ▶ | $way[$i][$j] = ($way[$i][$j-1] ? $way[$i][$j-1]:'') . $way; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | elsif ($way =~ /[kr]/) { | ||||||||||||||||||
| 106 | 41 | ▶ | $way[$i][$j] = ($way[$i-1][$j-1] ? $way[$i-1][$j-1]:'') . $way; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 113 | } | ||||||||||||||||||
| 114 | 7 | @row1 = @row2; | |||||||||||||||||
| 115 | 7 | ▶ | print "\n" if $verbose; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 116 | } | ||||||||||||||||||
| 117 | 2 | ▶ | if ($distance) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 142 | 0 | my @values; | |||||||||||||||||
| 143 | 0 | my @offsets = $tf->nearest ($array_ref); | |||||||||||||||||
| 144 | 0 | ▶ | if (@offsets) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 157 | 0 | return $array_ref->[$offset]; | |||||||||||||||||
| 158 | } | ||||||||||||||||||
| 159 | else { | ||||||||||||||||||
| 160 | 0 | return undef; | |||||||||||||||||
| 161 | } | ||||||||||||||||||
| 162 | } | ||||||||||||||||||
| 163 | } | ||||||||||||||||||
| 164 | |||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | 12 | 1; | |||||||||||||||||