Phonetic.pm

filter: Statementstmt 92.0% 92 / 100 Branchbran 78.8% 41 / 52 Conditioncond 62.9% 17 / 27 MC/DCmcdc 38.8% 7 / 18 Subroutinesub 88.8% 16 / 18 Podpod 66.6% 4 / 6 totaltotal 80.0% 177 / 221 CC 40 SCAR 18.8
CC
40
Cov
84%
compare
11.4
_compare_list
8.3
encode
8.1
CRAP 6.6
« Phonetic/DaitchMokotoff.pm ↑ Summary Phonetic/Phonix.pm »
1 # ============================================================================
2 package Text::Phonetic;
3 # ============================================================================
4 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Text::Unidecode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 our $AUTHORITY = 'cpan:MAROS';
13 10 our $VERSION = "2.09";
14  
15 10 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 10 our $DEFAULT_ALGORITHM = 'Phonix';
18 10 our @PREDICATES_CHECKED;
19 our @AVAILABLE_ALGORITHMS = grep
20 10 { s/^Text::Phonetic::(.+)$/$1/x }
21 findsubmod Text::Phonetic;
22  
23 10 has 'unidecode' => (
24 is => 'rw',
25 default => 1,
26 required => 1,
27 documentation => q[Transliterate strings to ASCII before processing]
28 );
29  
30 54 ▶after 'BUILDARGS' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 54 my ($class) = @_;
32 54 return $class->check_predicates;
33 10 };
34  
35 # ----------------------------------------------------------------------------
36 # Class methods
37  
38 1 ▶sub available_algorithms {
Subroutine available_algorithms: called CC=1 SCAR=0.0
Pod documented
39 1 return @AVAILABLE_ALGORITHMS;
40 }
41  
42 0 ▶sub register_algorithm {
Subroutine register_algorithm: not called CC=2 SCAR=17.9
Pod undocumented
43 0 my ($class,$algorithm) = @_;
44 push @AVAILABLE_ALGORITHMS,$algorithm
45 0 ▶ unless grep { $algorithm eq $_ } @AVAILABLE_ALGORITHMS;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$algorithm eq $_;} @Text::Phonetic::AVAILABLE_ALGORITHMS 0 0 0
46 0 return $algorithm;
47 }
48  
49 101 ▶sub check_predicates {
Subroutine check_predicates: called CC=5 SCAR=16.1
Pod undocumented
50 101 my ($class) = @_;
51  
52 101 ▶ if ($class->can('_predicates')
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("_predicates") and not grep({$class eq $_;} @Text::Phonetic::PREDICATES_CHECKED)) 9 92 101
Condition100%3/3
$class->can("_predicates") and not grep({$class eq $_;} @Text::Phonetic::PREDICATES_CHECKED)
!ll&&!rl&&r
62309
MC/DC100%2/2
$class->can("_predicates") and not grep({$class eq $_;} @Text::Phonetic::PREDICATES_CHECKED)
$class->can("_predicates") not grep({$class eq $_;} @Text::Phonetic::PREDICATES_CHECKED)
Truth table100%3/3
A and not(B)
A= $class->can("_predicates")B= not grep({$class eq $_;} @Text::Phonetic::PREDICATES_CHECKED)
ABresult
0X0
100
111
53 64 && ! grep { $class eq $_ } @PREDICATES_CHECKED) {
54 9 my @predicates = $class->_predicates;
55 9 foreach my $predicate (@predicates) {
56 9 my $ok = Class::Load::try_load_class($predicate);
57 9 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 7 2 9
58 2 croak("Could not load '$class' phonetic algorithm: Predicate '$predicate' is missing")
59 } else {
60 7 push(@PREDICATES_CHECKED,$class);
61 }
62 }
63 }
64 99 return;
65 }
66  
67 # ----------------------------------------------------------------------------
68 # Constructor (new provided by Moo)
69  
70 47 ▶sub load {
Subroutine load: called CC=7 SCAR=20.2
Pod documented
71 47 my $self = shift;
72 47 ▶ my $params = (scalar @_ == 1 && ref($_[0]) eq 'HASH') ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 && ref $_[0] eq 'HASH' ? : 1 46 47
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
4601
MC/DC50%1/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
73  
74 47 ▶ my $algorithm = delete($params->{algorithm}) || $DEFAULT_ALGORITHM;
Condition66%2/3
delete $params->{'algorithm'} || $Text::Phonetic::DEFAULT_ALGORITHM
l!l&&r!l&&!r
4610
MC/DC0%0/2
delete $params->{'algorithm'} || $Text::Phonetic::DEFAULT_ALGORITHM
delete $params->{'algorithm'} $Text::Phonetic::DEFAULT_ALGORITHM
Truth table66%2/3
A || B
A= delete $params->{'algorithm'}B= $Text::Phonetic::DEFAULT_ALGORITHM
ABresult
1X1
011
000
75 47 my $class = __PACKAGE__.'::'.$algorithm;
76  
77 47 ▶ unless (grep { $algorithm eq $_ } @AVAILABLE_ALGORITHMS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$algorithm eq $_;} @Text::Phonetic::AVAILABLE_ALGORITHMS) 0 47 47
78 0 croak("Could not load '$algorithm' phonetic algorithm: Algorithm not available");
79 }
80  
81 47 ▶ unless (Class::Load::is_class_loaded($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Class::Load::is_class_loaded($class)) 13 34 47
82 13 my ($ok,$error) = Class::Load::try_load_class($class);
83 13 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 13 13
84 0 croak("Could not load '$algorithm' phonetic algorithm: $error")
85 }
86 }
87  
88 47 $class->check_predicates;
89  
90 46 return $class->new($params);
91 }
92  
93 # ----------------------------------------------------------------------------
94 # Public methods
95  
96 283 ▶sub encode {
Subroutine encode: called CC=8 SCAR=20.9
Pod documented
97 283 my $self = shift;
98  
99 # Single value
100 283 ▶ if (scalar(@_) == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 281 2 283
elsif (scalar @_ > 1) { } 2 0 2
101 281 my $string = shift;
102 281 ▶ $string = Text::Unidecode::unidecode($string)
Branch: 1 branch
BranchTrueFalseTotal
if $self->unidecode 281 0 281
103 if ($self->unidecode);
104 return
105 281 ▶ unless defined $string && $string !~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string and not $string =~ /^\s*$/ 105 176 281
Condition100%3/3
defined $string and not $string =~ /^\s*$/
!ll&&!rl&&r
3570176
MC/DC100%2/2
defined $string and not $string =~ /^\s*$/
defined $string not $string =~ /^\s*$/
Truth table100%3/3
A and not(B)
A= defined $stringB= not $string =~ /^\s*$/
ABresult
0X0
100
111
106 176 return $self->_do_encode($string);
107 # Expand list
108 } elsif (scalar(@_) > 1) {
109 2 my @result_list;
110 2 foreach my $string (@_) {
111 6 push @result_list,$self->encode($string);
112 }
113 2 ▶ return wantarray ? @result_list : \@result_list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
114 }
115 # Fallback
116 0 return;
117 }
118  
119  
120 46 ▶sub compare {
Subroutine compare: called CC=10 SCAR=24.4
Pod documented
121 46 my ($self,$string1,$string2) = @_;
122  
123 46 ▶ return 0 unless defined $string1 && $string1 !~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string1 and not $string1 =~ /^\s*$/ 0 46 46
Condition33%1/3
defined $string1 and not $string1 =~ /^\s*$/
!ll&&!rl&&r
0046
MC/DC0%0/2
defined $string1 and not $string1 =~ /^\s*$/
defined $string1 not $string1 =~ /^\s*$/
Truth table33%1/3
A and not(B)
A= defined $string1B= not $string1 =~ /^\s*$/
ABresult
0X0
100
111
124 46 ▶ return 0 unless defined $string2 && $string2 !~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string2 and not $string2 =~ /^\s*$/ 35 11 46
Condition66%2/3
defined $string2 and not $string2 =~ /^\s*$/
!ll&&!rl&&r
03511
MC/DC50%1/2
defined $string2 and not $string2 =~ /^\s*$/
defined $string2 not $string2 =~ /^\s*$/
Truth table66%2/3
A and not(B)
A= defined $string2B= not $string2 =~ /^\s*$/
ABresult
0X0
100
111
125  
126 # Extremely rare case ;-)
127 11 ▶ return 100 if ($string1 eq $string2);
Branch: 1 branch
BranchTrueFalseTotal
if $string1 eq $string2 2 9 11
128  
129 9 ▶ if ($self->unidecode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unidecode) 9 0 9
130 9 $string1 = Text::Unidecode::unidecode($string1);
131 9 $string2 = Text::Unidecode::unidecode($string2);
132  
133 # Also not very likely, but has to be checked
134 9 ▶ return 99 if ($string1 eq $string2);
Branch: 1 branch
BranchTrueFalseTotal
if $string1 eq $string2 2 7 9
135 }
136  
137 7 my $value1 = $self->_do_encode($string1);
138 7 my $value2 = $self->_do_encode($string2);
139  
140 7 ▶ return 0 unless (defined $value1 && defined $value2);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value1 and defined $value2 0 7 7
Condition33%1/3
defined $value1 and defined $value2
!ll&&!rl&&r
007
MC/DC0%0/2
defined $value1 and defined $value2
defined $value1 defined $value2
Truth table33%1/3
A and B
A= defined $value1B= defined $value2
ABresult
0X0
100
111
141  
142 7 return $self->_do_compare($self->_do_encode($string1),$self->_do_encode($string2));
143 }
144  
145 3 ▶sub _do_compare {
Subroutine _do_compare: called CC=2 SCAR=6.9
146 3 my ($self,$result1,$result2) = @_;
147  
148 3 ▶ return 50 if ($result1 eq $result2);
Branch: 1 branch
BranchTrueFalseTotal
if $result1 eq $result2 2 1 3
149 1 return 0;
150 }
151  
152 0 ▶sub _do_encode {
Subroutine _do_encode: not called CC=1 SCAR=6.9
153 0 carp('_do_encode is an abstract method!');
154 }
155  
156 # ----------------------------------------------------------------------------
157 # Utility functions
158  
159 1091 ▶sub _is_inlist {
Subroutine _is_inlist: called CC=5 SCAR=16.2
160 1091 my $string = shift;
161 1091 ▶ return 0 unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 1091 1091
162 1091 ▶ my $list = (scalar @_ == 1 && ref($_[0]) eq 'ARRAY') ? shift : \@_;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 && ref $_[0] eq 'ARRAY' ? : 1 1090 1091
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
109001
MC/DC50%1/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1 ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
163  
164 1091 ▶ return 1 if grep {$string eq $_ } @$list;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$string eq $_;} @$list 235 856 1091
165 856 return 0;
166 }
167  
168 6 ▶sub _compare_list {
Subroutine _compare_list: called CC=8 SCAR=21.1
169 6 my ($list1,$list2) = @_;
170  
171 6 ▶ return 0 unless ref($list1) eq 'ARRAY' && ref($list2) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list1 eq "ARRAY" and ref $list2 eq "ARRAY" 0 6 6
Condition33%1/3
ref $list1 eq "ARRAY" and ref $list2 eq "ARRAY"
!ll&&!rl&&r
006
MC/DC0%0/2
ref $list1 eq "ARRAY" and ref $list2 eq "ARRAY"
ref $list1 eq "ARRAY" ref $list2 eq "ARRAY"
Truth table33%1/3
A and B
A= ref $list1 eq "ARRAY"B= ref $list2 eq "ARRAY"
ABresult
0X0
100
111
172  
173 6 foreach my $element1 (@$list1) {
174 8 ▶ next unless defined $element1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element1 1 7 8
175 7 foreach my $element2 (@$list2) {
176 13 ▶ next unless defined $element2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element2 1 12 13
177 12 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $element1 eq $element2 3 9 12
178 if $element1 eq $element2;
179 }
180 }
181  
182 3 return 0;
183 }
184  
185 10 "Schmitt ~ Smith ~ Schmitz";
186  
187 =encoding utf8
188  
189 =pod
190  
191 =head1 NAME
192  
193 Text::Phonetic - A base class for phonetic algorithms
194  
195 =head1 SYNOPSIS
196  
197 use Text::Phonetic::Metaphone;
198  
199 my $phonetic = Text::Phonetic::Metaphone->new();
200 $encoded_string = $phonetic->encode($string);
201 @encoded_list = $phonetic->encode(@list);
202  
203 my $same = $phonetic->compare($string1,$string2);
204  
205 Or
206  
207 use Text::Phonetic;
208 my $phonetic = Text::Phonetic->load( algorithm => 'Phonix' );
209 $encoded_string = $phonetic->encode($string);
210  
211 This module provides an easy and convinient way to encode names with various
212 phonetic algorithms. It acts as a wrapper around other phonetic algorithm
213 modules like L<Text::Metaphone>, L<Text::DoubleMetaphone>, L<Text::Soundex>
214 and also implements some other algorithms such as
215 L<Text::Phonetic::DaitchMokotoff>, L<Text::Phonetic::Koeln>,
216 L<Text::Phonetic::Phonem> and L<Text::Phonetic::Phonix>.
217  
218 This module can easily be subclassed.
219  
220 =head1 DESCRIPTION
221  
222 =head2 Constructors
223  
224 =head3 new
225  
226 $obj = Text::Phonetic::SUBCLASS->new(%PARAMETERS)
227  
228 You can pass arbitrary attributes to the constructor. The only global
229 attribute is C<unidecode> which defaults to 1 if not set. This attribute
230 controls if non-latin characters should be transliterated to A-Z
231 (see also L<Text::Unidecode>).
232  
233 Additional attributes may be defined by the various implementation classes.
234  
235 =head3 load
236  
237 $obj = Text::Phonetic->load(algorithm => $algorithm, %PARAMETERS)
238  
239 Alternative constructor which also loads the requested algorithm subclass.
240  
241 =head2 Methods
242  
243 =head3 encode
244  
245 $RETURN_STRING = $obj->encode($STRING);
246 OR
247 @RETURN_LIST = $obj->encode(@LIST);
248 OR
249 $RETURN_LIST_REF = $obj->encode(@LIST);
250  
251 Encodes the given string or list of strings. Returns a single value, array or
252 array reference depending on the caller context and parameters.
253  
254 Returns undef on an empty/undefined/whitespace only string.
255  
256 =head3 compare
257  
258 $RETURN_CODE = $obj->compare($STRING1,$STRING2);
259  
260 The return code is an integer between 100 and 0 indicating the likelihood that
261 the to results are the same. 100 means that the strings are completely
262 identical. 99 means that the strings match after all non-latin characters
263 have been transliterated. Values in between 98 and 1 usually mean that the
264 given strings match. 0 means that the used alogorithm couldn't match the two
265 strings at all.
266 C<compare> is a shortcut to the C<$obj-E<gt>_do_compare($CODE1,$CODE2)> method.
267  
268 =head2 Class Methods
269  
270 =head3 available_algorithms
271  
272 my @available = Text::Phonetic->available_algorithms;
273  
274 Returns a list of all available/installed algorithms
275  
276 =head1 SUBLCASSING
277  
278 You can easily subclass Text::Phonetic and add your own phonetic algorithm.
279 All subclasses must use Text::Phonetic as their base class, reside in
280 the Text::Phonetic namespace, and implement the following methods:
281  
282 =head2 _do_encode
283  
284 $RESULT = $obj->_do_encode($STRING);
285  
286 This method does the actual encoding. It should return either a string or
287 an array reference.
288  
289 =head2 _do_compare
290  
291 $RETURN_STRING = $obj->_do_compare($RESULT1,$RESULT2);
292  
293 If your C<_do_encode> method doesn't return a single scalar value you also
294 might need to implement a comparison method. It takes two results as returned
295 by C<_do_encode> and returns an integer value between 98 and 0
296 (see L<"compare">).
297  
298 =head2 _predicates
299  
300 Third party modules can be marked as predicates by adding the C<_predicates>
301 method which should return al list of package names. All predicates will be
302 loaded if installed. If missing an exception will be thrown.
303  
304 =head2 Object structure
305  
306 Text::Phonetic uses L<Moo> to declare attributes.
307  
308 =head2 Helper class methods
309  
310 =head3 _compare_list
311  
312 Text::Phonetic::_compare_list($LIST1_REF,$LIST2_REF);
313  
314 Compares the two arrays and returns true if at least one element is equal
315 (ignoring the position) in both lists.
316  
317 =head2 Example class
318  
319 package Text::Phonetic::MyAlgorithm;
320 use Moo;
321 extends qw(Text::Phonetic);
322  
323 has someattribute => (
324 is => 'rw',
325 );
326  
327 sub _do_encode {
328 my ($self,$string) = @_;
329 # Do something
330 return $phonetic_representation;
331 }
332  
333 __PACKAGE__->meta->make_immutable;
334 no Moo;
335 1;
336  
337 =head1 SEE ALSO
338  
339 L<DBIx::Class::PhoneticSearch> (Build phonetic indices via DBIx::Class),
340 L<Text::Phonetic::VideoGame> (Phonetic encoding for video game titles)
341  
342 =head1 SUPPORT
343  
344 Please report any bugs or feature requests to C<text-phonetic@rt.cpan.org>, or
345 through the web interface at
346 L<http://rt.cpan.org/Public/Bug/Report.html?Queue=Text::Phonetic>.
347 I will be notified, and then you'll automatically be notified of progress on
348 your report as I make changes.
349  
350 =head1 AUTHOR
351  
352 Maroš Kollár
353 CPAN ID: MAROS
354 maros [at] k-1.com
355  
356 http://www.k-1.com
357  
358 =head1 COPYRIGHT
359  
360 Text::Phonetic is Copyright (c) 2006-2012 Maroš Kollár
361 - L<http://www.k-1.com>
362  
363 =head1 LICENCE
364  
365 This library is free software, you can redistribute it and/or modify
366 it under the same terms as Perl itself.
367  
368 =cut
« Phonetic/DaitchMokotoff.pm ↑ Summary Phonetic/Phonix.pm »