| 1 | package Text::Levenshtein::Damerau; | ||||||||||||||
| 2 | 5 | ▶ | use 5.008_008; # for utf8, sorry legacy Perls | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 5 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 5 | ▶ | use List::Util qw/reduce/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 5 | require Exporter; | |||||||||||||
| 7 | |||||||||||||||
| 8 | 5 | our @ISA = qw(Exporter); | |||||||||||||
| 9 | 5 | our @EXPORT_OK = qw/edistance/; | |||||||||||||
| 10 | 5 | our $VERSION = '0.41'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | # To XS or not to XS... | ||||||||||||||
| 13 | 5 | ▶ | unless ( _set_backend('Text::Levenshtein::Damerau::XS::xs_edistance') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 14 | 5 | _set_backend('Text::Levenshtein::Damerau::PP::pp_edistance'); | |||||||||||||
| 15 | } | ||||||||||||||
| 16 | |||||||||||||||
| 17 | 6 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 18 | 6 | my $class = shift; | |||||||||||||
| 19 | 6 | my $self = {}; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 6 | $self->{'source'} = shift; | |||||||||||||
| 22 | 6 | $self->{'max_distance'} = 0; | |||||||||||||
| 23 | 6 | bless( $self, $class ); | |||||||||||||
| 24 | |||||||||||||||
| 25 | 6 | return $self; | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 20 | ▶ | sub dld { | ||||||||||||
|
Subroutine
dld: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||
| 29 | 20 | my $self = shift; | |||||||||||||
| 30 | 20 | my $args = shift; | |||||||||||||
| 31 | |||||||||||||||
| 32 | 20 | ▶ | if ( !ref $args ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 33 | 14 | return edistance( $self->{'source'}, $args, ); | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | elsif ( ref $args->{'list'} eq ref [] ) { | ||||||||||||||
| 36 | 6 | my $target_score; | |||||||||||||
| 37 | |||||||||||||||
| 38 | 6 | ▶ | if ( defined( $args->{'backend'} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | 0 | _set_backend( $args->{'backend'} ); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 6 | foreach my $target ( @{ $args->{'list'} } ) { | |||||||||||||
| 43 | my $ed = | ||||||||||||||
| 44 | 26 | edistance( $self->{'source'}, $target, $args->{max_distance} ); | |||||||||||||
| 45 | 26 | ▶ | $target_score->{$target} = $ed if ( $ed >= 0 ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 6 | return $target_score; | |||||||||||||
| 49 | |||||||||||||||
| 50 | } | ||||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 4 | ▶ | sub dld_best_match { | ||||||||||||
|
Subroutine
dld_best_match: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 54 | 4 | my $self = shift; | |||||||||||||
| 55 | 4 | my $args = shift; | |||||||||||||
| 56 | |||||||||||||||
| 57 | 4 | ▶ | if ( defined( $args->{'list'} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 4 | my $hash_ref = $self->dld($args); | |||||||||||||
| 59 | |||||||||||||||
| 60 | #Get the hashref key with the smallest value | ||||||||||||||
| 61 | 14 | ▶ | return reduce { $hash_ref->{$a} < $hash_ref->{$b} ? $a : $b } | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||
| 62 | 4 | keys %{$hash_ref}; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 2 | ▶ | sub dld_best_distance { | ||||||||||||
|
Subroutine
dld_best_distance: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 67 | 2 | my $self = shift; | |||||||||||||
| 68 | 2 | my $args = shift; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 2 | ▶ | if ( defined( $args->{'list'} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | 2 | return $self->dld( $self->dld_best_match($args) ); | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 10 | ▶ | sub _set_backend { | ||||||||||||
|
Subroutine
_set_backend: called CC=3 SCAR=11.0
| |||||||||||||||
| 76 | 10 | my $be = my $mod = shift; | |||||||||||||
| 77 | 10 | $mod =~ s/^(.*)::.*?$/$1/; | |||||||||||||
| 78 | |||||||||||||||
| 79 | 10 | local $@; | |||||||||||||
| 80 | 10 | eval "require $mod"; | |||||||||||||
| 81 | 10 | ▶ | unless ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 5 | eval "defined &$be"; | |||||||||||||
| 83 | 5 | ▶ | unless($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | # We welcome our new edistance overlord | ||||||||||||||
| 85 | 5 | *edistance = \&$be; | |||||||||||||
| 86 | 5 | return 1; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 5 | return 0; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 5 | 1; | |||||||||||||
| 94 | |||||||||||||||
| 95 | __END__ | ||||||||||||||