blib/lib/Lingua/DAN/Num2Word.pm

filter: Statementstmt 33.3% 32 / 96 Branchbran 3.2% 2 / 62 Conditioncond 11.1% 4 / 36 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 81.8% 9 / 11 Podpod 66.6% 2 / 3 totaltotal 21.4% 49 / 228 CC 44 SCAR 58.8
CC
44
Cov
20%
num2dan_ordinal
506.0
num2dan_cardinal
352.1
capabilities
2.0
CRAP 356.3
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2  
3 package Lingua::DAN::Num2Word;
4 # ABSTRACT: Number to word conversion in Danish
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 # {{{ num2dan_cardinal convert number to text
25  
26 sub num2dan_cardinal :Export {
27 2 ▶ my $positive = shift;
Subroutine num2dan_cardinal: called CC=23 SCAR=58.6
Pod documented
28  
29 2 ▶ 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 0 2 2
Condition33%3/9
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999
l!l&&r!l&&!r
002
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0
l!l&&r!l&&!r
002
defined $positive && $positive =~ /\A\d+\z/msux
!ll&&!rl&&r
002
MC/DC0%0/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 table20%1/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 2 my @tokens1 = qw(nul en to tre fire fem seks syv otte ni ti elleve tolv);
36 2 my @teens = qw(tretten fjorten femten seksten sytten atten nitten);
37 2 my @tokens2 = qw(tyve tredive fyrre halvtreds tres halvfjerds firs halvfems);
38  
39 2 ▶ return $tokens1[$positive] if ($positive >= 0 && $positive < 13); # 0 .. 12
Branch: 1 branch
BranchTrueFalseTotal
if $positive >= 0 and $positive < 13 2 0 2
Condition33%1/3
$positive >= 0 and $positive < 13
!ll&&!rl&&r
002
MC/DC0%0/2
$positive >= 0 and $positive < 13
$positive >= 0 $positive < 13
Truth table33%1/3
A and B
A= $positive >= 0B= $positive < 13
ABresult
0X0
100
111
40 0 ▶ return $teens[$positive - 13] if ($positive > 12 && $positive < 20); # 13 .. 19
Branch: 1 branch
BranchTrueFalseTotal
if $positive > 12 and $positive < 20 0 0 0
Condition0%0/3
$positive > 12 and $positive < 20
!ll&&!rl&&r
000
MC/DC0%0/2
$positive > 12 and $positive < 20
$positive > 12 $positive < 20
Truth table0%0/3
A and B
A= $positive > 12B= $positive < 20
ABresult
0X0
100
111
41  
42 0 my $out; # string for return value construction
43 my $one_idx; # index for tokens1 array
44 0 my $remain; # remainder
45  
46 0 ▶ if ($positive > 19 && $positive < 100) { # 20 .. 99
Branch: 4 branches
BranchTrueFalseTotal
if ($positive > 19 and $positive < 100) { } 0 0 0
elsif ($positive > 99 and $positive < 1000) { } 0 0 0
elsif ($positive > 999 and $positive < 1000000) { } 0 0 0
elsif ($positive > 999999 and $positive < 1000000000) { } 0 0 0
Condition0%0/3
$positive > 19 and $positive < 100
!ll&&!rl&&r
000
MC/DC0%0/2
$positive > 19 and $positive < 100
$positive > 19 $positive < 100
Truth table0%0/3
A and B
A= $positive > 19B= $positive < 100
ABresult
0X0
100
111
47 0 $one_idx = int ($positive / 10);
48 0 $remain = $positive % 10;
49  
50 0 ▶ $out = "$tokens1[$remain]og" if ($remain);
Branch: 1 branch
BranchTrueFalseTotal
if $remain 0 0 0
51 0 $out .= $tokens2[$one_idx - 2];
52 }
53 ▶ elsif ($positive > 99 && $positive < 1000) { # 100 .. 999
Condition0%0/3
$positive > 99 and $positive < 1000
!ll&&!rl&&r
000
MC/DC0%0/2
$positive > 99 and $positive < 1000
$positive > 99 $positive < 1000
Truth table0%0/3
A and B
A= $positive > 99B= $positive < 1000
ABresult
0X0
100
111
54 0 $one_idx = int ($positive / 100);
55 0 $remain = $positive % 100;
56  
57 # Danish uses "et" (neuter) before hundrede, not "en" (common)
58 0 ▶ my $prefix = $one_idx == 1 ? 'et' : $tokens1[$one_idx];
Branch: 1 branch
BranchTrueFalseTotal
$one_idx == 1 ? : 0 0 0
59 0 $out = "${prefix}hundrede";
60 0 ▶ $out .= $remain ? num2dan_cardinal($remain) : '';
Branch: 1 branch
BranchTrueFalseTotal
$remain ? : 0 0 0
61 }
62 ▶ elsif ($positive > 999 && $positive < 1_000_000) { # 1000 .. 999_999
Condition0%0/3
$positive > 999 and $positive < 1000000
!ll&&!rl&&r
000
MC/DC0%0/2
$positive > 999 and $positive < 1000000
$positive > 999 $positive < 1000000
Truth table0%0/3
A and B
A= $positive > 999B= $positive < 1000000
ABresult
0X0
100
111
63 0 $one_idx = int ($positive / 1000);
64 0 $remain = $positive % 1000;
65  
66 0 $out = num2dan_cardinal($one_idx).'tusind';
67 0 ▶ $out .= $remain ? num2dan_cardinal($remain) : '';
Branch: 1 branch
BranchTrueFalseTotal
$remain ? : 0 0 0
68 }
69 ▶ elsif ( $positive > 999_999
Condition0%0/3
$positive > 999999 and $positive < 1000000000
!ll&&!rl&&r
000
MC/DC0%0/2
$positive > 999999 and $positive < 1000000000
$positive > 999999 $positive < 1000000000
Truth table0%0/3
A and B
A= $positive > 999999B= $positive < 1000000000
ABresult
0X0
100
111
70 && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999
71 0 $one_idx = int ($positive / 1000000);
72 0 $remain = $positive % 1000000;
73  
74 0 $out = num2dan_cardinal($one_idx) . ' million';
75 0 ▶ $out .= 'er' if ($one_idx > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $one_idx > 1 0 0 0
76 0 ▶ $out .= $remain ? ' ' . num2dan_cardinal($remain) : '';
Branch: 1 branch
BranchTrueFalseTotal
$remain ? : 0 0 0
77 }
78  
79 0 return $out;
80 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 # }}}
83  
84 # {{{ num2dan_ordinal convert number to ordinal text
85  
86 sub num2dan_ordinal :Export {
87 0 ▶ my $number = shift;
Subroutine num2dan_ordinal: not called CC=22 SCAR=62.3
Pod documented
88  
89 0 ▶ croak 'You should specify a number from interval [1, 999_999_999]'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $number && $number =~ /\A\d+\z/msux or $number < 1 or $number > 999999999 0 0 0
Condition0%0/9
not defined $number && $number =~ /\A\d+\z/msux or $number < 1 or $number > 999999999
l!l&&r!l&&!r
000
not defined $number && $number =~ /\A\d+\z/msux or $number < 1
l!l&&r!l&&!r
000
defined $number && $number =~ /\A\d+\z/msux
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $number && $number =~ /\A\d+\z/msux or $number < 1 or $number > 999999999
defined $number $number =~ /\A\d+\z/msux $number < 1 $number > 999999999
Truth table0%0/5
not(A && B) or C or D
A= defined $numberB= $number =~ /\A\d+\z/msuxC= $number < 1D= $number > 999999999
ABCDresult
0XXX1
10XX1
111X1
11011
11000
90 if !defined $number
91 || $number !~ m{\A\d+\z}xms
92 || $number < 1
93 || $number > 999_999_999;
94  
95 # Fully irregular 1-3
96 0 ▶ return 'første' if $number == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 1 0 0 0
97 0 ▶ return 'anden' if $number == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 2 0 0 0
98 0 ▶ return 'tredje' if $number == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 3 0 0 0
99  
100 # Irregular 4-12
101 0 my %irregular = (
102 4 => 'fjerde',
103 5 => 'femte',
104 6 => 'sjette',
105 7 => 'syvende',
106 8 => 'ottende',
107 9 => 'niende',
108 10 => 'tiende',
109 11 => 'ellevte',
110 12 => 'tolvte',
111 );
112 0 ▶ return $irregular{$number} if exists $irregular{$number};
Branch: 1 branch
BranchTrueFalseTotal
if exists $irregular{$number} 0 0 0
113  
114 # 13-19: teens
115 0 my %teens = (
116 13 => 'trettende',
117 14 => 'fjortende',
118 15 => 'femtende',
119 16 => 'sekstende',
120 17 => 'syttende',
121 18 => 'attende',
122 19 => 'nittende',
123 );
124 0 ▶ return $teens{$number} if exists $teens{$number};
Branch: 1 branch
BranchTrueFalseTotal
if exists $teens{$number} 0 0 0
125  
126 # Tens ordinal forms (exact multiples)
127 0 my %tens_ord = (
128 20 => 'tyvende',
129 30 => 'tredivte',
130 40 => 'fyrretyvende',
131 50 => 'halvtredsindstyvende',
132 60 => 'tresindstyvende',
133 70 => 'halvfjerdsindstyvende',
134 80 => 'firsindstyvende',
135 90 => 'halvfemsindstyvende',
136 );
137  
138 # 20-99
139 0 ▶ if ($number < 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 100) 0 0 0
140 0 my $tens = int($number / 10) * 10;
141 0 my $ones = $number % 10;
142 0 ▶ return $tens_ord{$tens} if $ones == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ones == 0 0 0 0
143  
144 # Compound: cardinal ones + "og" + tens ordinal
145 0 my @tokens1 = qw(nul en to tre fire fem seks syv otte ni);
146 0 return $tokens1[$ones] . 'og' . $tens_ord{$tens};
147 }
148  
149 # 100-999
150 0 ▶ if ($number < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 1000) 0 0 0
151 0 my $hundreds = int($number / 100);
152 0 my $remain = $number % 100;
153  
154 0 ▶ if ($remain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain == 0) 0 0 0
155 0 ▶ my $prefix = $hundreds == 1 ? 'et' : num2dan_cardinal($hundreds);
Branch: 1 branch
BranchTrueFalseTotal
$hundreds == 1 ? : 0 0 0
156 0 return "${prefix}hundrede";
157 }
158 0 ▶ my $prefix = $hundreds == 1 ? 'et' : num2dan_cardinal($hundreds);
Branch: 1 branch
BranchTrueFalseTotal
$hundreds == 1 ? : 0 0 0
159 0 return "${prefix}hundrede" . num2dan_ordinal($remain);
160 }
161  
162 # 1000-999_999
163 0 ▶ if ($number < 1_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 1000000) 0 0 0
164 0 my $remain = $number % 1000;
165 0 ▶ if ($remain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain == 0) 0 0 0
166 0 return num2dan_cardinal(int($number / 1000)) . 'tusinde';
167 }
168 0 return num2dan_cardinal(int($number / 1000)) . 'tusind' . num2dan_ordinal($remain);
169 }
170  
171 # 1_000_000 - 999_999_999
172 0 ▶ if ($number < 1_000_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 1000000000) 0 0 0
173 0 my $millions = int($number / 1_000_000);
174 0 my $remain = $number % 1_000_000;
175  
176 0 ▶ if ($remain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain == 0) 0 0 0
177 0 my $base = num2dan_cardinal($millions);
178 0 ▶ my $suf = $millions > 1 ? 'er' : '';
Branch: 1 branch
BranchTrueFalseTotal
$millions > 1 ? : 0 0 0
179 0 return "$base million${suf}te";
180 }
181 0 my $base = num2dan_cardinal($millions);
182 0 ▶ my $suf = $millions > 1 ? 'er' : '';
Branch: 1 branch
BranchTrueFalseTotal
$millions > 1 ? : 0 0 0
183 0 return "$base million${suf} " . num2dan_ordinal($remain);
184 }
185  
186 0 return;
187 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
188  
189 # }}}
190  
191 # {{{ capabilities declare supported features
192  
193 sub capabilities {
194 return {
195 0 ▶ cardinal => 1,
Subroutine capabilities: not called CC=1 SCAR=6.9
Pod undocumented
196 ordinal => 1,
197 };
198 }
199  
200 # }}}
201 1 1;
202  
203 __END__
↑ Summary