/usr/share/perl5/Text/Levenshtein/Damerau.pm is in libtext-levenshtein-damerau-perl 0.41-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | package Text::Levenshtein::Damerau;
use 5.008_008; # for utf8, sorry legacy Perls
use strict;
use utf8;
use List::Util qw/reduce/;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw/edistance/;
our $VERSION = '0.41';
# To XS or not to XS...
unless ( _set_backend('Text::Levenshtein::Damerau::XS::xs_edistance') ) {
_set_backend('Text::Levenshtein::Damerau::PP::pp_edistance');
}
sub new {
my $class = shift;
my $self = {};
$self->{'source'} = shift;
$self->{'max_distance'} = 0;
bless( $self, $class );
return $self;
}
sub dld {
my $self = shift;
my $args = shift;
if ( !ref $args ) {
return edistance( $self->{'source'}, $args, );
}
elsif ( ref $args->{'list'} eq ref [] ) {
my $target_score;
if ( defined( $args->{'backend'} ) ) {
_set_backend( $args->{'backend'} );
}
foreach my $target ( @{ $args->{'list'} } ) {
my $ed =
edistance( $self->{'source'}, $target, $args->{max_distance} );
$target_score->{$target} = $ed if ( $ed >= 0 );
}
return $target_score;
}
}
sub dld_best_match {
my $self = shift;
my $args = shift;
if ( defined( $args->{'list'} ) ) {
my $hash_ref = $self->dld($args);
#Get the hashref key with the smallest value
return reduce { $hash_ref->{$a} < $hash_ref->{$b} ? $a : $b }
keys %{$hash_ref};
}
}
sub dld_best_distance {
my $self = shift;
my $args = shift;
if ( defined( $args->{'list'} ) ) {
return $self->dld( $self->dld_best_match($args) );
}
}
sub _set_backend {
my $be = my $mod = shift;
$mod =~ s/^(.*)::.*?$/$1/;
local $@;
eval "require $mod";
unless ($@) {
eval "defined &$be";
unless($@) {
# We welcome our new edistance overlord
*edistance = \&$be;
return 1;
}
}
return 0;
}
1;
__END__
=encoding utf8
=head1 NAME
Text::Levenshtein::Damerau - Damerau Levenshtein edit distance.
=head1 SYNOPSIS
use Text::Levenshtein::Damerau;
use warnings;
use strict;
my @targets = ('fuor','xr','fourrrr','fo');
# Initialize Text::Levenshtein::Damerau object with text to compare against
my $tld = Text::Levenshtein::Damerau->new('four');
print $tld->dld($targets[0]);
# prints 1
my $tld = $tld->dld({ list => \@targets });
print $tld->{'fuor'};
# prints 1
print $tld->dld_best_match({ list => \@targets });
# prints fuor
print $tld->dld_best_distance({ list => \@targets });
# prints 1
# or even more simply
use Text::Levenshtein::Damerau qw/edistance/;
use warnings;
use strict;
print edistance('Neil','Niel');
# prints 1
=head1 DESCRIPTION
Returns the true Damerau Levenshtein edit distance of strings with adjacent transpositions. Useful for fuzzy matching, DNA variation metrics, and fraud detection.
Defaults to using Pure Perl L<Text::Levenshtein::Damerau::PP>, but has an XS addon L<Text::Levenshtein::Damerau::XS> for massive speed imrovements. Works correctly with utf8 if backend supports it; known to work with C<Text::Levenshtein::Damerau::PP> and C<Text::Levenshtein::Damerau::XS>.
use utf8;
my $tld = Text::Levenshtein::Damerau->new('ⓕⓞⓤⓡ');
print $tld->dld('ⓕⓤⓞⓡ');
# prints 1
=head1 CONSTRUCTOR
=head2 new
Creates and returns a C<Text::Levenshtein::Damerau> object. Takes a scalar with the text (source) you want to compare against.
my $tld = Text::Levenshtein::Damerau->new('Neil');
# Creates a new Text::Levenshtein::Damerau object $tld
=head1 METHODS
=head2 $tld->dld
B<Scalar> Argument: Takes a string to compare with.
Returns: an integer representing the edit distance between the source and the passed argument.
B<Hashref> Argument: Takes a hashref containing:
=over 4
=item * list => \@array (array ref of strings to compare with)
=item * I<OPTIONAL> max_distance => $int (only return results with $int distance or less).
=item * I<OPTIONAL> backend => 'Some::Module::its_function' Any module that will take 2 arguments and returns an int. If the module fails to load, the function doesn't exist, or the function doesn't return a number when passed 2 strings, then C<backend> remains unchanged.
# Override defaults and use Text::Levenshtein::Damerau::PP's pp_edistance()
$tld->dld({ list=> \@list, backend => 'Text::Levenshtein::Damerau::PP::pp_edistance');
# Override defaults and use Text::Levenshtein::Damerau::XS's xs_edistance()
use Text::Levenshtein::Damerau;
requires Text::Levenshtein::Damerau::XS;
...
$tld->dld({ list=> \@list, backend => 'Text::Levenshtein::Damerau::XS::xs_edistance');
=back
Returns: hashref with each word from the passed list as keys, and their edit distance (if less than max_distance, which is unlimited by default).
my $tld = Text::Levenshtein::Damerau->new('Neil');
print $tld->dld( 'Niel' );
# prints 1
#or if you want to check the distance of various items in a list
my @names_list = ('Niel','Jack');
my $tld = Text::Levenshtein::Damerau->new('Neil');
my $d_ref = $tld->dld({ list=> \@names_list }); # pass a list, returns a hash ref
print $d_ref->{'Niel'}; #prints 1
print $d_ref->{'Jack'}; #prints 4
=head2 $tld->dld_best_match
Argument: an array reference of strings.
Returns: the string with the smallest edit distance between the source and the array of strings passed.
Takes distance of $tld source against every item in @targets, then returns the string of the best match.
my $tld = Text::Levenshtein::Damerau->new('Neil');
my @name_spellings = ('Niel','Neell','KNiel');
print $tld->dld_best_match({ list=> \@name_spellings });
# prints Niel
=head2 $tld->dld_best_distance
Arguments: an array reference of strings.
Returns: the smallest edit distance between the source and the array reference of strings passed.
Takes distance of $tld source against every item in the passed array, then returns the smallest edit distance.
my $tld = Text::Levenshtein::Damerau->new('Neil');
my @name_spellings = ('Niel','Neell','KNiel');
print $tld->dld_best_distance({ list => \@name_spellings });
# prints 1
=head1 EXPORTABLE METHODS
=head2 edistance
Arguments: source string and target string.
=over
=item * I<OPTIONAL 3rd argument> int (max distance; only return results with $int distance or less). 0 = unlimited. Default = 0.
=back
Returns: int that represents the edit distance between the two argument. -1 if max distance is set and reached.
Wrapper function to take the edit distance between a source and target string. It will attempt to use, in order:
=over 4
=item * L<Text::Levenshtein::Damerau::XS> B<xs_edistance>
=item * L<Text::Levenshtein::Damerau::PP> B<pp_edistance>
=back
use Text::Levenshtein::Damerau qw/edistance/;
print edistance('Neil','Niel');
# prints 1
=head1 SEE ALSO
=over 4
=item * L<https://github.com/ugexe/Text--Levenshtein--Damerau> I<Repository>
=item * L<http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance> I<Damerau levenshtein explanation>
=item * L<Text::Fuzzy> I<Regular levenshtein distance>
=back
=head1 BUGS
Please report bugs to:
L<https://rt.cpan.org/Public/Dist/Display.html?Name=Text-Levenshtein-Damerau>
=head1 AUTHOR
Nick Logan <F<ug@skunkds.com>>
=head1 LICENSE AND COPYRIGHT
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut
|