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