| 1 | package Text::Levenshtein::Damerau::PPscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/; |
| 2 | use 5.008_008scip-perl cpan Text-Levenshtein-Damerau 0.41 `5.008_008`/; # for utf8, sorry legacy Perls |
| 3 | use strictscip-perl cpan Text-Levenshtein-Damerau 0.41 strict/; |
| 4 | use List::Utilscip-perl cpan Text-Levenshtein-Damerau 0.41 List/Util/ qw/min/; |
| 5 | requirescip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/require(). Exporter; |
| 6 | |
| 7 | our @ISAscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/`@ISA`. = qw(Exporter); |
| 8 | our @EXPORT_OKscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/`@EXPORT_OK`. = qw/pp_edistance/; |
| 9 | our $VERSIONscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/$VERSION. = '0.25'; |
| 10 | |
| 11 | sub pp_edistancescip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/pp_edistance(). { |
| 12 | # Does the actual calculation on a pair of strings |
| 13 | my ( $source, $target, $max_distance ) = @_scip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/`@_`.; |
| 14 | $max_distance = intscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/int().($max_distance || 0); |
| 15 | |
| 16 | my $source_length = lengthscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/length().($source) || 0; |
| 17 | my $target_length = lengthscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/length().($target) || 0; |
| 18 | return ($source_length?$source_length:$target_length) if(!$target_length || !$source_length); |
| 19 | |
| 20 | my $lengths_max = $source_length + $target_length; |
| 21 | my $dictionary_count; #create dictionary to keep character count |
| 22 | my $swap_count; |
| 23 | my $swap_score; |
| 24 | my $target_char_count; |
| 25 | my $source_index; |
| 26 | my $target_index; |
| 27 | my @scores; |
| 28 | |
| 29 | # init values outside of work loops |
| 30 | $scores[0][0] = $scores[1][0] = $scores[0][1] = $lengths_max; |
| 31 | $scores[1][1] = 0; |
| 32 | |
| 33 | # Work Loops |
| 34 | foreach $source_index ( 1 .. $source_length ) { |
| 35 | $swap_count = 0; |
| 36 | $dictionary_count->{ substrscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/substr().( $source, $source_index - 1, 1 ) } = 0; |
| 37 | $scores[ $source_index + 1 ][1] = $source_index; |
| 38 | $scores[ $source_index + 1 ][0] = $lengths_max; |
| 39 | |
| 40 | foreach $target_index ( 1 .. $target_length ) { |
| 41 | if ( $source_index == 1 ) { |
| 42 | $dictionary_count->{ substrscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/substr().( $target, $target_index - 1, 1 ) } = 0; |
| 43 | $scores[1][ $target_index + 1 ] = $target_index; |
| 44 | $scores[0][ $target_index + 1 ] = $lengths_max; |
| 45 | } |
| 46 | |
| 47 | $target_char_count = |
| 48 | $dictionary_count->{ substrscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/substr().( $target, $target_index - 1, 1 ) }; |
| 49 | $swap_score = $scores[$target_char_count][$swap_count] + |
| 50 | ( $source_index - $target_char_count - 1 ) + 1 + |
| 51 | ( $target_index - $swap_count - 1 ); |
| 52 | |
| 53 | if ( |
| 54 | substrscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/substr().( $source, $source_index - 1, 1 ) ne |
| 55 | substrscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/substr().( $target, $target_index - 1, 1 ) ) |
| 56 | { |
| 57 | $scores[ $source_index + 1 ][ $target_index + 1 ] = minscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/min().( |
| 58 | $scores[$source_index][$target_index]+1, |
| 59 | $scores[ $source_index + 1 ][$target_index]+1, |
| 60 | $scores[$source_index][ $target_index + 1 ]+1, |
| 61 | $swap_score |
| 62 | ); |
| 63 | } |
| 64 | else { |
| 65 | $swap_count = $target_index; |
| 66 | |
| 67 | $scores[ $source_index + 1 ][ $target_index + 1 ] = minscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/min().( |
| 68 | $scores[$source_index][$target_index], $swap_score |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #unless ( $max_distance == 0 || $max_distance >= $scores[ $source_index + 1 ][ $target_length + 1 ] ) |
| 74 | #{ |
| 75 | # return -1; |
| 76 | #} |
| 77 | |
| 78 | $dictionary_count->{ substrscip-perl cpan Text-Levenshtein-Damerau 0.41 Text/Levenshtein/Damerau/PP/substr().( $source, $source_index - 1, 1 ) } = |
| 79 | $source_index; |
| 80 | } |
| 81 | |
| 82 | my $score = $scores[$source_length+1][$target_length+1]; |
| 83 | return ($max_distance != 0 && $max_distance < $score)?(-1):$score; |
| 84 | } |
| 85 | |
| 86 | 1; |
| 87 | |
| 88 | __END__ |
| 89 | |
| 90 | =encoding utf8 |
| 91 | |
| 92 | =head1 NAME |
| 93 | |
| 94 | Text::Levenshtein::Damerau::PP - Pure Perl Damerau Levenshtein edit distance. |
| 95 | |
| 96 | =head1 SYNOPSIS |
| 97 | |
| 98 | use Text::Levenshtein::Damerau::PP qw/pp_edistance/; |
| 99 | |
| 100 | print pp_edistance('Neil','Niel'); |
| 101 | # prints 1 |
| 102 | |
| 103 | =head1 DESCRIPTION |
| 104 | |
| 105 | Returns the true Damerau Levenshtein edit distance of strings with adjacent transpositions. Pure Perl implementation. Works correctly with utf8. |
| 106 | |
| 107 | use Text::Levenshtein::Damerau::PP qw/pp_edistance/; |
| 108 | use utf8; |
| 109 | |
| 110 | pp_edistance('ⓕⓞⓤⓡ','ⓕⓤⓞⓡ'), |
| 111 | # prints 1 |
| 112 | |
| 113 | =head1 METHODS |
| 114 | |
| 115 | =head2 pp_edistance |
| 116 | |
| 117 | Arguments: source string and target string. |
| 118 | |
| 119 | =over |
| 120 | |
| 121 | =item * I<OPTIONAL 3rd argument> int (max distance; only return results with $int distance or less). 0 = unlimited. Default = 0. |
| 122 | |
| 123 | =back |
| 124 | |
| 125 | Returns: int that represents the edit distance between the two argument. Stops calculations and returns -1 if max distance is set and reached. |
| 126 | |
| 127 | Function to take the edit distance between a source and target string. Contains the actual algorithm implementation. |
| 128 | |
| 129 | use Text::Levenshtein::Damerau::PP qw/pp_edistance/; |
| 130 | print pp_edistance('Neil','Niel'); |
| 131 | # prints 1 |
| 132 | |
| 133 | print pp_edistance('Neil','Nielx',1); |
| 134 | # prints -1 |
| 135 | |
| 136 | |
| 137 | =head1 SEE ALSO |
| 138 | |
| 139 | =over 4 |
| 140 | |
| 141 | =item * L<Text::Levenshtein::Damerau> |
| 142 | |
| 143 | =item * L<Text::Levenshtein::Damerau::XS> |
| 144 | |
| 145 | =back |
| 146 | |
| 147 | =head1 BUGS |
| 148 | |
| 149 | Please report bugs to: |
| 150 | |
| 151 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Text-Levenshtein-Damerau> |
| 152 | |
| 153 | =head1 AUTHOR |
| 154 | |
| 155 | Nick Logan <F<[email protected]>> |
| 156 | |
| 157 | =head1 LICENSE AND COPYRIGHT |
| 158 | |
| 159 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
| 160 | |
| 161 | =cut |
| 162 | |
| 163 |