blib/lib/Lingua/EUS/Numbers.pm

filter: Statementstmt 98.9% 97 / 98 Branchbran 88.8% 48 / 54 Conditioncond 81.0% 47 / 58 MC/DCmcdc 33.3% 9 / 27 Subroutinesub 100% 9 / 9 Podpod 50.0% 2 / 4 totaltotal 84.8% 212 / 250 SCAR 39.0
CC
48
Cov
91%
cardinal2alpha
29.2
triple_digit_handling
10.3
double_digit_handling
6.1
CRAP 49.5
↑ Summary
1 # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8; -*-
2  
3 package Lingua::EUS::Numbers;
4 # ABSTRACT: Number 2 word conversion in EUS.
5  
6 # {{{ use block
7  
8 5 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 use vars qw(
14 @EXPORT_OK @ISA $VERSION
15 %num2alpha
16 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 require Exporter;
18  
19 # }}}
20 # {{{ variables declaration
21  
22 5 @ISA = qw(Exporter);
23  
24 5 $VERSION = 0.0682;
25  
26 5 @EXPORT_OK = qw(
27 %num2alpha
28 &cardinal2alpha
29 &ordinal2alpha
30 );
31  
32 # The Bask numeral system is vigesimal (base 20). So far, going to
33 # 999_999_999_999.
34  
35 5 %num2alpha = (
36 0 => 'zero',
37 1 => 'bat',
38 2 => 'bi',
39 3 => 'hiru',
40 4 => 'lau',
41 5 => 'bost',
42 6 => 'sei',
43 7 => 'zazpi',
44 8 => 'zortzi',
45 9 => 'bederatzi',
46 10 => 'hamar',
47 11 => 'hamaika',
48 12 => 'hamabi',
49 13 => 'hamahiru',
50 14 => 'hamalau',
51 15 => 'hamabost',
52 16 => 'hamasei',
53 17 => 'hamazazpi',
54 18 => 'hemezortzi',
55 19 => 'hemeretzi',
56 20 => 'hogei',
57 40 => 'berrogei',
58 60 => 'hirurogei',
59 80 => 'laurogei',
60 100 => 'ehun',
61 200 => 'berrehun',
62 300 => 'hirurehun',
63 400 => 'laurehun',
64 500 => 'bostehun',
65 600 => 'seiehun',
66 700 => 'zazpiehun',
67 800 => 'zortziehun',
68 900 => 'bederatziehun',
69 1000 => 'mila',
70 1000000 => 'milioi bat',
71 1000000000 => 'mila milioi'
72 );
73  
74 #Names for quantifiers, every block of 3 digits
75 #(thousands, millions, billions)
76 5 my %block2alpha = (
77 block1 => 'mila',
78 block2 => 'milioi',
79 block3 => 'mila milioi'
80 );
81  
82 # }}}
83  
84 #This function accepts an integer (scalar) as a parameter and
85 #returns a string (array), which is its Bask cardinal equivalent.
86 # {{{ cardinal2alpha
87  
88 sub cardinal2alpha {
89 318 ▶ my $orig_num = shift // return;
Subroutine cardinal2alpha: called CC=29 SCAR=33.8
Pod documented
Condition50%1/2
shift() // (return)
l!l
3180
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
90 318 my @result = ();
91 318 my ( $thousands, $hundreds, $tens, $units );
92 318 my $num = $orig_num;
93  
94 #Input validation
95 318 ▶ unless ( $num =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num =~ /^\d+$/) 5 313 318
96 5 carp "Entry $num not valid. Should be numeric characters only";
97 5 return;
98 }
99  
100 313 ▶ if ( $num > 999_999_999_999 or $num < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > '999999999999' or $num < 0) 1 312 313
Condition66%2/3
$num > '999999999999' or $num < 0
l!l&&r!l&&!r
10312
MC/DC50%1/2
$num > '999999999999' or $num < 0
$num > '999999999999' $num < 0
Truth table66%2/3
A or B
A= $num > '999999999999'B= $num < 0
ABresult
1X1
011
000
101 1 carp "Entry $num not valid. Number should be an integer between 0 and 999,999,999,999";
102 1 return;
103 }
104  
105 #Handling special cases
106 312 ▶ return $num2alpha{0} if $num == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num == 0 2 310 312
107 310 ▶ return $num2alpha{$num} if $num2alpha{$num};
Branch: 1 branch
BranchTrueFalseTotal
if $Lingua::EUS::Numbers::num2alpha{$num} 70 240 310
108  
109 240 my $len = length($num);
110  
111 #Main logic: cutting number by block of 3 digits
112 240 ▶ while ( $len > 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 3) 166 240 406
113  
114 166 $num = reverse($num);
115  
116 #Dealing with the part off the block(s) of three
117 166 my $extra_digits = substr( $num, int( ( $len - 1 ) / 3 ) * 3 );
118 166 $extra_digits = reverse($extra_digits);
119 166 ▶ push ( @result, triple_digit_handling($extra_digits) )
Branch: 1 branch
BranchTrueFalseTotal
unless $extra_digits == 1 112 54 166
120 unless $extra_digits == 1;
121  
122 #Adding name for the quantifier
123 166 my $quantif = 'block' . ( int( ( $len - 1 ) / 3 ) );
124 166 ▶ push ( @result, $block2alpha{$quantif} ) unless $num =~ /000$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $num =~ /000$/ 100 66 166
125  
126 #Special case for 1 million: adding the term for "one"
127 166 ▶ push ( @result, $num2alpha{1} ) if $len == 7 && $extra_digits == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 7 and $extra_digits == 1 32 134 166
Condition66%2/3
$len == 7 and $extra_digits == 1
!ll&&!rl&&r
134032
MC/DC50%1/2
$len == 7 and $extra_digits == 1
$len == 7 $extra_digits == 1
Truth table66%2/3
A and B
A= $len == 7B= $extra_digits == 1
ABresult
0X0
100
111
128  
129 #Adding "eta" after millions (except when there's no thousand)
130 166 my $whats_left = substr( reverse($num), length($extra_digits) );
131 166 ▶ if ( ( $len <= 8 and $len >= 7 )
Branch: 1 branch
BranchTrueFalseTotal
if ($len <= 8 and $len >= 7 and $whats_left != 0 and not reverse($num) =~ /^[^0]000/) 22 144 166
Condition100%9/9
$len <= 8 and $len >= 7 and $whats_left != 0 and not reverse($num) =~ /^[^0]000/
!ll&&!rl&&r
1261822
$len <= 8 and $len >= 7 and $whats_left != 0
!ll&&!rl&&r
122440
$len <= 8 and $len >= 7
!ll&&!rl&&r
447844
MC/DC100%4/4
$len <= 8 and $len >= 7 and $whats_left != 0 and not reverse($num) =~ /^[^0]000/
$len <= 8 $len >= 7 $whats_left != 0 not reverse($num) =~ /^[^0]000/
Truth table100%5/5
A and B and C and not(D)
A= $len <= 8B= $len >= 7C= $whats_left != 0D= not reverse($num) =~ /^[^0]000/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
132 && $whats_left != 0
133 && ( reverse($num) !~ /^[^0]000/ ) )
134 {
135 22 push ( @result, "eta" );
136 }
137  
138 #Adding 'eta' for hundreds, except when there are tens and/or units
139 166 ▶ if ( length($num) <= 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $num <= 6) 78 88 166
140 78 ( $units, $tens, $hundreds, $thousands, my @rest ) =
141 split ( //, reverse($orig_num) );
142  
143 78 ▶ if ( ( $hundreds != 0 && $tens == 0 && $units == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($hundreds != 0 and $tens == 0 and $units == 0 or $hundreds == 0 and $tens || $units and not $num =~ /^0/ or $thousands == 0 and $hundreds == 0 and $tens || $units) 18 42 60
Condition90%27/30
$hundreds != 0 and $tens == 0 and $units == 0 or $hundreds == 0 and $tens || $units and not $num =~ /^0/ or $thousands == 0 and $hundreds == 0 and $tens || $units
l!l&&r!l&&!r
34044
$hundreds != 0 and $tens == 0 and $units == 0 or $hundreds == 0 and $tens || $units and not $num =~ /^0/
l!l&&r!l&&!r
181644
$hundreds != 0 and $tens == 0 and $units == 0
!ll&&!rl&&r
52818
$hundreds != 0 and $tens == 0
!ll&&!rl&&r
50226
$hundreds == 0 and $tens || $units and not $num =~ /^0/
!ll&&!rl&&r
40416
$hundreds == 0 and $tens || $units
!ll&&!rl&&r
103020
$tens or $units
l!l&&r!l&&!r
41630
$thousands == 0 and $hundreds == 0 and $tens || $units
!ll&&!rl&&r
16280
$thousands == 0 and $hundreds == 0
!ll&&!rl&&r
14228
$tens or $units
l!l&&r!l&&!r
2026
MC/DC18%2/11
$hundreds != 0 and $tens == 0 and $units == 0 or $hundreds == 0 and $tens || $units and not $num =~ /^0/ or $thousands == 0 and $hundreds == 0 and $tens || $units
$hundreds != 0 $tens == 0 $units == 0 $hundreds == 0 $tens $units not $num =~ /^0/ $thousands == 0 $hundreds == 0 $tens $units
Truth table4%3/67
A and B and C or D and E or F and not(G) or H and I and J or K
A= $hundreds != 0B= $tens == 0C= $units == 0D= $hundreds == 0E= $tensF= $unitsG= not $num =~ /^0/H= $thousands == 0I= $hundreds == 0J= $tensK= $units
ABCDEFGHIJKresult
111XXXXXXXX1
0XX11X1XXXX1
0XX1011XXXX1
10X11X1XXXX1
10X1011XXXX1
11011X1XXXX1
1101011XXXX1
0XX0XXX111X1
0XX0XXX11011
0XX100X111X1
0XX100X11011
0XX11X0111X1
0XX11X011011
0XX1010111X1
0XX101011011
10X0XXX111X1
10X0XXX11011
10X100X111X1
10X100X11011
10X11X0111X1
10X11X011011
10X1010111X1
10X101011011
1100XXX111X1
1100XXX11011
110100X111X1
110100X11011
11011X0111X1
11011X011011
1101010111X1
110101011011
0XX0XXX0XXX0
0XX0XXX10XX0
0XX0XXX11000
0XX100X0XXX0
0XX100X10XX0
0XX100X11000
0XX11X00XXX0
0XX11X010XX0
0XX11X011000
0XX10100XXX0
0XX101010XX0
0XX101011000
10X0XXX0XXX0
10X0XXX10XX0
10X0XXX11000
10X100X0XXX0
10X100X10XX0
10X100X11000
10X11X00XXX0
10X11X010XX0
10X11X011000
10X10100XXX0
10X101010XX0
10X101011000
1100XXX0XXX0
1100XXX10XX0
1100XXX11000
110100X0XXX0
110100X10XX0
110100X11000
11011X00XXX0
11011X010XX0
11011X011000
11010100XXX0
110101010XX0
110101011000
144 || ( $hundreds == 0 && ( $tens || $units ) ) && $num !~ /^0/
145 || ( $thousands == 0 && $hundreds == 0 && ( $tens || $units ) )
146 )
147 {
148 36 push ( @result, "eta" );
149 }
150 }
151  
152 #Dealing with the remaining digits
153 166 $num = reverse($num);
154 166 $num = substr( $num, length($extra_digits) );
155 166 $len = length($num);
156  
157 } #end while len > 3
158  
159 240 ▶ if ( $len <= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len <= 3) 240 0 240
160 240 push ( @result, triple_digit_handling($num) );
161 240 return "@result";
162 }
163 }
164  
165 # }}}
166  
167 #This function takes an integer (scalar) as a parameter, which is
168 #a 3-digit number or less, and returns a string (array), which is
169 #its Bask equivalent.
170 # {{{ triple_digit_handling
171  
172 sub triple_digit_handling {
173 352 ▶ my $num = shift;
Subroutine triple_digit_handling: called CC=10 SCAR=23.4
Pod undocumented
174 352 my @result = ();
175 352 my ( $hundreds, $tens, $units, @tens_n_units );
176  
177 #Handling exceptional cases
178 352 ▶ return if $num > 999 || $num < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num > 999 or $num < 0 0 352 352
Condition33%1/3
$num > 999 or $num < 0
l!l&&r!l&&!r
00352
MC/DC0%0/2
$num > 999 or $num < 0
$num > 999 $num < 0
Truth table33%1/3
A or B
A= $num > 999B= $num < 0
ABresult
1X1
011
000
179 352 ▶ return if $num == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num == 0 96 256 352
180 256 ▶ return $num2alpha{$num} if $num2alpha{$num};
Branch: 1 branch
BranchTrueFalseTotal
if $Lingua::EUS::Numbers::num2alpha{$num} 42 214 256
181  
182 214 my $len = length($num);
183  
184 #Handling 2-digit numbers
185 214 ▶ if ( $len == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 2) 152 62 214
186 152 ( $tens, $units ) = split ( //, sprintf( "%02d", $num ) );
187 152 @result = double_digit_handling( $tens, $units );
188 152 return @result;
189 }
190  
191 #Handling 3-digit numbers
192 62 ▶ if ( $len == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 3) 62 0 62
193 62 ( $hundreds, $tens, $units ) = split ( //, sprintf( "%03d", $num ) );
194 62 ▶ unless ( $hundreds == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hundreds == 0) 28 34 62
195 28 $hundreds *= 100;
196 28 push ( @result, $num2alpha{$hundreds} );
197 28 ▶ push ( @result, "eta" ) if $tens || $units;
Branch: 1 branch
BranchTrueFalseTotal
if $tens or $units 28 0 28
Condition66%2/3
$tens or $units
l!l&&r!l&&!r
10180
MC/DC0%0/2
$tens or $units
$tens $units
Truth table66%2/3
A or B
A= $tensB= $units
ABresult
1X1
011
000
198 }
199  
200 62 @tens_n_units = double_digit_handling( $tens, $units );
201 62 push ( @result, @tens_n_units );
202 62 return @result;
203 }
204  
205 }
206  
207 # }}}
208  
209 #This function takes two integers (scalars) as parameters (tens and units)
210 #and returns a string (array), which is their Bask equivalent.
211 # {{{ double_digit_handling
212  
213 sub double_digit_handling {
214 214 ▶ my $diz = shift;
Subroutine double_digit_handling: called CC=6 SCAR=18.0
Pod undocumented
215 214 my $unit = shift;
216 214 my $num = "$diz$unit";
217 214 my @result;
218  
219 #Handling exceptional cases
220 214 ▶ return if $num == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num == 0 0 214 214
221  
222 214 ▶ return $num2alpha{$num} if $num2alpha{$num};
Branch: 1 branch
BranchTrueFalseTotal
if $Lingua::EUS::Numbers::num2alpha{$num} 22 192 214
223  
224 192 ▶ return $num2alpha{$unit} unless $diz;
Branch: 1 branch
BranchTrueFalseTotal
unless $diz 38 154 192
225  
226 #Dealing with base 20
227 154 ▶ if ( $diz =~ /[3579]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diz =~ /[3579]/) 80 74 154
228 80 $diz -= 1;
229 80 $unit += 10;
230 }
231 154 $diz = $diz * 10;
232  
233 154 ▶ if ($unit) { push ( @result, "$num2alpha{$diz}ta" ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($unit) { } 154 0 154
234 0 else { push ( @result, $num2alpha{$diz} ); }
235 154 push ( @result, $num2alpha{$unit} );
236  
237 154 return @result;
238 }
239  
240 # }}}
241  
242 #This function accepts an integer (scalar) as a parameter and
243 #returns a string (array), which is its Bask ordinal equivalent.
244 # {{{ ordinal2alpha
245  
246 sub ordinal2alpha {
247 162 ▶ my $num = shift // return;
Subroutine ordinal2alpha: called CC=6 SCAR=18.0
Pod documented
Condition50%1/2
shift() // (return)
l!l
1620
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
248 162 my @result;
249  
250 #Handling special cases
251 162 ▶ return unless $num =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $num =~ /^\d+$/ 5 157 162
252 157 ▶ return if ( $num < 0 || $num > 999_999_999_999 );
Branch: 1 branch
BranchTrueFalseTotal
if $num < 0 or $num > '999999999999' 1 156 157
Condition66%2/3
$num < 0 or $num > '999999999999'
l!l&&r!l&&!r
01156
MC/DC50%1/2
$num < 0 or $num > '999999999999'
$num < 0 $num > '999999999999'
Truth table66%2/3
A or B
A= $num < 0B= $num > '999999999999'
ABresult
1X1
011
000
253 156 ▶ return "lehenengo" if $num == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num == 1 1 155 156
254  
255 155 push ( @result, join ( '', cardinal2alpha($num), "garren" ) );
256 155 return "@result";
257 }
258  
259 # }}}
260  
261 5 1;
262 __END__
↑ Summary