| 1 | package Text::Levenshtein::Damerau::PP; | ||||||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use 5.008_008; # for utf8, sorry legacy Perls | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use List::Util qw/min/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 6 | require Exporter; | |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 6 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||
| 8 | 6 | our @EXPORT_OK = qw/pp_edistance/; | |||||||||||||||||||||||||||||||
| 9 | 6 | our $VERSION = '0.25'; | |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 63 | ▶ | sub pp_edistance { | ||||||||||||||||||||||||||||||
|
Subroutine
pp_edistance: called CC=13 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 12 | # Does the actual calculation on a pair of strings | ||||||||||||||||||||||||||||||||
| 13 | 63 | my ( $source, $target, $max_distance ) = @_; | |||||||||||||||||||||||||||||||
| 14 | 63 | ▶ | $max_distance = int($max_distance || 0); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$max_distance || 0
MC/DC100%1/1
$max_distance || 0
$max_distance
Truth table100%2/2
A
A= $max_distance
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 63 | ▶ | my $source_length = length($source) || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
length $source || 0
MC/DC100%1/1
length $source || 0
length $source
Truth table100%2/2
A
A= length $source
| |||||||||||||||||||||||||||||||||
| 17 | 63 | ▶ | my $target_length = length($target) || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
length $target || 0
MC/DC100%1/1
length $target || 0
length $target
Truth table100%2/2
A
A= length $target
| |||||||||||||||||||||||||||||||||
| 18 | 63 | ▶ | return ($source_length?$source_length:$target_length) if(!$target_length || !$source_length); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$target_length and $source_length
MC/DC100%2/2
$target_length and $source_length
$target_length
$source_length
Truth table100%3/3
A and B
A= $target_lengthB= $source_length
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 53 | my $lengths_max = $source_length + $target_length; | |||||||||||||||||||||||||||||||
| 21 | 53 | my $dictionary_count; #create dictionary to keep character count | |||||||||||||||||||||||||||||||
| 22 | my $swap_count; | ||||||||||||||||||||||||||||||||
| 23 | 53 | my $swap_score; | |||||||||||||||||||||||||||||||
| 24 | 53 | my $target_char_count; | |||||||||||||||||||||||||||||||
| 25 | 53 | my $source_index; | |||||||||||||||||||||||||||||||
| 26 | 53 | my $target_index; | |||||||||||||||||||||||||||||||
| 27 | 53 | my @scores; | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # init values outside of work loops | ||||||||||||||||||||||||||||||||
| 30 | 53 | $scores[0][0] = $scores[1][0] = $scores[0][1] = $lengths_max; | |||||||||||||||||||||||||||||||
| 31 | 53 | $scores[1][1] = 0; | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | # Work Loops | ||||||||||||||||||||||||||||||||
| 34 | 53 | foreach $source_index ( 1 .. $source_length ) { | |||||||||||||||||||||||||||||||
| 35 | 205 | $swap_count = 0; | |||||||||||||||||||||||||||||||
| 36 | 205 | $dictionary_count->{ substr( $source, $source_index - 1, 1 ) } = 0; | |||||||||||||||||||||||||||||||
| 37 | 205 | $scores[ $source_index + 1 ][1] = $source_index; | |||||||||||||||||||||||||||||||
| 38 | 205 | $scores[ $source_index + 1 ][0] = $lengths_max; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 205 | foreach $target_index ( 1 .. $target_length ) { | |||||||||||||||||||||||||||||||
| 41 | 903 | ▶ | if ( $source_index == 1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 230 | $dictionary_count->{ substr( $target, $target_index - 1, 1 ) } = 0; | |||||||||||||||||||||||||||||||
| 43 | 230 | $scores[1][ $target_index + 1 ] = $target_index; | |||||||||||||||||||||||||||||||
| 44 | 230 | $scores[0][ $target_index + 1 ] = $lengths_max; | |||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | $target_char_count = | ||||||||||||||||||||||||||||||||
| 48 | 903 | $dictionary_count->{ substr( $target, $target_index - 1, 1 ) }; | |||||||||||||||||||||||||||||||
| 49 | 903 | $swap_score = $scores[$target_char_count][$swap_count] + | |||||||||||||||||||||||||||||||
| 50 | ( $source_index - $target_char_count - 1 ) + 1 + | ||||||||||||||||||||||||||||||||
| 51 | ( $target_index - $swap_count - 1 ); | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 903 | ▶ | if ( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | substr( $source, $source_index - 1, 1 ) ne | ||||||||||||||||||||||||||||||||
| 55 | substr( $target, $target_index - 1, 1 ) ) | ||||||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||||||
| 57 | 667 | $scores[ $source_index + 1 ][ $target_index + 1 ] = 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 | 236 | $swap_count = $target_index; | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 236 | $scores[ $source_index + 1 ][ $target_index + 1 ] = 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 | 205 | $dictionary_count->{ substr( $source, $source_index - 1, 1 ) } = | |||||||||||||||||||||||||||||||
| 79 | $source_index; | ||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 53 | my $score = $scores[$source_length+1][$target_length+1]; | |||||||||||||||||||||||||||||||
| 83 | 53 | ▶ | return ($max_distance != 0 && $max_distance < $score)?(-1):$score; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$max_distance != 0 and $max_distance < $score
MC/DC100%2/2
$max_distance != 0 and $max_distance < $score
$max_distance != 0
$max_distance < $score
Truth table100%3/3
A and B
A= $max_distance != 0B= $max_distance < $score
| |||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 6 | 1; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||||||