Text-Levenshtein-Damerau-0.41/0000755000175300017530000000000012156751752015223 5ustar nicklnicklText-Levenshtein-Damerau-0.41/MANIFEST.SKIP0000644000175300017530000000015312073717626017120 0ustar nicklnickl^.git/ blib/ ^.gitignore Build _build* pm_to_blib .tar.gz$ .zip$ ^Makefile$ MYMETA.* XS.c$ .o$ .bs$ .SKIP$ Text-Levenshtein-Damerau-0.41/LICENSE0000644000175300017530000004377112043122455016227 0ustar nicklnicklThis software is copyright (c) 2012 by Nick Logan (ugexe) >. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2012 by Nick Logan (ugexe) >. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2012 by Nick Logan (ugexe) >. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Text-Levenshtein-Damerau-0.41/META.json0000644000175300017530000000265612156751752016655 0ustar nicklnickl{ "abstract" : "Damerau Levenshtein edit distance.", "author" : [ "Nick Logan " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 6.66, CPAN::Meta::Converter version 2.120921", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Text-Levenshtein-Damerau", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "Test::More" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "List::Util" : "0", "perl" : "5.008008" } } }, "provides" : { "Text::Levenshtein::Damerau" : { "file" : "lib/Text/Levenshtein/Damerau.pm", "version" : "0.41" }, "Text::Levenshtein::Damerau::PP" : { "file" : "lib/Text/Levenshtein/Damerau/PP.pm", "version" : "0.25" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://rt.cpan.org/Public/Dist/Display.html?Name=Text-Levenshtein-Damerau" }, "repository" : { "url" : "https://github.com/ugexe/Text--Levenshtein--Damerau" } }, "version" : "0.41" } Text-Levenshtein-Damerau-0.41/README0000644000175300017530000001354712073450271016103 0ustar nicklnicklNAME Text::Levenshtein::Damerau - Damerau Levenshtein edit distance. SYNOPSIS use Text::Levenshtein::Damerau; 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/; print edistance('Neil','Niel'); # prints 1 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 Text::Levenshtein::Damerau::PP, but has an XS addon Text::Levenshtein::Damerau::XS for massive speed imrovements. Works correctly with utf8 if backend supports it; known to work with "Text::Levenshtein::Damerau::PP" and "Text::Levenshtein::Damerau::XS". use Text::Levenshtein::Damerau; use utf8; my $tld = Text::Levenshtein::Damerau->new('ⓕⓞⓤⓡ'); print $tld->dld('ⓕⓤⓞⓡ'); # prints 1 CONSTRUCTOR new Creates and returns a "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 METHODS $tld->dld Scalar Argument: Takes a string to compare with. Returns: an integer representing the edit distance between the source and the passed argument. Hashref Argument: Takes a hashref containing: * list => \@array (array ref of strings to compare with) * *OPTIONAL* max_distance => $int (only return results with $int distance or less). * *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 "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'); 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 $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 $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 EXPORTABLE METHODS edistance Arguments: source string and target string. * *OPTIONAL 3rd argument* int (max distance; only return results with $int distance or less). 0 = unlimited. Default = 0. 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: * Text::Levenshtein::Damerau::XS xs_edistance * Text::Levenshtein::Damerau::PP pp_edistance use Text::Levenshtein::Damerau qw/edistance/; print edistance('Neil','Niel'); # prints 1 SEE ALSO * *repository* * *damerau levenshtein explaination* * Text::Fuzzy *regular levenshtein distance* BUGS Please report bugs to: AUTHOR Nick Logan LICENSE AND COPYRIGHT This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Text-Levenshtein-Damerau-0.41/t/0000755000175300017530000000000012156751752015466 5ustar nicklnicklText-Levenshtein-Damerau-0.41/t/04_dld_best_distance.t0000644000175300017530000000112612060043504021567 0ustar nicklnickluse strict; use warnings; use Test::More tests => 2; use Text::Levenshtein::Damerau; my $tld = Text::Levenshtein::Damerau->new('four'); my @list = ('fourty','fuor','fourth','fourteen',''); is($tld->dld_best_distance({ list => \@list }), 1, 'test dld_best_distance'); #Test some utf8 use utf8; no warnings; # Work around for Perl 5.6 and setting output encoding my $tld_utf8 = Text::Levenshtein::Damerau->new('ⓕⓞⓤⓡ'); my @list_utf8 = ('ⓕⓤⓞⓡ','ⓕⓞⓤⓡⓣⓗ','ⓕⓧⓧⓡ',''); is($tld_utf8->dld_best_distance({ list => \@list_utf8 }), 1, 'test dld_best_distance (utf8)'); Text-Levenshtein-Damerau-0.41/t/02_pp_edistance.t0000644000175300017530000000404712147217656020617 0ustar nicklnickluse strict; use warnings; use Test::More tests => 23; use Text::Levenshtein::Damerau::PP qw/pp_edistance/; # We test pp_edistance before edistance because edistance might be using ::XS backend and fail is( pp_edistance('four','four'), 0, 'test pp_edistance matching'); is( pp_edistance('four','for'), 1, 'test pp_edistance insertion'); is( pp_edistance('four','fourth'), 2, 'test pp_edistance deletion'); is( pp_edistance('four','fuor'), 1, 'test pp_edistance transposition'); is( pp_edistance('four','fxxr'), 2, 'test pp_edistance substitution'); is( pp_edistance('four','FOuR'), 3, 'test pp_edistance case'); is( pp_edistance('four',''), 4, 'test pp_edistance target empty'); is( pp_edistance('','four'), 4, 'test pp_edistance source empty'); is( pp_edistance('',''), 0, 'test pp_edistance source & target empty'); is( pp_edistance('11','1'), 1, 'test pp_edistance numbers'); is( pp_edistance('xxx','x',1), -1, 'test pp_edistance > max distance setting'); is( pp_edistance('xxx','xx',1), 1, 'test pp_edistance <= max distance setting'); # some extra maxDistance tests is( pp_edistance("xxx","xxxx",1), 1, 'test xs_edistance misc 1'); is( pp_edistance("xxx","xxxx",2), 1, 'test xs_edistance misc 2'); is( pp_edistance("xxx","xxxx",3), 1, 'test xs_edistance misc 3'); is( pp_edistance("xxxx","xxx",1), 1, 'test xs_edistance misc 4'); is( pp_edistance("xxxx","xxx",2), 1, 'test xs_edistance misc 5'); is( pp_edistance("xxxx","xxx",3), 1, 'test xs_edistance misc 6'); # Test some utf8 use utf8; no warnings; # Work around for Perl 5.6 and setting output encoding is( pp_edistance('ⓕⓞⓤⓡ','ⓕⓞⓤⓡ'), 0, 'test pp_edistance matching (utf8)'); is( pp_edistance('ⓕⓞⓤⓡ','ⓕⓞⓡ'), 1, 'test pp_edistance insertion (utf8)'); is( pp_edistance('ⓕⓞⓤⓡ','ⓕⓞⓤⓡⓣⓗ'), 2, 'test pp_edistance deletion (utf8)'); is( pp_edistance('ⓕⓞⓤⓡ','ⓕⓤⓞⓡ'), 1, 'test pp_edistance transposition (utf8)'); is( pp_edistance('ⓕⓞⓤⓡ','ⓕⓧⓧⓡ'), 2, 'test pp_edistance substitution (utf8)'); Text-Levenshtein-Damerau-0.41/t/01_imports.t0000644000175300017530000000027012042453324017633 0ustar nicklnickluse strict; use warnings; use Test::More tests => 2; BEGIN { use_ok 'Text::Levenshtein::Damerau', qw/edistance/ } BEGIN { use_ok 'Text::Levenshtein::Damerau::PP', qw/pp_edistance/ } Text-Levenshtein-Damerau-0.41/t/05_dld_best_match.t0000644000175300017530000000114112060043520021065 0ustar nicklnickluse strict; use warnings; use Test::More tests => 2; use Text::Levenshtein::Damerau; my $tld = Text::Levenshtein::Damerau->new('four'); my @list = ('fourty','fuor','fourth','fourteen',''); is($tld->dld_best_match({ list => \@list }), 'fuor', 'test dld_best_distance'); #Test some utf8 use utf8; no warnings; # Work around for Perl 5.6 and setting output encoding my $tld_utf8 = Text::Levenshtein::Damerau->new('ⓕⓞⓤⓡ'); my @list_utf8 = ('ⓕⓤⓞⓡ','ⓕⓞⓤⓡⓣⓗ','ⓕⓧⓧⓡ',''); is($tld_utf8->dld_best_match({ list => \@list_utf8 }), 'ⓕⓤⓞⓡ', 'test dld_best_distance (utf8)'); Text-Levenshtein-Damerau-0.41/t/03_dld.t0000644000175300017530000000277312060043473016715 0ustar nicklnickluse strict; use warnings; use Test::More tests => 14; use Text::Levenshtein::Damerau; my $tld = Text::Levenshtein::Damerau->new('four'); #Test scalar argument is( $tld->dld('four'), 0, 'test helper matching'); is( $tld->dld('for'), 1, 'test helper insertion'); is( $tld->dld('fourth'), 2, 'test helper deletion'); is( $tld->dld('fuor'), 1, 'test helper transposition'); is( $tld->dld('fxxr'), 2, 'test helper substitution'); is( $tld->dld('FOuR'), 3, 'test helper case'); is( $tld->dld(''), 4, 'test helper empty'); #Test array reference argument my @list = ('four','fourty','fourteen',''); is_deeply($tld->dld({ list => \@list }), { four => 0, fourty => 2, fourteen => 4, '', => 4 }, 'test dld(\@array_ref)'); #Test some utf8 use utf8; no warnings; # Work around for Perl 5.6 and setting output encoding my $tld_utf8 = Text::Levenshtein::Damerau->new('ⓕⓞⓤⓡ'); #Test utf8 scalar argument is( $tld_utf8->dld('ⓕⓞⓤⓡ'), 0, 'test helper matching (utf8)'); is( $tld_utf8->dld('ⓕⓞⓡ'), 1, 'test helper insertion (utf8)'); is( $tld_utf8->dld('ⓕⓞⓤⓡⓣⓗ'), 2, 'test helper deletion (utf8)'); is( $tld_utf8->dld('ⓕⓤⓞⓡ'), 1, 'test helper transposition (utf8)'); is( $tld_utf8->dld('ⓕⓧⓧⓡ'), 2, 'test helper substitution (utf8)'); #Test utf8 array reference argument my @list_utf8 = ('ⓕⓞⓤⓡ','ⓕⓞⓡ','ⓕⓤⓞⓡ',''); is_deeply($tld_utf8->dld({ list => \@list_utf8 }), { 'ⓕⓞⓤⓡ' => 0, 'ⓕⓞⓡ' => 1, 'ⓕⓤⓞⓡ' => 1, '' => 4 }, 'test dld(\@array_ref) (utf8)'); Text-Levenshtein-Damerau-0.41/t/00_compile.t0000644000175300017530000000022712042453315017567 0ustar nicklnickluse strict; use warnings; use Test::More tests => 2; BEGIN { use_ok 'Text::Levenshtein::Damerau' } BEGIN { use_ok 'Text::Levenshtein::Damerau::PP' } Text-Levenshtein-Damerau-0.41/lib/0000755000175300017530000000000012156751752015771 5ustar nicklnicklText-Levenshtein-Damerau-0.41/lib/Text/0000755000175300017530000000000012156751752016715 5ustar nicklnicklText-Levenshtein-Damerau-0.41/lib/Text/Levenshtein/0000755000175300017530000000000012156751752021201 5ustar nicklnicklText-Levenshtein-Damerau-0.41/lib/Text/Levenshtein/Damerau.pm0000644000175300017530000001642112156750315023113 0ustar nicklnicklpackage 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, but has an XS addon L for massive speed imrovements. Works correctly with utf8 if backend supports it; known to work with C and C. use utf8; my $tld = Text::Levenshtein::Damerau->new('ⓕⓞⓤⓡ'); print $tld->dld('ⓕⓤⓞⓡ'); # prints 1 =head1 CONSTRUCTOR =head2 new Creates and returns a C 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 Argument: Takes a string to compare with. Returns: an integer representing the edit distance between the source and the passed argument. B Argument: Takes a hashref containing: =over 4 =item * list => \@array (array ref of strings to compare with) =item * I max_distance => $int (only return results with $int distance or less). =item * I 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 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 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 B =item * L B =back use Text::Levenshtein::Damerau qw/edistance/; print edistance('Neil','Niel'); # prints 1 =head1 SEE ALSO =over 4 =item * L I =item * L I =item * L I =back =head1 BUGS Please report bugs to: L =head1 AUTHOR Nick Logan > =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 Text-Levenshtein-Damerau-0.41/lib/Text/Levenshtein/Damerau/0000755000175300017530000000000012156751752022557 5ustar nicklnicklText-Levenshtein-Damerau-0.41/lib/Text/Levenshtein/Damerau/PP.pm0000644000175300017530000001105712156751740023435 0ustar nicklnicklpackage Text::Levenshtein::Damerau::PP; use 5.008_008; # for utf8, sorry legacy Perls use strict; use List::Util qw/min/; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw/pp_edistance/; our $VERSION = '0.25'; sub pp_edistance { # Does the actual calculation on a pair of strings my ( $source, $target, $max_distance ) = @_; $max_distance = int($max_distance || 0); my $source_length = length($source) || 0; my $target_length = length($target) || 0; return ($source_length?$source_length:$target_length) if(!$target_length || !$source_length); my $lengths_max = $source_length + $target_length; my $dictionary_count; #create dictionary to keep character count my $swap_count; my $swap_score; my $target_char_count; my $source_index; my $target_index; my @scores; # init values outside of work loops $scores[0][0] = $scores[1][0] = $scores[0][1] = $lengths_max; $scores[1][1] = 0; # Work Loops foreach $source_index ( 1 .. $source_length ) { $swap_count = 0; $dictionary_count->{ substr( $source, $source_index - 1, 1 ) } = 0; $scores[ $source_index + 1 ][1] = $source_index; $scores[ $source_index + 1 ][0] = $lengths_max; foreach $target_index ( 1 .. $target_length ) { if ( $source_index == 1 ) { $dictionary_count->{ substr( $target, $target_index - 1, 1 ) } = 0; $scores[1][ $target_index + 1 ] = $target_index; $scores[0][ $target_index + 1 ] = $lengths_max; } $target_char_count = $dictionary_count->{ substr( $target, $target_index - 1, 1 ) }; $swap_score = $scores[$target_char_count][$swap_count] + ( $source_index - $target_char_count - 1 ) + 1 + ( $target_index - $swap_count - 1 ); if ( substr( $source, $source_index - 1, 1 ) ne substr( $target, $target_index - 1, 1 ) ) { $scores[ $source_index + 1 ][ $target_index + 1 ] = min( $scores[$source_index][$target_index]+1, $scores[ $source_index + 1 ][$target_index]+1, $scores[$source_index][ $target_index + 1 ]+1, $swap_score ); } else { $swap_count = $target_index; $scores[ $source_index + 1 ][ $target_index + 1 ] = min( $scores[$source_index][$target_index], $swap_score ); } } #unless ( $max_distance == 0 || $max_distance >= $scores[ $source_index + 1 ][ $target_length + 1 ] ) #{ # return -1; #} $dictionary_count->{ substr( $source, $source_index - 1, 1 ) } = $source_index; } my $score = $scores[$source_length+1][$target_length+1]; return ($max_distance != 0 && $max_distance < $score)?(-1):$score; } 1; __END__ =encoding utf8 =head1 NAME Text::Levenshtein::Damerau::PP - Pure Perl Damerau Levenshtein edit distance. =head1 SYNOPSIS use Text::Levenshtein::Damerau::PP qw/pp_edistance/; print pp_edistance('Neil','Niel'); # prints 1 =head1 DESCRIPTION Returns the true Damerau Levenshtein edit distance of strings with adjacent transpositions. Pure Perl implementation. Works correctly with utf8. use Text::Levenshtein::Damerau::PP qw/pp_edistance/; use utf8; pp_edistance('ⓕⓞⓤⓡ','ⓕⓤⓞⓡ'), # prints 1 =head1 METHODS =head2 pp_edistance Arguments: source string and target string. =over =item * I 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. Stops calculations and returns -1 if max distance is set and reached. Function to take the edit distance between a source and target string. Contains the actual algorithm implementation. use Text::Levenshtein::Damerau::PP qw/pp_edistance/; print pp_edistance('Neil','Niel'); # prints 1 print pp_edistance('Neil','Nielx',1); # prints -1 =head1 SEE ALSO =over 4 =item * L =item * L =back =head1 BUGS Please report bugs to: L =head1 AUTHOR Nick Logan > =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 Text-Levenshtein-Damerau-0.41/Changes0000644000175300017530000000677712156750330016526 0ustar nicklnicklRevision history for Perl module Text::Levenshtein::Damerau 0.41 Fri Jun 22:24:03 - Exporter 'import' 5.6 hack removed - List::Util min check removed (used full time) - ::PP max_distance hack-fixed like XS 0.38 Wed Jan 16 14:52:44 2013 - Variable name clarifications - ::PP refactoring/optimizations - Declaration changes 0.37 Thu Jan 10 00:18:41 2013 - Optimizations in ::PP 0.36 Thu Dec 13 00:45:41 - MIN_PERL_VERSION added back to Makefile using different number format 0.35 Sat Dec 08 23:40:10 2012 - Support for Perls earlier than 5.8 dropped (ha!) 0.34 Thu Dec 06 02:18:37 2012 - another utf8 update for 5.6 0.33 Sun Nov 30 10:53:13 2012 - utf8 changes for a Perl 5.6 test failure 0.32 Sun Nov 25 12:31:42 2012 - Small optimization in backend selection - Exporter import error in older Exporter/Perl fixed 0.31 Sun Nov 18 18:14:31 2012 - Moved pod tests to /xt - Doc updates 0.30 Wed Nov 13 17:34:19 2012 - max distance added to stop calculations at a certain distance 0.29 Sun Oct 28 02:08:19 2012 - Hardcoded Makefile.PL values 0.28 Sat Oct 27 20:33:14 2012 - Document updates 0.27 Sat Oct 27 20:33:14 2012 - Modified _set_backend to test passed module::function for valid return value, ignoring the change request on failure. 0.26 Sat Oct 27 18:29:57 2012 - Fixed pod utf8 stuff 0.25 Sat Oct 27 15:58:22 2012 - Document updates 0.24 Sat Oct 27 02:40:38 2012 - Removed List::Util from ::PP - Fixed dld_best_match so it correctly passed the entire argument hash ref - Allow changing of backend at run time via dld - Document updates 0.23 Fri Oct 26 17:39:51 2012 - Moved edistance to ::PP sub module. Adding a ::XS distro engine to do the heavy lifting of edistance in C if available. - Lots of refactoring 0.19 Thu Oct 25 00:02:39 2012 - Test consolidation; All tests use Test::More, removed Test::Base tests - Passes use strict by checking dld if its a reference - Perl::Tidy'd - Pure perl functionality finalized, working on Inline::C backend to do heavy lifting 0.18 Tues Oct 23 23:47:03 2012 - MORE TESTS - fixed error with dld_best_distance - I'm sure CPAN authors everywhere are laughing at the number of releases per day :) 0.17 Tues Oct 23 21:23:17 2012 - Documentation clarifications 0.16 Tues Oct 23 19:23:30 2012 - Documentation updates - Argument/return changes 0.15 Tues Oct 23 13:20:42 2012 - Documentation updates 0.14 Tues Oct 23 01:51:29 2012 - Fixed documentation formatting ruining source - Sorry CPAN! :( 0.12 Mon Oct 22 23:28:40 2012 - Documentation updates - Returned exportable edistance() function 0.11 Mon Oct 22 19:15:12 2012 - Completely recoded to be more OO - Added ability to retrieve best match from a list - Added github repository to metadata - Added: utf8 tests, CaSE tests, pod_coverage tests - Added List::Util back 0.09 Sun Oct 21 19:52:16 2012 - Fixed file creation time error with cpan installation 0.08 Sun Oct 21 18:54:48 2012 - Documentation fix - Removed List::Util in favor of local min subroutine - return 0 without doing calculations if strings match 0.07 Sun Oct 21 14:35:11 2012 - use utf8; - Added more tests - Makefile updated: require List::Util, test_requires Test::More 0.06 Sat Oct 20 16:40:19 2012 - Added function dld (mirror of edistance) for clarity - Modified documentation - Added tests using Test::Base and Test::More - Switched to Module::Install 0.05 Sat Sep 20 19:32:28 2012 - Corrected directory structure to install with cpanm 0.04 Sat Jun 20 14:34:43 2012 - original version; created by ExtUtils::ModuleMaker Text-Levenshtein-Damerau-0.41/Makefile.PL0000644000175300017530000000221712156750466017200 0ustar nicklnickluse 5.008_008; # for utf8, sorry legacy Perls use strict; use warnings; use ExtUtils::MakeMaker; my $pm = 'lib/Text/Levenshtein/Damerau.pm'; my $repo = 'https://github.com/ugexe/Text--Levenshtein--Damerau'; my $bugs = 'https://rt.cpan.org/Public/Dist/Display.html?Name=Text-Levenshtein-Damerau'; WriteMakefile ( NAME => 'Text::Levenshtein::Damerau', VERSION_FROM => $pm, ABSTRACT_FROM => $pm, AUTHOR => 'Nick Logan ', LICENSE => 'perl', MIN_PERL_VERSION => '5.008008', PREREQ_PM => { 'List::Util' => 0, }, BUILD_REQUIRES => { 'Test::More' => 0, }, META_MERGE => { resources => { repository => $repo, bugtracker => "$bugs", }, provides => { 'Text::Levenshtein::Damerau' => { file => 'lib/Text/Levenshtein/Damerau.pm', version => '0.41', }, 'Text::Levenshtein::Damerau::PP' => { file => 'lib/Text/Levenshtein/Damerau/PP.pm', version => '0.25', }, }, }, depend => { '$(FIRST_MAKEFILE)' => '$(VERSION_FROM)' }, );Text-Levenshtein-Damerau-0.41/xt/0000755000175300017530000000000012156751752015656 5ustar nicklnicklText-Levenshtein-Damerau-0.41/xt/pod_coverage.t0000644000175300017530000000027312052260146020465 0ustar nicklnickluse strict; use warnings; use Test::More; eval "use Test::Pod::Coverage 1.00"; plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; all_pod_coverage_ok();Text-Levenshtein-Damerau-0.41/xt/pod.t0000644000175300017530000000023312052260177016612 0ustar nicklnickluse strict; use warnings; use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok();Text-Levenshtein-Damerau-0.41/MANIFEST0000644000175300017530000000070112156751752016352 0ustar nicklnicklChanges lib/Text/Levenshtein/Damerau.pm lib/Text/Levenshtein/Damerau/PP.pm LICENSE Makefile.PL MANIFEST This list of files MANIFEST.SKIP t/00_compile.t t/01_imports.t t/02_pp_edistance.t t/03_dld.t t/04_dld_best_distance.t t/05_dld_best_match.t xt/pod.t xt/pod_coverage.t README META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Text-Levenshtein-Damerau-0.41/META.yml0000644000175300017530000000155012156751752016475 0ustar nicklnickl--- abstract: 'Damerau Levenshtein edit distance.' author: - 'Nick Logan ' build_requires: Test::More: 0 configure_requires: ExtUtils::MakeMaker: 0 dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 6.66, CPAN::Meta::Converter version 2.120921' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Text-Levenshtein-Damerau no_index: directory: - t - inc provides: Text::Levenshtein::Damerau: file: lib/Text/Levenshtein/Damerau.pm version: 0.41 Text::Levenshtein::Damerau::PP: file: lib/Text/Levenshtein/Damerau/PP.pm version: 0.25 requires: List::Util: 0 perl: 5.008008 resources: bugtracker: https://rt.cpan.org/Public/Dist/Display.html?Name=Text-Levenshtein-Damerau repository: https://github.com/ugexe/Text--Levenshtein--Damerau version: 0.41