| 1 |
NAME |
| 2 |
Text::Levenshtein::Damerau - Damerau Levenshtein edit distance. |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Text::Levenshtein::Damerau; |
| 6 |
|
| 7 |
my @targets = ('fuor','xr','fourrrr','fo'); |
| 8 |
|
| 9 |
# Initialize Text::Levenshtein::Damerau object with text to compare against |
| 10 |
my $tld = Text::Levenshtein::Damerau->new('four'); |
| 11 |
|
| 12 |
print $tld->dld($targets[0]); |
| 13 |
# prints 1 |
| 14 |
|
| 15 |
my $tld = $tld->dld({ list => \@targets }); |
| 16 |
print $tld->{'fuor'}; |
| 17 |
# prints 1 |
| 18 |
|
| 19 |
print $tld->dld_best_match({ list => \@targets }); |
| 20 |
# prints fuor |
| 21 |
|
| 22 |
print $tld->dld_best_distance({ list => \@targets }); |
| 23 |
# prints 1 |
| 24 |
|
| 25 |
|
| 26 |
# or even more simply |
| 27 |
use Text::Levenshtein::Damerau qw/edistance/; |
| 28 |
|
| 29 |
print edistance('Neil','Niel'); |
| 30 |
# prints 1 |
| 31 |
|
| 32 |
DESCRIPTION |
| 33 |
Returns the true Damerau Levenshtein edit distance of strings with |
| 34 |
adjacent transpositions. Useful for fuzzy matching, DNA variation |
| 35 |
metrics, and fraud detection. |
| 36 |
|
| 37 |
Defaults to using Pure Perl Text::Levenshtein::Damerau::PP, but has an |
| 38 |
XS addon Text::Levenshtein::Damerau::XS for massive speed imrovements. |
| 39 |
Works correctly with utf8 if backend supports it; known to work with |
| 40 |
"Text::Levenshtein::Damerau::PP" and "Text::Levenshtein::Damerau::XS". |
| 41 |
|
| 42 |
use Text::Levenshtein::Damerau; |
| 43 |
use utf8; |
| 44 |
|
| 45 |
my $tld = Text::Levenshtein::Damerau->new('ⓕⓞⓤⓡ'); |
| 46 |
print $tld->dld('ⓕⓤⓞⓡ'); |
| 47 |
# prints 1 |
| 48 |
|
| 49 |
CONSTRUCTOR |
| 50 |
new |
| 51 |
Creates and returns a "Text::Levenshtein::Damerau" object. Takes a |
| 52 |
scalar with the text (source) you want to compare against. |
| 53 |
|
| 54 |
my $tld = Text::Levenshtein::Damerau->new('Neil'); |
| 55 |
# Creates a new Text::Levenshtein::Damerau object $tld |
| 56 |
|
| 57 |
METHODS |
| 58 |
$tld->dld |
| 59 |
Scalar Argument: Takes a string to compare with. |
| 60 |
|
| 61 |
Returns: an integer representing the edit distance between the source |
| 62 |
and the passed argument. |
| 63 |
|
| 64 |
Hashref Argument: Takes a hashref containing: |
| 65 |
|
| 66 |
* list => \@array (array ref of strings to compare with) |
| 67 |
|
| 68 |
* *OPTIONAL* max_distance => $int (only return results with $int |
| 69 |
distance or less). |
| 70 |
|
| 71 |
* *OPTIONAL* backend => 'Some::Module::its_function' Any module that |
| 72 |
will take 2 arguments and returns an int. If the module fails to |
| 73 |
load, the function doesn't exist, or the function doesn't return a |
| 74 |
number when passed 2 strings, then "backend" remains unchanged. |
| 75 |
|
| 76 |
# Override defaults and use Text::Levenshtein::Damerau::PP's pp_edistance() |
| 77 |
$tld->dld({ list=> \@list, backend => 'Text::Levenshtein::Damerau::PP::pp_edistance'); |
| 78 |
|
| 79 |
# Override defaults and use Text::Levenshtein::Damerau::XS's xs_edistance() |
| 80 |
use Text::Levenshtein::Damerau; |
| 81 |
requires Text::Levenshtein::Damerau::XS; |
| 82 |
... |
| 83 |
$tld->dld({ list=> \@list, backend => 'Text::Levenshtein::Damerau::XS::xs_edistance'); |
| 84 |
|
| 85 |
Returns: hashref with each word from the passed list as keys, and their |
| 86 |
edit distance (if less than max_distance, which is unlimited by |
| 87 |
default). |
| 88 |
|
| 89 |
my $tld = Text::Levenshtein::Damerau->new('Neil'); |
| 90 |
print $tld->dld( 'Niel' ); |
| 91 |
# prints 1 |
| 92 |
|
| 93 |
#or if you want to check the distance of various items in a list |
| 94 |
|
| 95 |
my @names_list = ('Niel','Jack'); |
| 96 |
my $tld = Text::Levenshtein::Damerau->new('Neil'); |
| 97 |
my $d_ref = $tld->dld({ list=> \@names_list }); # pass a list, returns a hash ref |
| 98 |
print $d_ref->{'Niel'}; #prints 1 |
| 99 |
print $d_ref->{'Jack'}; #prints 4 |
| 100 |
|
| 101 |
$tld->dld_best_match |
| 102 |
Argument: an array reference of strings. |
| 103 |
|
| 104 |
Returns: the string with the smallest edit distance between the source |
| 105 |
and the array of strings passed. |
| 106 |
|
| 107 |
Takes distance of $tld source against every item in @targets, then |
| 108 |
returns the string of the best match. |
| 109 |
|
| 110 |
my $tld = Text::Levenshtein::Damerau->new('Neil'); |
| 111 |
my @name_spellings = ('Niel','Neell','KNiel'); |
| 112 |
print $tld->dld_best_match({ list=> \@name_spellings }); |
| 113 |
# prints Niel |
| 114 |
|
| 115 |
$tld->dld_best_distance |
| 116 |
Arguments: an array reference of strings. |
| 117 |
|
| 118 |
Returns: the smallest edit distance between the source and the array |
| 119 |
reference of strings passed. |
| 120 |
|
| 121 |
Takes distance of $tld source against every item in the passed array, |
| 122 |
then returns the smallest edit distance. |
| 123 |
|
| 124 |
my $tld = Text::Levenshtein::Damerau->new('Neil'); |
| 125 |
my @name_spellings = ('Niel','Neell','KNiel'); |
| 126 |
print $tld->dld_best_distance({ list => \@name_spellings }); |
| 127 |
# prints 1 |
| 128 |
|
| 129 |
EXPORTABLE METHODS |
| 130 |
edistance |
| 131 |
Arguments: source string and target string. |
| 132 |
|
| 133 |
* *OPTIONAL 3rd argument* int (max distance; only return results with |
| 134 |
$int distance or less). 0 = unlimited. Default = 0. |
| 135 |
|
| 136 |
Returns: int that represents the edit distance between the two argument. |
| 137 |
-1 if max distance is set and reached. |
| 138 |
|
| 139 |
Wrapper function to take the edit distance between a source and target |
| 140 |
string. It will attempt to use, in order: |
| 141 |
|
| 142 |
* Text::Levenshtein::Damerau::XS xs_edistance |
| 143 |
|
| 144 |
* Text::Levenshtein::Damerau::PP pp_edistance |
| 145 |
|
| 146 |
use Text::Levenshtein::Damerau qw/edistance/; |
| 147 |
print edistance('Neil','Niel'); |
| 148 |
# prints 1 |
| 149 |
|
| 150 |
SEE ALSO |
| 151 |
* <https://github.com/ugexe/Text--Levenshtein--Damerau> *repository* |
| 152 |
|
| 153 |
* <http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance> |
| 154 |
*damerau levenshtein explaination* |
| 155 |
|
| 156 |
* Text::Fuzzy *regular levenshtein distance* |
| 157 |
|
| 158 |
BUGS |
| 159 |
Please report bugs to: |
| 160 |
|
| 161 |
<https://rt.cpan.org/Public/Dist/Display.html?Name=Text-Levenshtein-Dame |
| 162 |
rau> |
| 163 |
|
| 164 |
AUTHOR |
| 165 |
Nick Logan <[email protected]> |
| 166 |
|
| 167 |
LICENSE AND COPYRIGHT |
| 168 |
This library is free software; you can redistribute it and/or modify it |
| 169 |
under the same terms as Perl itself. |
| 170 |
|