blib/lib/Lingua/LTZ/Num2Word.pm

filter: Statementstmt 98.5% 67 / 68 Branchbran 95.6% 44 / 46 Conditioncond 66.6% 18 / 27 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 90.0% 9 / 10 Podpod 100% 2 / 2 totaltotal 86.9% 147 / 169 CC 33 SCAR 31.8
CC
33
Cov
91%
num2ltz_cardinal
32.0
_connector
3.0
capabilities
2.0
CRAP 24.1
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2  
3 package Lingua::LTZ::Num2Word;
4 # ABSTRACT: Number to word conversion in Luxembourgish
5  
6 1 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # {{{ use block
11  
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # }}}
17 # {{{ variable declarations
18  
19 1 my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present';
20 1 our $VERSION = '0.2603270';
21  
22 # }}}
23  
24 # {{{ num2ltz_cardinal convert number to text
25  
26 sub num2ltz_cardinal :Export {
27 49 ▶ my $positive = shift;
Subroutine num2ltz_cardinal: called CC=31 SCAR=34.7
Pod documented
28  
29 49 ▶ croak 'You should specify a number from interval [0, 999_999_999]'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999 1 47 48
Condition77%7/9
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999
l!l&&r!l&&!r
1147
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0
l!l&&r!l&&!r
1048
defined $positive && $positive =~ /\A\d+\z/msux
!ll&&!rl&&r
1048
MC/DC50%2/4
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999
defined $positive $positive =~ /\A\d+\z/msux $positive < 0 $positive > 999999999
Truth table60%3/5
not(A && B) or C or D
A= defined $positiveB= $positive =~ /\A\d+\z/msuxC= $positive < 0D= $positive > 999999999
ABCDresult
0XXX1
10XX1
111X1
11011
11000
30 if !defined $positive
31 || $positive !~ m{\A\d+\z}xms
32 || $positive < 0
33 || $positive > 999_999_999;
34  
35 # 0..12 are irregular standalone forms
36 47 my @tokens1 = qw(null een zwee dräi véier fënnef sechs siwen aacht néng zéng eelef zwielef);
37  
38 # tens 20..90 (index 0=20, 1=30, ...)
39 47 my @tokens2 = qw(zwanzeg drësseg véierzeg fofzeg sechzeg siwwenzeg achtzeg nonzeg);
40  
41 # standalone 0
42 47 ▶ return 'null' if $positive == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 0 1 46 47
43  
44 # standalone 1 has trailing 't'
45 46 ▶ return 'eent' if $positive == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 1 1 45 46
46  
47 45 ▶ return $tokens1[$positive] if ($positive >= 2 && $positive < 13); # 2 .. 12
Branch: 1 branch
BranchTrueFalseTotal
if $positive >= 2 and $positive < 13 10 35 45
Condition66%2/3
$positive >= 2 and $positive < 13
!ll&&!rl&&r
03510
MC/DC50%1/2
$positive >= 2 and $positive < 13
$positive >= 2 $positive < 13
Truth table66%2/3
A and B
A= $positive >= 2B= $positive < 13
ABresult
0X0
100
111
48 35 ▶ return 'fofzéng' if ($positive == 15); # 15 exception
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 15 1 34 35
49 34 ▶ return 'siechzéng' if ($positive == 16); # 16 exception
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 16 1 33 34
50 33 ▶ return 'siwwenzéng' if ($positive == 17); # 17 exception
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 17 1 32 33
51 32 ▶ return 'uechtzéng' if ($positive == 18); # 18 exception
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 18 1 31 32
52 31 ▶ return 'nonzéng' if ($positive == 19); # 19 exception
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 19 2 29 31
53 29 ▶ return $tokens1[$positive-10] . 'zéng' if ($positive > 12 && $positive < 20); # 13, 14
Branch: 1 branch
BranchTrueFalseTotal
if $positive > 12 and $positive < 20 1 28 29
Condition66%2/3
$positive > 12 and $positive < 20
!ll&&!rl&&r
0281
MC/DC50%1/2
$positive > 12 and $positive < 20
$positive > 12 $positive < 20
Truth table66%2/3
A and B
A= $positive > 12B= $positive < 20
ABresult
0X0
100
111
54  
55 28 my $out; # string for return value construction
56 my $one_idx; # index for array
57 28 my $remain; # remainder
58  
59 28 ▶ if ($positive > 19 && $positive < 100) { # 20 .. 99
Branch: 4 branches
BranchTrueFalseTotal
if ($positive > 19 and $positive < 100) { } 15 13 28
elsif ($positive >= 100 and $positive < 1000) { } 7 6 13
elsif ($positive > 999 and $positive < 1000000) { } 4 2 6
elsif ($positive > 999999 and $positive < 1000000000) { } 2 0 2
Condition66%2/3
$positive > 19 and $positive < 100
!ll&&!rl&&r
01315
MC/DC50%1/2
$positive > 19 and $positive < 100
$positive > 19 $positive < 100
Truth table66%2/3
A and B
A= $positive > 19B= $positive < 100
ABresult
0X0
100
111
60 15 $one_idx = int ($positive / 10);
61 15 $remain = $positive % 10;
62  
63 15 ▶ if ($remain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain) { } 14 1 15
64 14 my $unit = $tokens1[$remain];
65 14 my $tens = $tokens2[$one_idx - 2];
66 14 my $connector = _connector($tens);
67 14 $out = $unit . $connector . $tens;
68 }
69 else {
70 1 $out = $tokens2[$one_idx - 2];
71 }
72 }
73 ▶ elsif ($positive >= 100 && $positive < 1000) { # 100 .. 999
Condition66%2/3
$positive >= 100 and $positive < 1000
!ll&&!rl&&r
067
MC/DC50%1/2
$positive >= 100 and $positive < 1000
$positive >= 100 $positive < 1000
Truth table66%2/3
A and B
A= $positive >= 100B= $positive < 1000
ABresult
0X0
100
111
74 7 $one_idx = int ($positive / 100);
75 7 $remain = $positive % 100;
76  
77 7 ▶ $out = ($one_idx == 1 ? '' : $tokens1[$one_idx]) . 'honnert';
Branch: 1 branch
BranchTrueFalseTotal
$one_idx == 1 ? : 2 5 7
78 7 ▶ $out .= $remain ? num2ltz_cardinal($remain) : '';
Branch: 1 branch
BranchTrueFalseTotal
$remain ? : 4 3 7
79 }
80 ▶ elsif ($positive > 999 && $positive < 1_000_000) { # 1000 .. 999_999
Condition66%2/3
$positive > 999 and $positive < 1000000
!ll&&!rl&&r
024
MC/DC50%1/2
$positive > 999 and $positive < 1000000
$positive > 999 $positive < 1000000
Truth table66%2/3
A and B
A= $positive > 999B= $positive < 1000000
ABresult
0X0
100
111
81 4 $one_idx = int ($positive / 1000);
82 4 $remain = $positive % 1000;
83  
84 4 ▶ $out = ($one_idx == 1 ? '' : num2ltz_cardinal($one_idx)) . 'dausend';
Branch: 1 branch
BranchTrueFalseTotal
$one_idx == 1 ? : 2 2 4
85 4 ▶ $out .= $remain ? num2ltz_cardinal($remain) : '';
Branch: 1 branch
BranchTrueFalseTotal
$remain ? : 2 2 4
86 }
87 ▶ elsif ( $positive > 999_999
Condition33%1/3
$positive > 999999 and $positive < 1000000000
!ll&&!rl&&r
002
MC/DC0%0/2
$positive > 999999 and $positive < 1000000000
$positive > 999999 $positive < 1000000000
Truth table33%1/3
A and B
A= $positive > 999999B= $positive < 1000000000
ABresult
0X0
100
111
88 && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999
89 2 $one_idx = int ($positive / 1000000);
90 2 $remain = $positive % 1000000;
91  
92 # "eng Millioun" for 1M, "zwee Milliounen" for 2M+
93 2 ▶ if ($one_idx == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($one_idx == 1) { } 1 1 2
94 1 $out = 'eng Millioun';
95 }
96 else {
97 1 $out = num2ltz_cardinal($one_idx) . ' Milliounen';
98 }
99 2 ▶ $out .= $remain ? ' ' . num2ltz_cardinal($remain) : '';
Branch: 1 branch
BranchTrueFalseTotal
$remain ? : 0 2 2
100 }
101  
102 28 return $out;
103 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 # }}}
106 # {{{ _connector apply n-rule (Eifel rule) for 'an'
107  
108 sub _connector {
109 14 ▶ my $tens = shift;
Subroutine _connector: called CC=3 SCAR=11.0
110  
111 # The Eifel rule (n-Regel): final 'n' is dropped before a consonant,
112 # EXCEPT before n, d, t, z, h.
113 # Applied to the connector 'an' before the tens word.
114 14 my $first_char = substr($tens, 0, 1);
115  
116 # Before a vowel: keep 'n'
117 14 ▶ return 'an' if $first_char =~ m/[aeiouäëéAEIOU]/;
Branch: 1 branch
BranchTrueFalseTotal
if $first_char =~ /[aeiou\344\353\351AEIOU]/u 1 13 14
118  
119 # Before consonants n, d, t, z, h: keep 'n'
120 13 ▶ return 'an' if $first_char =~ m/[ndtzh]/;
Branch: 1 branch
BranchTrueFalseTotal
if $first_char =~ /[ndtzh]/u 8 5 13
121  
122 # Before all other consonants: drop 'n'
123 5 return 'a';
124 }
125  
126 # }}}
127 # {{{ capabilities declare supported features
128  
129 sub capabilities {
130 return {
131 0 ▶ cardinal => 1,
Subroutine capabilities: not called CC=1 SCAR=6.9
Pod documented
132 ordinal => 0,
133 };
134 }
135  
136 # }}}
137 1 1;
138  
139 __END__
↑ Summary