blib/lib/Lingua/ITA/Numbers.pm

filter: Statementstmt 68.6% 103 / 150 Branchbran 48.3% 30 / 62 Conditioncond 66.6% 10 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 68.1% 15 / 22 Podpod 100% 13 / 13 totaltotal 64.3% 175 / 272 SCAR 47.1
CC
38
Cov
63%
number_to_it
37.1
convert_to_string
16.8
div
12.0
CRAP 111.2
↑ Summary
1 # For Emacs: -*- mode:cperl; mode:folding -*-
2  
3 package Lingua::ITA::Numbers;
4 # ABSTRACT: Number 2 Word conversion in ITA.
5  
6 # {{{ use block
7  
8 1 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 use overload (
14 1 '""' => \&get_string,
15 '0+' => \&get_number,
16 '=' => \&clone,
17 '+' => \&add,
18 '-' => \&minus,
19 '*' => \&mult,
20 '/' => \&div,
21 1 ▶ fallback => 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Perl6::Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # }}}
27 # {{{ variables declaration
28  
29 1 our $VERSION = 0.0682;
30  
31 1 our %SIGN_NAMES = ('-' => 'meno',
32 '+' => '');
33  
34 1 our %OUTPUT_DECIMAL_DELIMITERS = (normal => 'virgola',
35 fract => 'e');
36  
37 1 our %NUMBER_NAMES = (
38 0 => 'zero',
39 1 => 'un',
40 2 => 'due',
41 3 => 'tre',
42 4 => 'quattro',
43 5 => 'cinque',
44 6 => 'sei',
45 7 => 'sette',
46 8 => 'otto',
47 9 => 'nove',
48 10 => 'dieci',
49 11 => 'undici',
50 12 => 'dodici',
51 13 => 'tredici',
52 14 => 'quattordici',
53 15 => 'quindici',
54 16 => 'sedici',
55 17 => 'diciassette',
56 18 => 'diciotto',
57 19 => 'diciannove',
58 20 => 'venti',
59 30 => 'trenta',
60 40 => 'quaranta',
61 50 => 'cinquanta',
62 60 => 'sessanta',
63 70 => 'settanta',
64 80 => 'ottanta',
65 90 => 'novanta',
66 100 => 'cento');
67  
68 1 our @PART_NAMES = qw(_ mila milioni miliardi);
69 1 our @UNITS_NAMES = qw(_ mille milione miliardo);
70 1 our @FRACT_NAMES = qw(decim centesim millesim decimillesim centomillesim
71 milionesim decimilionesim centomilionesim
72 miliardesim);
73  
74 1 our %DEFAULT_OPTS = (
75 decimal => 0,
76 decmode => 'normal',
77 name => "",
78 );
79  
80 # }}}
81 # {{{ number_to_it
82  
83 sub number_to_it :Export {
84 17 ▶ my ($number,$opts) = @_;
Subroutine number_to_it: called CC=11 SCAR=36.1
Pod documented
85 17 ▶ $opts = {} unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 8 9 17
86 17 $opts = {%DEFAULT_OPTS,%$opts};
87 17 my $parsed = parse_num_string($number);
88 17 my @parts = ();
89 17 push @parts,$SIGN_NAMES{$parsed->{sign}};
90 17 my $intpart = convert_to_string($parsed->{intpart});
91 17 my $one = $NUMBER_NAMES{1};
92 17 $intpart =~ s/($one)$/$1o/;
93 17 push @parts,$intpart;
94 17 ▶ if ($opts->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"name"}) 0 17 17
95 my $name = ! ref($opts->{name}) ? $opts->{name} :
96 0 ▶ ($intpart eq $NUMBER_NAMES{1} ? $opts->{name}[0] : $opts->{name}[1]);
Branch: 2 branches
BranchTrueFalseTotal
!ref($opts->{'name'}) ? : 0 0 0
$intpart eq $Lingua::ITA::Numbers::NUMBER_NAMES{'1'} ? : 0 0 0
97  
98 0 push @parts,$name;
99 }
100  
101 17 ▶ if ($parsed->{fracpart} || $opts->{decimal}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed->{"fracpart"} or $opts->{"decimal"}) 0 17 17
Condition33%1/3
$parsed->{"fracpart"} or $opts->{"decimal"}
l!l&&r!l&&!r
0017
MC/DC0%0/2
$parsed->{"fracpart"} or $opts->{"decimal"}
$parsed->{"fracpart"} $opts->{"decimal"}
Truth table33%1/3
A or B
A= $parsed->{"fracpart"}B= $opts->{"decimal"}
ABresult
1X1
011
000
102 0 push @parts,$OUTPUT_DECIMAL_DELIMITERS{$opts->{decmode}};
103 0 ▶ if ($opts->{decimal}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"decimal"}) 0 0 0
104 0 ▶ if (length($parsed->{fracpart}) < $opts->{decimal}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $parsed->{"fracpart"} < $opts->{"decimal"}) 0 0 0
105 $parsed->{fracpart} .= "0" x ($opts->{decimal} -
106 0 length($parsed->{fracpart}));
107 }
108 }
109 0 my $fractpart = convert_to_string($parsed->{fracpart});
110 0 ▶ if ($opts->{decmode} eq 'fract') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'decmode'} eq 'fract') { } 0 0 0
111 0 ▶ if ($fractpart eq $NUMBER_NAMES{1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fractpart eq $Lingua::ITA::Numbers::NUMBER_NAMES{'1'}) { } 0 0 0
112 0 push @parts,$fractpart,$FRACT_NAMES[length($parsed->{fracpart}) - 1] . "o";
113 }
114 else {
115 0 push @parts,$fractpart,$FRACT_NAMES[length($parsed->{fracpart}) - 1] . "i";
116 }
117 }
118 else {
119 0 my $one = $NUMBER_NAMES{1};
120 0 $fractpart =~ s/($one)$/$1o/;
121 0 push @parts,$fractpart;
122 }
123 }
124 17 my $result = join(" ",@parts);
125 17 $result =~ s/^\s*//;
126 17 return $result;
127 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 # }}}
130 # {{{ convert_short
131  
132 sub convert_short {
133 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 22 ▶ my $num = shift; # 1 < num < 1000
Subroutine convert_short: called CC=8 SCAR=20.8
Pod documented
135  
136 22 my $hundreds = $num / 100;
137 22 my $tens = $num % 100;
138 22 my @parts = ();
139 22 ▶ if ($hundreds == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hundreds == 1) { } 5 17 22
elsif ($hundreds > 1) { } 8 9 17
140 5 push @parts,$NUMBER_NAMES{100};
141 }
142 elsif ($hundreds > 1) {
143 8 push @parts,$NUMBER_NAMES{$hundreds},$NUMBER_NAMES{100};
144 }
145 22 ▶ if ($tens == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tens == 0) { } 2 20 22
elsif ($tens <= 20) { } 6 14 20
146 #nothing
147 ;
148 }
149 elsif ($tens <= 20) {
150 6 push @parts,$NUMBER_NAMES{$tens};
151 }
152 else {
153 14 my $units = $tens % 10;
154 14 $tens = $tens - $units;
155 14 my $tenstr = $NUMBER_NAMES{$tens};
156 14 ▶ $tenstr =~ s/.$// if ($units == 1) or ($units == 8);
Branch: 1 branch
BranchTrueFalseTotal
if $units == 1 or $units == 8 7 7 14
Condition100%3/3
$units == 1 or $units == 8
l!l&&r!l&&!r
437
MC/DC100%2/2
$units == 1 or $units == 8
$units == 1 $units == 8
Truth table100%3/3
A or B
A= $units == 1B= $units == 8
ABresult
1X1
011
000
157 14 push @parts,$tenstr;
158 14 ▶ if ($units >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($units >= 1) 14 0 14
159 14 push @parts,$NUMBER_NAMES{$units};
160 }
161 }
162 22 return join("",@parts);
163 }
164  
165 # }}}
166 # {{{ convert_to_string
167  
168 sub convert_to_string {
169 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 17 ▶ my $number = shift; #$number >= 0 and integer
Subroutine convert_to_string: called CC=15 SCAR=28.2
Pod documented
171 17 ▶ return $NUMBER_NAMES{0} if !$number || $number !~ m/[1-9]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $number and $number =~ /[1-9]/ 1 16 17
Condition66%2/3
$number and $number =~ /[1-9]/
!ll&&!rl&&r
1016
MC/DC50%1/2
$number and $number =~ /[1-9]/
$number $number =~ /[1-9]/
Truth table66%2/3
A and B
A= $numberB= $number =~ /[1-9]/
ABresult
0X0
100
111
172 16 ▶ return $NUMBER_NAMES{1} if "$number" eq "1" ;
Branch: 1 branch
BranchTrueFalseTotal
if "$number" eq 1 2 14 16
173 14 ▶ if (my $r = length($number) % 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = length($number) % 3) 9 5 14
174 9 $number = "0" x (3 - $r) . $number;
175 }
176 14 my @blocks = ($number =~ m!(\d\d\d)!g);
177 14 @blocks = reverse @blocks;
178 14 ▶ if (@blocks > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@blocks > 4) 0 14 14
179 0 carp "Numbers bigger than 1e10-1 not handled in version $VERSION";
180 0 return;
181 }
182 14 my @name_parts = ();
183 14 my $firstpart = "";
184 14 ▶ if ($blocks[0] == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($blocks[0] == 1) { } 2 12 14
elsif ($blocks[0] > 1) { } 12 0 12
185 #nb one of the following blocks is != 0, since the whole number
186 #is greater than one
187 2 $firstpart = $NUMBER_NAMES{1};
188 }
189 elsif ($blocks[0] > 1) {
190 12 $firstpart = convert_short($blocks[0]);
191 }
192 14 ▶ if ($#blocks >= 1 && $blocks[1] == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#blocks >= 1 and $blocks[1] == 1) { } 0 14 14
elsif ($#blocks >= 1 and $blocks[1] > 1) { } 6 16 22
Condition66%2/3
$#blocks >= 1 and $blocks[1] == 1
!ll&&!rl&&r
860
MC/DC0%0/2
$#blocks >= 1 and $blocks[1] == 1
$#blocks >= 1 $blocks[1] == 1
Truth table66%2/3
A and B
A= $#blocks >= 1B= $blocks[1] == 1
ABresult
0X0
100
111
193 0 $firstpart = $UNITS_NAMES[1] . $firstpart;
194 }
195 ▶ elsif ($#blocks >= 1 && $blocks[1] > 1) {
Condition66%2/3
$#blocks >= 1 and $blocks[1] > 1
!ll&&!rl&&r
806
MC/DC50%1/2
$#blocks >= 1 and $blocks[1] > 1
$#blocks >= 1 $blocks[1] > 1
Truth table66%2/3
A and B
A= $#blocks >= 1B= $blocks[1] > 1
ABresult
0X0
100
111
196 6 $firstpart = convert_short($blocks[1]) . $PART_NAMES[1] . $firstpart;
197 }
198 14 push @name_parts,$firstpart;
199 14 foreach my $pos (2..$#blocks) {
200 4 ▶ next unless $blocks[$pos];
Branch: 1 branch
BranchTrueFalseTotal
unless $blocks[$pos] 0 4 4
201 4 push @name_parts," ";
202 4 ▶ if ($blocks[$pos] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocks[$pos] == 1) { } 0 4 4
203 0 push @name_parts,$NUMBER_NAMES{1} . " " . $UNITS_NAMES[$pos];
204 }
205 else {
206 4 my $part = convert_short($blocks[$pos]);
207 4 push @name_parts,$part. " " . $PART_NAMES[$pos];
208 }
209 }
210 14 my $tmp = join("",reverse(@name_parts));
211 14 $tmp =~ s/^\s*//;
212 14 $tmp =~ s/\s*$//;
213 14 $tmp =~ s!\s+! !g;
214 14 return $tmp;
215 }
216  
217 # }}}
218 # {{{ parse_num_string
219  
220 sub parse_num_string {
221 17 ▶ my $string = shift;
Subroutine parse_num_string: called CC=1 SCAR=0.0
Pod documented
222  
223 17 return { intpart => $string,
224 sign => '+',
225 fracpart => 0,
226 };
227 }
228  
229 # }}}
230  
231 # OO Methods
232 # {{{ new
233 sub new {
234 9 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
235 9 my $number = shift;
236 9 my @a = @_;
237 9 my %opts = (%DEFAULT_OPTS,@a);
238 9 return bless { number => $number,
239 opts => \%opts}, $class;
240 }
241  
242 # }}}
243 # {{{ get_string
244  
245 sub get_string {
246 9 ▶ my $self = shift;
Subroutine get_string: called CC=1 SCAR=0.0
Pod documented
247 9 return number_to_it($self->{number},$self->{opts});
248 }
249  
250 # }}}
251 # {{{ get_number
252  
253 sub get_number {
254 0 ▶ my $self = shift;
Subroutine get_number: not called CC=1 SCAR=6.9
Pod documented
255 return $self->{number}
256 0 }
257  
258 # }}}
259 # {{{ set_number
260  
261 sub set_number {
262 0 ▶ my $self = shift;
Subroutine set_number: not called CC=1 SCAR=6.9
Pod documented
263 0 $self->{number} = shift;
264 0 return $self;
265 }
266  
267 # }}}
268 # {{{ add
269  
270 sub add {
271 0 ▶ my $self = shift;
Subroutine add: not called CC=2 SCAR=17.9
Pod documented
272 0 my $num = shift;
273 0 ▶ $num = UNIVERSAL::isa($num,__PACKAGE__) ? $num->{number} : $num;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($num, __PACKAGE__) ? : 0 0 0
274 0 my $tmp = $self->{number} + $num;
275 return bless {number => $tmp,
276 0 opts => $self->{opts}},ref($self);
277 }
278  
279 # }}}
280 # {{{ mult
281  
282 sub mult {
283 0 ▶ my $self = shift;
Subroutine mult: not called CC=2 SCAR=17.9
Pod documented
284 0 my $num = shift;
285 0 ▶ $num = UNIVERSAL::isa($num,__PACKAGE__) ? $num->{number} : $num;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($num, __PACKAGE__) ? : 0 0 0
286 return bless {number => $self->{number} * $num,
287 0 opts => $self->{opts}},ref($self);
288 }
289  
290 # }}}
291 # {{{ div
292  
293 sub div {
294 0 ▶ my $self = shift;
Subroutine div: not called CC=3 SCAR=24.8
Pod documented
295 0 my $num = shift;
296 0 ▶ $num = UNIVERSAL::isa($num,__PACKAGE__) ? $num->{number} : $num;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($num, __PACKAGE__) ? : 0 0 0
297 0 my $inverted = shift;
298 my $tmp =
299 0 ▶ ($inverted) ? $num / $self->{number} : $self->{number} / $num;
Branch: 1 branch
BranchTrueFalseTotal
$inverted ? : 0 0 0
300 return bless {number => $tmp,
301 0 opts => $self->{opts}},ref($self);
302 }
303  
304 # }}}
305 # {{{ minus
306  
307 sub minus {
308 0 ▶ my $self = shift;
Subroutine minus: not called CC=3 SCAR=24.8
Pod documented
309 0 my $num = shift;
310 0 ▶ $num = UNIVERSAL::isa($num,__PACKAGE__) ? $num->{number} : $num;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($num, __PACKAGE__) ? : 0 0 0
311 0 my $inverted = shift;
312 my $tmp =
313 0 ▶ ($inverted) ? $num - $self->{number} : $self->{number} - $num;
Branch: 1 branch
BranchTrueFalseTotal
$inverted ? : 0 0 0
314 return bless {number => $tmp,
315 0 opts => $self->{opts}},ref($self);
316 }
317  
318 # }}}
319 # {{{ clone
320  
321 sub clone {
322 0 ▶ my $self = shift;
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
323 0 my $class = ref($self);
324 0 return bless {%$self},$class;
325 }
326  
327 # }}}
328  
329 1 1;
330 __END__
↑ Summary