blib/lib/Lingua/ENG/Numbers.pm

filter: Statementstmt 88.8% 136 / 153 Branchbran 59.6% 31 / 52 Conditioncond 48.8% 21 / 43 MC/DCmcdc 11.5% 3 / 26 Subroutinesub 100% 17 / 17 Podpod 100% 9 / 9 totaltotal 72.3% 217 / 300 SCAR 42.4
CC
43
Cov
76%
parse
19.7
string_to_number
11.9
import
6.6
CRAP 69.2
↑ Summary
1 # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8; -*-
2  
3 package Lingua::ENG::Numbers;
4 # ABSTRACT: Number 2 word conversion for ENG.
5  
6 # {{{ use block
7  
8 1 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 use vars qw(
15 @EXPORT_OK
16 $VERSION
17  
18 $MODE
19  
20 %INPUT_GROUP_DELIMITER
21 %INPUT_DECIMAL_DELIMITER
22 %OUTPUT_BLOCK_DELIMITER
23 %OUTPUT_GROUP_DELIMITER
24 %OUTPUT_NUMBER_DELIMITER
25 %OUTPUT_DECIMAL_DELIMITER
26  
27 %NUMBER_NAMES
28 %SIGN_NAMES
29  
30 $TRUE
31 $FALSE
32 $SIGN_POSITIVE
33 $SIGN_NEGATIVE
34 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # }}}
37 # {{{ variables declaration
38  
39 1 our $VERSION = 0.1106;
40  
41 BEGIN {
42  
43 # Exporter Stuff
44 1 ▶ @EXPORT_OK = qw(American British);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 # Constants
47 1 $TRUE = 1;
48 1 $FALSE = 0;
49 1 $SIGN_POSITIVE = 1;
50 1 $SIGN_NEGATIVE = -1;
51  
52 # Default Mode
53 1 $MODE = "American";
54  
55 # Delimiters
56 1 %OUTPUT_NUMBER_DELIMITER
57 = (
58 'American' => '-',
59 'British' => '-'
60 );
61  
62 1 %OUTPUT_GROUP_DELIMITER = (
63 'American' => ' ',
64 'British' => ' '
65 );
66  
67 1 %OUTPUT_BLOCK_DELIMITER = (
68 'American' => ', ',
69 'British' => ', '
70 );
71  
72 1 %OUTPUT_DECIMAL_DELIMITER = (
73 'American' => 'point ',
74 'British' => 'point '
75 );
76  
77  
78 1 %INPUT_GROUP_DELIMITER = (
79 'American' => ',',
80 'British' => '.'
81 );
82  
83 1 %INPUT_DECIMAL_DELIMITER = (
84 'American' => '.',
85 'British' => ','
86 );
87  
88  
89 # Low-Level Names
90 1 %SIGN_NAMES = (
91 'American' => {
92 $SIGN_POSITIVE => '',
93 $SIGN_NEGATIVE => 'Negative'
94 },
95 'British' => {
96 $SIGN_POSITIVE => '',
97 $SIGN_NEGATIVE => 'Negative'
98 }
99 );
100  
101 1 %NUMBER_NAMES = (
102 'American' => {
103 0 => 'Zero',
104 1 => 'One',
105 2 => 'Two',
106 3 => 'Three',
107 4 => 'Four',
108 5 => 'Five',
109 6 => 'Six',
110 7 => 'Seven',
111 8 => 'Eight',
112 9 => 'Nine',
113 10 => 'Ten',
114 11 => 'Eleven',
115 12 => 'Twelve',
116 13 => 'Thirteen',
117 14 => 'Fourteen',
118 15 => 'Fifteen',
119 16 => 'Sixteen',
120 17 => 'Seventeen',
121 18 => 'Eighteen',
122 19 => 'Nineteen',
123 20 => 'Twenty',
124 30 => 'Thirty',
125 40 => 'Fourty',
126 50 => 'Fifty',
127 60 => 'Sixty',
128 70 => 'Seventy',
129 80 => 'Eighty',
130 90 => 'Ninety',
131 10**2 => 'Hundred',
132 10**3 => 'Thousand',
133 10**6 => 'Million',
134 10**9 => 'Billion',
135 10**12 => 'Trillion',
136 10**15 => 'Quadrillion',
137 10**18 => 'Quintillion',
138 10**21 => 'Sextillion',
139 10**24 => 'Septillion',
140 10**27 => 'Octillion',
141 10**30 => 'Nonillian',
142 10**33 => 'Decillion',
143 10**36 => 'Undecillion',
144 10**39 => 'Duodecillion',
145 10**42 => 'Tredecillion',
146 10**45 => 'Quattuordecillion',
147 10**48 => 'Quindecillion',
148 10**51 => 'Sexdecillion',
149 10**54 => 'Septendecillion',
150 10**57 => 'Octodecillion',
151 10**60 => 'Novemdecillion',
152 10**63 => 'Vigintillion'
153 },
154 'British' => {
155 0 => 'Zero',
156 1 => 'One',
157 2 => 'Two',
158 3 => 'Three',
159 4 => 'Four',
160 5 => 'Five',
161 6 => 'Six',
162 7 => 'Seven',
163 8 => 'Eight',
164 9 => 'Nine',
165 10 => 'Ten',
166 11 => 'Eleven',
167 12 => 'Twelve',
168 13 => 'Thirteen',
169 14 => 'Fourteen',
170 15 => 'Fifteen',
171 16 => 'Sixteen',
172 17 => 'Seventeen',
173 18 => 'Eighteen',
174 19 => 'Nineteen',
175 20 => 'Twenty',
176 30 => 'Thirty',
177 40 => 'Fourty',
178 50 => 'Fifty',
179 60 => 'Sixty',
180 70 => 'Seventy',
181 80 => 'Eighty',
182 90 => 'Ninety',
183 10**2 => 'Hundred',
184 10**3 => 'Thousand',
185 10**6 => 'Million',
186 10**9 => 'Milliard',
187 10**12 => 'Billion',
188 10**15 => 'Billiard',
189 10**18 => 'Trillion',
190 10**21 => 'Trilliard',
191 10**24 => 'Quadrillion',
192 10**27 => 'Quadrilliard',
193 10**30 => 'Quintillion',
194 10**33 => 'Quintilliard',
195 10**36 => 'Sextillion',
196 10**39 => 'Sextilliard',
197 10**42 => 'Septillion',
198 10**45 => 'Septilliard',
199 10**48 => 'Octillion',
200 10**51 => 'Octilliard',
201 10**54 => 'Nonillian',
202 10**57 => 'Nonilliard',
203 10**60 => 'Decillion',
204 10**63 => 'Decilliard'
205 }
206 );
207 }
208  
209 # }}}
210  
211 # Exporter Routines
212 # {{{ import
213  
214 sub import {
215 2 ▶ my $module = shift;
Subroutine import: called CC=5 SCAR=18.9
216 2 ▶ my $tag = shift // 'American';
Condition100%2/2
shift() // "American"
l!l
11
MC/DC100%1/1
shift() // "American"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
217 2 ▶ if (($tag eq 'American') || ($tag eq 'British')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'American' or $tag eq 'British') { } 2 0 2
Condition33%1/3
$tag eq "American" or $tag eq "British"
l!l&&r!l&&!r
200
MC/DC0%0/2
$tag eq "American" or $tag eq "British"
$tag eq "American" $tag eq "British"
Truth table33%1/3
A or B
A= $tag eq "American"B= $tag eq "British"
ABresult
1X1
011
000
218 2 $MODE = $tag;
219 }
220 else {
221 0 ▶ croak "Error: $module does not support tag: '$tag'.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $tag 0 0 0
222 if ($tag);
223 }
224  
225 2 return;
226 }
227  
228 # }}}
229  
230 # Math Routines
231 # {{{ pow10Block
232  
233 sub pow10Block {
234 6 ▶ my ($number) = @_;
Subroutine pow10Block: called CC=2 SCAR=6.9
Pod documented
235 6 ▶ if ($number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number) { } 5 1 6
236 5 return (int(pow10($number) / 3) * 3);
237 }
238 else {
239 1 return 0;
240 }
241 }
242  
243 # }}}
244 # {{{ pow10
245  
246 sub pow10 {
247 5 ▶ my ($number) = @_;
Subroutine pow10: called CC=1 SCAR=0.0
Pod documented
248 5 return (length $number) - 1;
249 }
250  
251 # }}}
252  
253 # Numeric String Parsing Routines
254 # {{{ string_to_number
255  
256 sub string_to_number {
257 2 ▶ my $numberString = shift // return '';
Subroutine string_to_number: called CC=8 SCAR=24.8
Pod documented
Condition50%1/2
shift() // (return "")
l!l
20
MC/DC0%0/1
shift() // (return "")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
258  
259 # Strip out delimiters
260 2 $numberString =~ s/\Q$INPUT_GROUP_DELIMITER{$MODE}\E//g;
261  
262 2 my $sign = $SIGN_POSITIVE;
263 2 ▶ if ($numberString =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numberString =~ /^-/) 0 2 2
264 0 $numberString =~ s/^-//;
265 0 $sign = $SIGN_NEGATIVE;
266 }
267  
268 2 ▶ if (length($numberString)>1) { ### VSM 0.02 - Solve zero case
Branch: 1 branch
BranchTrueFalseTotal
if (length $numberString > 1) 2 0 2
269 2 $numberString =~ s/^0+//g;
270 }
271  
272 2 my $number = '';
273 2 my $decimal = '';
274 2 ▶ if ($numberString =~ /(^.+)\Q$INPUT_DECIMAL_DELIMITER{$MODE}\E(.+$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numberString =~ /(^.+)\Q$Lingua::ENG::Numbers::INPUT_DECIMAL_DELIMITER{$Lingua::ENG::Numbers::MODE}\E(.+$)/) { } 0 2 2
275 0 ($number, $decimal) = ($1, $2);
276 }
277 else {
278 2 $number = $numberString;
279 }
280  
281 2 ▶ if ($number =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number =~ /\D/) 0 2 2
282 0 return ();
283 }
284 2 ▶ if ($decimal && ($decimal =~ /\D/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decimal and $decimal =~ /\D/) 0 2 2
Condition33%1/3
$decimal and $decimal =~ /\D/
!ll&&!rl&&r
200
MC/DC0%0/2
$decimal and $decimal =~ /\D/
$decimal $decimal =~ /\D/
Truth table33%1/3
A and B
A= $decimalB= $decimal =~ /\D/
ABresult
0X0
100
111
285 0 return ();
286 }
287  
288 2 return ($number, $decimal, $sign);
289 }
290  
291 # }}}
292 # {{{ parse_number
293  
294 sub parse_number {
295 2 ▶ my ($number) = @_;
Subroutine parse_number: called CC=4 SCAR=13.9
Pod documented
296  
297 2 ▶ if (! defined $number) { # VSM 0.02 - Number zero is not a valid condition
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $number) 0 2 2
298 0 return { '0' => $NUMBER_NAMES{$MODE}{0} };
299 }
300  
301 2 my %names;
302 2 my $powerOfTen = pow10Block($number);
303 2 ▶ while ($powerOfTen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($powerOfTen > 0) 4 2 6
304 4 my $factor = int($number / 10**$powerOfTen);
305 4 my $component = $factor * 10**$powerOfTen;
306 4 my $magnitude = $NUMBER_NAMES{$MODE}{10**$powerOfTen};
307 4 my $factorName = &parse_number_low($factor);
308  
309 4 $names{$component}{'factor'} = $factorName;
310 4 $names{$component}{'magnitude'} = $magnitude;
311  
312 4 $number -= $component;
313 4 $powerOfTen = pow10Block($number);
314 }
315  
316 2 ▶ if (defined $number) { # VSM 0.02 - Number zero is not a valid condition
Branch: 1 branch
BranchTrueFalseTotal
if (defined $number) 2 0 2
317 2 $names{'1'}{'factor'} = &parse_number_low($number);
318 2 $names{'1'}{'magnitude'} = '';
319 }
320  
321 2 return \%names;
322 }
323  
324 # }}}
325 # {{{ parse_number_low
326  
327 sub parse_number_low {
328 6 ▶ my ($number) = @_;
Subroutine parse_number_low: called CC=4 SCAR=13.9
Pod documented
329 6 my @names;
330  
331 6 ▶ if ($number >= 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 100) 3 3 6
332 3 my $hundreds = int($number / 10**2);
333 3 push @names, [ $NUMBER_NAMES{$MODE}{$hundreds}, $NUMBER_NAMES{$MODE}{10**2} ];
334 3 $number -= $hundreds * 10**2;
335 }
336  
337 6 ▶ if ($number >= 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 20) { } 3 3 6
338 3 my $tens = int($number / 10**1) * 10**1;
339 3 my $ones = $number - $tens;
340  
341 3 ▶ if ($ones) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ones) { } 2 1 3
342 2 push @names, [ $NUMBER_NAMES{$MODE}{$tens} , $NUMBER_NAMES{$MODE}{$ones} ];
343 }
344 else {
345 1 push @names, [ $NUMBER_NAMES{$MODE}{$tens} ];
346 }
347 }
348 else {
349 3 push @names, [ $NUMBER_NAMES{$MODE}{$number} ];
350 }
351  
352 6 return \@names;
353 }
354  
355 # }}}
356  
357  
358 # Class Methods
359 # {{{ new
360  
361 sub new {
362 4 ▶ my ($class, @initializer) = @_;
Subroutine new: called CC=4 SCAR=16.1
Pod documented
363  
364 4 ▶ if (! defined $class || ! $class) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class and $class) 0 4 4
Condition33%1/3
defined $class and $class
!ll&&!rl&&r
004
MC/DC0%0/2
defined $class and $class
defined $class $class
Truth table33%1/3
A and B
A= defined $classB= $class
ABresult
0X0
100
111
365 0 return ();
366 }
367  
368 4 my $self = {};
369 4 bless $self, $class;
370  
371 4 ▶ if (@initializer) {
Branch: 1 branch
BranchTrueFalseTotal
if (@initializer) 0 4 4
372 0 $self->parse(@initializer);
373 }
374  
375 4 return $self;
376 }
377  
378 # }}}
379 # {{{ do_get_string
380  
381 sub do_get_string {
382 2 ▶ my ($self, $block) = @_;
Subroutine do_get_string: called CC=6 SCAR=18.8
Pod documented
383  
384 2 ▶ if (! defined $self || ! $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and $self) 0 2 2
Condition33%1/3
defined $self and $self
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self and $self
defined $self $self
Truth table33%1/3
A and B
A= defined $selfB= $self
ABresult
0X0
100
111
385 0 return '';
386 }
387  
388 2 ▶ if (! defined $block || ! $block) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $block and $block) 0 2 2
Condition33%1/3
defined $block and $block
!ll&&!rl&&r
002
MC/DC0%0/2
defined $block and $block
defined $block $block
Truth table33%1/3
A and B
A= defined $blockB= $block
ABresult
0X0
100
111
389 0 return '';
390 }
391  
392 2 my @blockStrings;
393 2 my $number = $self->{'string_data'}{$block};
394 2 for my $component( sort {$b <=> $a } keys %{$number} ) {
395 6 my $magnitude = $$number{$component}{'magnitude'};
396 6 my $factor = $$number{$component}{'factor'};
397  
398 6 my @strings;
399 6 map { push @strings, join($OUTPUT_NUMBER_DELIMITER{$MODE}, @{$_}) } @{$factor};
400  
401 6 my $string = join($OUTPUT_GROUP_DELIMITER{$MODE}, @strings) . ' ' . $magnitude;
402 6 push @blockStrings, $string;
403 }
404  
405 2 my $blockString = join($OUTPUT_BLOCK_DELIMITER{$MODE}, @blockStrings);
406 2 $blockString =~ s{(?<=.),?\s?Zero}{}xmsg;
407  
408 2 return $blockString;
409 }
410  
411 # }}}
412 # {{{ parse
413  
414 sub parse {
415 4 ▶ my ($self, $numberString) = @_;
Subroutine parse: called CC=13 SCAR=29.8
Pod documented
416  
417 4 ▶ if ( $numberString && $numberString =~ m{\A\d+\.?\d*?e\+\d+\z}xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numberString and $numberString =~ /\A\d+\.?\d*?e\+\d+\z/msx) 0 4 4
Condition66%2/3
$numberString and $numberString =~ /\A\d+\.?\d*?e\+\d+\z/msx
!ll&&!rl&&r
130
MC/DC0%0/2
$numberString and $numberString =~ /\A\d+\.?\d*?e\+\d+\z/msx
$numberString $numberString =~ /\A\d+\.?\d*?e\+\d+\z/msx
Truth table66%2/3
A and B
A= $numberStringB= $numberString =~ /\A\d+\.?\d*?e\+\d+\z/msx
ABresult
0X0
100
111
418 0 croak q{You shouldn't use scientific notation};
419 }
420  
421 4 ▶ croak 'You should specify a number from interval [0, 10^66)'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $numberString && $numberString =~ /\A\d+\z/msx or $numberString < 0 or $numberString >= "1e+66" 1 2 3
Condition77%7/9
not defined $numberString && $numberString =~ /\A\d+\z/msx or $numberString < 0 or $numberString >= "1e+66"
l!l&&r!l&&!r
112
not defined $numberString && $numberString =~ /\A\d+\z/msx or $numberString < 0
l!l&&r!l&&!r
103
defined $numberString && $numberString =~ /\A\d+\z/msx
!ll&&!rl&&r
103
MC/DC50%2/4
not defined $numberString && $numberString =~ /\A\d+\z/msx or $numberString < 0 or $numberString >= "1e+66"
defined $numberString $numberString =~ /\A\d+\z/msx $numberString < 0 $numberString >= "1e+66"
Truth table60%3/5
not(A && B) or C or D
A= defined $numberStringB= $numberString =~ /\A\d+\z/msxC= $numberString < 0D= $numberString >= "1e+66"
ABCDresult
0XXX1
10XX1
111X1
11011
11000
422 if !defined $numberString
423 || $numberString !~ m{\A\d+\z}xms
424 || $numberString < 0
425 || $numberString >= 10 ** 66;
426  
427 2 ▶ if (! defined $self || ! $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and $self) 0 2 2
Condition33%1/3
defined $self and $self
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self and $self
defined $self $self
Truth table33%1/3
A and B
A= defined $selfB= $self
ABresult
0X0
100
111
428 0 return $FALSE;
429 }
430  
431 2 my ($number, $decimal, $sign) = &string_to_number($numberString);
432  
433 2 $self->{'numeric_data'}{'number'} = $number;
434 2 $self->{'numeric_data'}{'decimal'} = $decimal;
435 2 $self->{'numeric_data'}{'sign'} = $sign;
436  
437 2 ▶ if (defined $number && length($number)>0) { # VSM 0.02 - Number zero is not a valid condition
Branch: 1 branch
BranchTrueFalseTotal
if (defined $number and length $number > 0) 2 0 2
Condition33%1/3
defined $number and length $number > 0
!ll&&!rl&&r
002
MC/DC0%0/2
defined $number and length $number > 0
defined $number length $number > 0
Truth table33%1/3
A and B
A= defined $numberB= length $number > 0
ABresult
0X0
100
111
438 2 $self->{'string_data'}{'number'} = &parse_number($number);
439 2 $self->{'string_data'}{'sign'} = $SIGN_NAMES{$MODE}{$sign};
440 }
441  
442 2 ▶ if (defined $decimal && $decimal) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decimal and $decimal) 0 2 2
Condition33%1/3
defined $decimal and $decimal
!ll&&!rl&&r
020
MC/DC0%0/2
defined $decimal and $decimal
defined $decimal $decimal
Truth table33%1/3
A and B
A= defined $decimalB= $decimal
ABresult
0X0
100
111
443 0 $self->{'string_data'}{'decimal'} = &parse_number($decimal);
444 }
445  
446 2 return $TRUE;
447 }
448  
449 # }}}
450 # {{{ get_string
451  
452 sub get_string {
453 2 ▶ my ($self) = @_;
Subroutine get_string: called CC=5 SCAR=18.5
Pod documented
454  
455 2 ▶ if (! defined $self || ! $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and $self) 0 2 2
Condition33%1/3
defined $self and $self
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self and $self
defined $self $self
Truth table33%1/3
A and B
A= defined $selfB= $self
ABresult
0X0
100
111
456 0 return '';
457 }
458  
459 2 my @strings;
460 2 push @strings, $self->do_get_string('number');
461  
462 2 ▶ if ($self->{'string_data'}{'decimal'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"string_data"}{"decimal"}) 0 2 2
463 0 push @strings, $self->do_get_string('decimal');
464 }
465  
466 2 my $string = join($OUTPUT_DECIMAL_DELIMITER{$MODE}, @strings);
467 2 ▶ if ($self->{'string_data'}{'sign'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"string_data"}{"sign"}) 0 2 2
468 0 $string = $self->{'string_data'}{'sign'} . " $string";
469 }
470  
471 2 $string =~ s/\s+$//;
472 2 return $string;
473 }
474  
475 # }}}
476  
477 1 1;
478  
479 __END__
↑ Summary