blib/lib/Text/Metaphone/Amharic.pm

filter: Statementstmt 67.6% 92 / 136 Branchbran 41.3% 24 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.5% 12 / 17 Podpod 0.0% 0 / 9 totaltotal 58.1% 128 / 220 CC 33 SCAR 22.9
CC
33
Cov
60%
_formatStyle
30.0
im
13.7
import
12.0
CRAP 9.8
↑ Summary
1 package Text::Metaphone::Amharic;
2  
3 # If either of these next two lines are inside
4 # the BEGIN block the package will break.
5 #
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::Ethiopic::Amharic qw(:forms overload setForm);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 BEGIN
10 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶ use vars qw( $VERSION %IMExpected %IMError %plosives $GRANULARITY $STYLE );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 $VERSION = "0.14";
16  
17 1 %plosives = (
18 ቅ => 'k',
19 ጥ => 't',
20 ጭ => 'ʧ',
21 ጵ => 'p',
22 ጽ => 's'
23 );
24 1 %IMExpected =(
25 ስ => "s",
26 ጽ => "s'",
27 ቅ => "k'",
28 ቕ => "q",
29 ት => "t",
30 ጥ => "t'",
31 ች => "ʧ",
32 ጭ => "ʧ",
33 ን => "n",
34 ኝ => "n",
35 ክ => "k",
36 ዝ => "z",
37 ዥ => "ʒ",
38 ጵ => "p'",
39 ፕ => "p"
40 );
41 1 %IMError =(
42 ስ => "ጽ",
43 ጽ => "ስ",
44 ቅ => "ቕ",
45 ቕ => "ቅ",
46 ት => "ጥ",
47 ጥ => "ት",
48 ች => "ጭ",
49 ጭ => "ች",
50 ን => "ኝ",
51 ኝ => "ን",
52 ክ => "ኽ",
53 ዝ => "ዥ",
54 ዥ => "ዝ",
55 ጵ => "ፕ",
56 ፕ => "ጵ"
57 );
58 1 $GRANULARITY = "low";
59 1 $STYLE = "ethio";
60 }
61  
62  
63 sub import
64 0 ▶{
Subroutine import: not called CC=3 SCAR=24.8
65 0 my ( $pkg, %args ) = @_;
66  
67 0 ▶ $STYLE = lc($args{style}) if ( $args{style} );
Branch: 1 branch
BranchTrueFalseTotal
if $args{"style"} 0 0 0
68 0 ▶ $GRANULARITY = lc($args{granularity}) if ( $args{granularity} );
Branch: 1 branch
BranchTrueFalseTotal
if $args{"granularity"} 0 0 0
69 }
70  
71  
72 sub new
73 1 ▶{
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
74 1 my $class = shift;
75 1 my $self = { _style => $STYLE, _granularity => $GRANULARITY };
76  
77 1 my $blessing = bless ( $self, $class );
78  
79 1 %_ = @_;
80  
81 1 ▶ $self->{_style} = lc($_{style}) if ( $_{style} );
Branch: 1 branch
BranchTrueFalseTotal
if $_{"style"} 0 1 1
82 1 ▶ $self->{_granularity} = lc($_{granularity}) if ( $_{granularity} );
Branch: 1 branch
BranchTrueFalseTotal
if $_{"granularity"} 0 1 1
83  
84 1 $blessing;
85 }
86  
87  
88 sub _formatStyle
89 0 ▶{
Subroutine _formatStyle: not called CC=5 SCAR=34.0
90 0 my ($self, $keys) = @_;
91  
92 0 ▶ if ( $self->{_style} eq "ipa" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_style'} eq 'ipa') { } 0 0 0
elsif ($self->{'_style'} eq 'sera') { } 0 0 0
93 0 foreach my $i ( 0..$#{$keys} ) {
94 0 $keys->[$i] =~ s/([ቅጥጭጵጽ])/$plosives{$1}'/og;
95 0 $keys->[$i] =~ tr/ህልምርስሽቕብትችንኝእክውይድዽጅዝዥግጝፍፕ/hlmrsʃqbtʧnɲakwjdɗʤzʒgɲfp/;
96 }
97 }
98 elsif ( $self->{_style} eq "sera" ) {
99 0 foreach my $i ( 0..$#{$keys} ) {
100 0 $keys->[$i] =~ tr/ህልምርስሽቅቕብትችንኝእክውይድዽጅዝዥግጝጥጭጵጽፍፕ/hlmrsxqQbtcnNakwydDjzZgGTCPSfp/;
101 }
102 }
103  
104 }
105  
106  
107 sub metaphone
108 9 ▶{
Subroutine metaphone: called CC=4 SCAR=14.0
Pod undocumented
109 9 my $self = shift;
110  
111 9 $_ = $self->simplify($_[0]);
112 9 my ($re, @keys) = $self->glyphs ( $_ );
113 9 ($re, @keys) = $self->phono ( $re, @keys );
114 9 ($re, @keys) = $self->im ( $re, @keys );
115  
116 9 ▶ if ( @keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) 9 0 9
117 9 push ( @keys, qr/$re/ );
118 }
119  
120 9 ▶ $self->_formatStyle ( \@keys ) if ( $self->{_style} ne "ethio" );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_style"} ne "ethio" 0 9 9
121  
122 9 ▶ (wantarray) ? @keys : $keys[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 7 9
123 }
124  
125  
126 sub simplify
127 9 ▶{
Subroutine simplify: called CC=3 SCAR=11.2
Pod undocumented
128 9 my $self = shift;
129  
130 9 $_ = $_[0];
131  
132 #
133 # strip out all but first vowel:
134 #
135 9 s/^[=#አ#=]/አ/o;
136 9 s/[=#ሀ#=]/ሀ/og;
137  
138 9 s/(.)[=#አ#=]/$1/g;
139  
140 9 ▶ if ( $self->{_granularity} eq "low" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_granularity'} eq 'low') { } 9 0 9
141 9 s/(.)[#ወ#]/$1/og;
142 9 s/(.)[#የ#]/$1/og;
143 }
144 else {
145 0 s/([#11#])/setForm($1,$ሳድስ)."ዋ"/eg;
146 }
147 9 s/[=#ሰ#=]/ሰ/og;
148 9 s/[=#ጸ#=]/ጸ/og;
149 9 s/[#ቨ#]/በ/og;
150  
151 #
152 # now strip vowels, this simplies later code:
153 #
154 9 ▶ s/(\p{Ethiopic})/ ($1 eq 'ኘ') ? $1 : setForm($1,$ሳድስ)/eg;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq "\x{1298}" ? : 0 24 24
155  
156  
157 9 $_;
158 }
159  
160  
161 sub glyphs
162 9 ▶{
Subroutine glyphs: called CC=4 SCAR=21.7
Pod undocumented
163 9 my $self = shift;
164  
165  
166 9 my @keys = ( $_[0] );
167 9 my $re = $_[0];
168  
169 #
170 # Confusion with ዽ
171 #
172 9 ▶ if ( $keys[0] =~ /ዽ/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keys[0] =~ /\x{12fd}/) 0 9 9
173 0 $keys[2] = $keys[1] = $keys[0];
174 0 $keys[0] =~ s/ዽ/ድ/o; # caps problem
175 # $keys[1] literal
176 0 $keys[2] =~ s/ዽ/ጵ/o; # mistaken glyph
177 0 $re =~ s/ዽ/([ድዽጵ])/og;
178 }
179 #
180 # Confusion with ኘ
181 #
182 9 ▶ if ( $keys[0] =~ /ኘ/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keys[0] =~ /\x{1298}/) 0 9 9
183 0 my (@newKeysA, @newKeysB);
184 0 ▶ for (my $i=0; $i < @keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @keys) 0 0 0
185 0 $newKeysA[$i] = $newKeysB[$i] = $keys[$i]; # copy old keys
186 # $keys[$i] literal
187 0 $newKeysA[$i] =~ s/ኘ/ን/o; # caps problem
188 0 $newKeysB[$i] =~ s/ኘ/ፕ/o; # mistaken glyph
189 }
190 0 push (@keys,@newKeysA); # add new keys to old keys
191 0 push (@keys,@newKeysB); # add new keys to old keys
192 0 $re =~ s/ኘ/[ንኝፕ]/og;
193 }
194  
195 9 ($re, @keys);
196 }
197  
198  
199 sub phono
200 9 ▶{
Subroutine phono: called CC=3 SCAR=11.0
Pod undocumented
201 9 my ( $self, $re, @keys ) = @_;
202  
203  
204  
205 9 ▶ if ( $keys[0] =~ /ም[ብፍ]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keys[0] =~ /\x{121d}[\x{1265}\x{134d}]/) 1 8 9
206 1 my @newKeys;
207 1 ▶ for (my $i=0; $i < @keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @keys) 1 1 2
208 1 $newKeys[$i] = $keys[$i]; # copy old keys
209 1 $newKeys[$i] =~ s/ምብ/ንብ/o; # update old keys for primary mapping
210 1 $newKeys[$i] =~ s/ምፍ/ንፍ/o; # update old keys for primary mapping
211 }
212 1 push (@keys,@newKeys); # add new keys to old keys
213 1 $re =~ s/ምብ/[ምን]ብ/og;
214 1 $re =~ s/ምፍ/[ምን]ፍ/og;
215 }
216  
217 9 ($re, @keys);
218 }
219  
220  
221 sub im
222 9 ▶{
Subroutine im: called CC=11 SCAR=26.2
Pod undocumented
223 9 my ( $self, $re, @keys ) = @_;
224  
225 9 my $first = 1;
226 #
227 # Handle IM problems
228 # try to keep least probable keys last:
229 #
230 9 $_ = $keys[0]; # bidi folding # upper-to-lower
231 9 ▶ my $keyboard = ( $self->{_granularity} eq "high" ) ? qr/([ስቅቕትችንኝክዝዥጥጭጽጵፕ])/ : qr/([ቕኝዥጥጭጽጵ])/ ;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_granularity'} eq 'high' ? : 0 9 9
232  
233 9 ▶ while ( /$keyboard/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$keyboard/) 3 9 12
234 3 my $a = $1;
235 3 my @newKeys;
236 3 ▶ if ( $self->{_granularity} eq "low" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_granularity'} eq 'low') { } 3 0 3
237 3 s/$a/$IMExpected{$a}/g;
238 }
239 else {
240 0 s/$a/$IMExpected{$a}/;
241 }
242  
243 3 ▶ for (my $i=0; $i < @keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @keys) 3 3 6
244 3 $newKeys[$i] = $keys[$i]; # copy old keys
245 }
246  
247 3 ▶ if ( $first ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 3 0 3
248 # update new keys for alternative
249 3 ▶ $keys[0] =~ s/^$a/ሀ$a/ if ( $self->{_style} ne "ipa" );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_style"} ne "ipa" 3 0 3
250 3 ▶ if ( $self->{_granularity} eq "low" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_granularity'} eq 'low') { } 3 0 3
251 3 $newKeys[0] =~ s/$a/ሀ$IMError{$a}/g;
252 }
253 else {
254 0 $newKeys[0] =~ s/$a/ሀ$IMError{$a}/;
255 }
256 }
257  
258 3 ▶ for (my $i=$first; $i < @newKeys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @newKeys) 0 3 3
259 # update new keys for alternative
260 0 ▶ if ( $self->{_granularity} eq "low" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_granularity'} eq 'low') { } 0 0 0
261 0 $newKeys[$i] =~ s/([^ሀ])$a/$1ሀ$IMError{$a}/g;
262 }
263 else {
264 0 $newKeys[$i] =~ s/([^ሀ])$a/$1ሀ$IMError{$a}/;
265 }
266 }
267  
268 3 $first = 0;
269 3 push (@keys,@newKeys); # add new keys to old keys
270  
271 #
272 # this still needs work:
273 #
274 # $re =~ s/$a(?!\w+?\])/[$a$IMError{$a}]/g;
275 3 $re =~ s/(?<!\[\w)$a(?!\w+?\])/[$a$IMError{$a}]/g;
276 }
277  
278 #
279 # convert symbols that were missed in low granularity mode:
280 #
281 9 foreach my $i (0..$#keys) {
282 13 $keys[$i] =~ s/ሀ//og;
283 13 $keys[$i] =~ s/ኘ/ኝ/og;
284 }
285  
286 9 ($re, @keys);
287 }
288  
289  
290 sub reverse
291 0 ▶{
Subroutine reverse: not called CC=3 SCAR=24.8
Pod undocumented
292 0 my $self = shift;
293  
294 0 $_ = $_[0];
295
296 0 ▶ if ( $self->{_style} eq "ipa" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_style'} eq 'ipa') { } 0 0 0
elsif ($self->{'_style'} eq 'sera') { } 0 0 0
297 0 s/([stʧkp])'/$plosives{$1}/g;
298 0 tr/hlmrsʃqbtʧnɲakwjdɗʤzʒgɲfp/ህልምርስሽቕብትችንኝእክውይድዽጅዝዥግጝፍፕ/;
299 }
300 elsif ( $self->{_style} eq "sera" ) {
301 0 tr/hlmrsxqQbtcnNakwydDjzZgTCPSGfp/ህልምርስሽቅቕብትችንኝእክውይድዽጅዝዥግጝጥጭጵጽፍፕ/;
302 }
303  
304 0 $_;
305 }
306  
307  
308 sub style
309 0 ▶{
Subroutine style: not called CC=2 SCAR=17.9
Pod undocumented
310 0 my $self = shift;
311  
312 0 ▶ $self->{_style} = lc($_[0]) if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
313  
314 0 $self->{_style};
315 }
316  
317  
318 sub granularity
319 0 ▶{
Subroutine granularity: not called CC=2 SCAR=17.9
Pod undocumented
320 0 my $self = shift;
321  
322 0 ▶ $self->{_granularity} = lc($_[0]) if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
323  
324 0 $self->{_granularity};
325 }
326  
327  
328  
329 #########################################################
330 # Do not change this, Do not put anything below this.
331 # File must return "true" value at termination
332 1 1;
333 ##########################################################
334  
335 __END__
↑ Summary