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