File Coverage

blib/lib/String/Equivalence/Amharic.pm
Criterion Covered Total %
statement 93 113 82.3
branch 16 34 47.0
condition 1 3 33.3
subroutine 13 16 81.2
pod 5 6 83.3
total 128 172 74.4


line stmt bran cond sub pod time code
1             package String::Equivalence::Amharic;
2              
3             # If either of these next two lines are inside
4             # the BEGIN block the package will break.
5             #
6             binmode(STDOUT, ":utf8");
7 2     2   562050 use strict;
  2         4  
  2         75  
8 2     2   11 use warnings;
  2         3  
  2         130  
9 2     2   10 use utf8;
  2         4  
  2         21  
10 2     2   1133 use Regexp::Ethiopic::Amharic qw(:forms overload setForm subForm %AmharicEquivalence);
  2         11364  
  2         12  
11              
12             BEGIN
13             {
14 2     2   1023 use base qw( Exporter );
  2         4  
  2         353  
15 2     2   15 use vars qw( $VERSION @EXPORT %HaMaps );
  2         3  
  2         248  
16              
17 2     2   7 $VERSION = "0.08";
18              
19 2         7 @EXPORT = qw( &downgrade &inflate &isEquivalentTo &isReducible &hasEquivalence );
20              
21 2         453 %HaMaps =(
22             ሐ => "ኀኃሀሃ",
23             ኀ => "ሐኃሀሃ",
24             ኃ => "ኀሐሀሃ",
25             ሓ => "ሐኀኃሀሃ",
26             ኻ => "ሀሃ",
27             ኍ => "ኁሁሑ"
28             );
29             }
30              
31              
32             sub new
33             {
34 2     2 0 2686 bless ( {}, shift );
35             }
36              
37              
38             sub _downgradeMultiTarget
39             {
40 1     1   4 my ( $list, $re, $from, $targets ) = @_;
41              
42 1         6 my @to = split ( //, $targets );
43 1         2 my @outList = ();
44              
45 1         4 foreach my $to (@to) {
46 4         7 my @newList;
47 4         9 for (my $i=0; $i < @{$list}; $i++) {
  12         31  
48 8         16 $newList[$i] = $list->[$i]; # copy old list
49 8         43 $newList[$i] =~ s/$from/$to/;
50             }
51 4         14 push ( @outList, @newList ); # add new keys to old keys
52             }
53 1         3 push ( @{$list}, @outList ); # add new keys to old keys
  1         4  
54 1         39 $$re =~ s/$from(?!\])/[$from$targets]/;
55             }
56              
57              
58             sub _downgrade
59             {
60 1     1   4 my ( $list, $re, $from, $to ) = @_;
61              
62 1 50       5 unless ( $to ) {
63 1         2 $to = $from;
64 1         6 $to =~ tr/ሀሃሗሠ-ሧኣእኧቍኵጕቈኰጐቆኮጎዑዒዔዕዖፀ-ፆኹኺኼኽኾ/ሃሀኋሰ-ሷአእቁኩጉቆኮጎቈኰጐዕኡኢኤእኦጸ-ጾሁሂሄህሆ/;
65             }
66              
67 1         3 my @newList;
68 1         3 for (my $i=0; $i < @{$list}; $i++) {
  2         9  
69 1         3 $newList[$i] = $list->[$i]; # copy old list
70 1         23 $newList[$i] =~ s/$from/$to/;
71             }
72 1         7 push ( @{$list}, @newList ); # add new keys to old keys
  1         3  
73 1         27 $$re =~ s/$from(?!\])/[$from$to]/;
74             }
75              
76              
77             sub downgrade
78             {
79 1     1 1 951 my $self;
80              
81 1         4 ($self, $_) = @_;
82 1 50       6 $_ = $self unless ( ref($self) );
83              
84 1         3 my @list = ( $_ );
85 1         4 my $re = $_;
86 1         4 my @letters = split ( // );
87              
88 1         4 foreach ( @letters ) {
89 3 100       18 if ( /([#ሠፀ#]|[ሀሃሗኣእኧቍኵጕቈኰጐቆኮጎዑዒዔዕዖኹኺኼኽኾ])/ ) {
90 1         4 my $from = $1;
91 1 50 33     8 _downgrade ( \@list, \$re, $from )
92             unless ( $from eq "እ" && $re =~ /^እ/ );
93             }
94 3 50       13 if ( /([ዓዐ])/ ) {
95 0 0       0 my $to = ( $1 eq "ዓ" ) ? "አዐ" : "አዓ" ;
96 0         0 _downgradeMultiTarget ( \@list, \$re, $1, $to );
97             }
98 3 50       20 if ( /([ሑሒሔሕሖኁኂኄኅኆ])/ ) {
99 0         0 my $from = $1;
100 0 0       0 my $compliment = ( $from =~ /[#ኀ#]/ ) ? "ሐ" : "ኀ" ;
101 0         0 my $to = subForm ( $compliment, $from ).subForm ( 'ሀ', $from );
102 0         0 _downgradeMultiTarget ( \@list, \$re, $from, $to );
103             }
104 3 100       14 if ( /([ሐኀኃሓኻኍ])/ ) {
105 1         5 my $to = $HaMaps{$1};
106 1         7 _downgradeMultiTarget ( \@list, \$re, $1, $to );
107             }
108             }
109              
110              
111 1 50       13 wantarray ? ( @list, $re ) : $list[$#list] ;
112             }
113              
114              
115             sub isReducible
116             {
117 0     0 1 0 my $self;
118              
119 0         0 ($self, $_) = @_;
120 0 0       0 $_ = $self unless ( ref($self) );
121              
122 0         0 /[#ሐኀሠዐፀ#]|[ቍኍኵጕቈኈኰጐ]/;
123              
124             }
125              
126              
127             sub hasEquivalence
128             {
129 0     0 1 0 my $self;
130              
131 0         0 ($self, $_) = @_;
132 0 0       0 $_ = $self unless ( ref($self) );
133              
134 0         0 /[=#ሀሠዐፀ#=]|[=ቍ=]|[=ኍ=]|[=ኵ=]|[=ጕ=]|[=ቈ=]|[=ኈ=]|[=ኰ=]|[=ጐ=]/;
135             }
136              
137              
138             sub _inflate
139             {
140 2     2   4 my ($re, @words);
141              
142 2         5 foreach (@_) {
143 3         6 $re = $_;
144 3         24 $re =~ s/\[(\w+)\]//;
145              
146 3         13 my @letters = split ( //, $1 );
147 3         5 foreach ( @letters ) {
148 14         28 push ( @words,$re );
149 14         48 $words[ $#words ] =~ s//$_/;
150             }
151              
152             }
153              
154 2 100       8 if ( $words[0] =~ /\[/ ) {
155 1         4 push ( @words, _inflate( @words ) );
156 1         4 @words = grep { !/\[/ } @words;
  14         33  
157             }
158            
159 2         25 return @words;
160             }
161              
162              
163             sub inflate
164             {
165 1     1 1 975 my $self;
166              
167 1         3 ($self, $_) = @_;
168 1 50       8 $_ = $self unless ( ref($self) );
169              
170 1         3 my @words = ( $_ );
171 1         2 my $re = $_;
172              
173 1         4 my @letters = split ( // );
174              
175 1         4 foreach ( @letters ) {
176 3 100       12 if ( $AmharicEquivalence{$_} ) {
177             #
178             # these next 3 lines are here to skip over old Amharic
179             #
180 2         5 my $equiv = $AmharicEquivalence{$_};
181 2         11 $equiv =~ s/[#ኸ#]//g;
182 2         44 $re =~ s/$_/[$equiv]/g;
183             # $re =~ s/$_/[$AmharicEquivalence{$_}]/g;
184             }
185             }
186              
187 1 50       6 if ( $re =~ /\[/ ) {
188 1         3 push ( @words, _inflate( $re ) );
189 1         4 @words = grep { !/\[/ } @words;
  13         29  
190 1         2 push ( @words, $re );
191             }
192              
193 1         10 return @words;
194             }
195              
196              
197             sub isEquivalentTo
198             {
199 0     0 1   my ($self, $a, $b) = @_;
200              
201 0 0         unless ( ref($self) ) {
202 0           $b = $a;
203 0           $a = $self;
204             }
205              
206 0           my @b = $self->inflate( $b );
207            
208 0           ( $a =~ /^$b[$#b]$/ );
209             }
210              
211              
212             #########################################################
213             # Do not change this, Do not put anything below this.
214             # File must return "true" value at termination
215             1;
216             ##########################################################
217              
218             __END__