File Coverage

blib/lib/String/Equivalence/Amharic.pm
Criterion Covered Total %
statement 96 116 82.7
branch 16 34 47.0
condition 1 3 33.3
subroutine 14 17 82.3
pod 5 6 83.3
total 132 176 75.0


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