blib/lib/Lingua/FIN/Num2Word.pm

filter: Statementstmt 29.4% 30 / 102 Branchbran 2.8% 2 / 70 Conditioncond 7.0% 4 / 57 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 75.0% 9 / 12 Podpod 66.6% 2 / 3 totaltotal 16.9% 47 / 278 CC 55 SCAR 62.9
CC
55
Cov
16%
num2fin_ordinal
812.0
num2fin_cardinal
494.7
_fin_cardinal_prefix
12.0
CRAP 540.0
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2  
3 package Lingua::FIN::Num2Word;
4 # ABSTRACT: Number to word conversion in Finnish
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 # {{{ num2fin_cardinal convert number to text
25  
26 sub num2fin_cardinal :Export {
27 2 ▶ my $positive = shift;
Subroutine num2fin_cardinal: called CC=26 SCAR=62.0
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 @ones = qw(nolla yksi kaksi kolme neljä viisi kuusi seitsemän kahdeksan yhdeksän);
36  
37 2 ▶ return $ones[$positive] if ($positive >= 0 && $positive < 10);
Branch: 1 branch
BranchTrueFalseTotal
if $positive >= 0 and $positive < 10 2 0 2
Condition33%1/3
$positive >= 0 and $positive < 10
!ll&&!rl&&r
002
MC/DC0%0/2
$positive >= 0 and $positive < 10
$positive >= 0 $positive < 10
Truth table33%1/3
A and B
A= $positive >= 0B= $positive < 10
ABresult
0X0
100
111
38 0 ▶ return 'kymmenen' if ($positive == 10);
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 10 0 0 0
39 0 ▶ return $ones[$positive - 10] . 'toista' if ($positive > 10 && $positive < 20);
Branch: 1 branch
BranchTrueFalseTotal
if $positive > 10 and $positive < 20 0 0 0
Condition0%0/3
$positive > 10 and $positive < 20
!ll&&!rl&&r
000
MC/DC0%0/2
$positive > 10 and $positive < 20
$positive > 10 $positive < 20
Truth table0%0/3
A and B
A= $positive > 10B= $positive < 20
ABresult
0X0
100
111
40  
41 0 my $out;
42 my $remain;
43  
44 0 ▶ if ($positive > 19 && $positive < 100) { # 20 .. 99
Branch: 5 branches
BranchTrueFalseTotal
if ($positive > 19 and $positive < 100) { } 0 0 0
elsif ($positive == 100) { } 0 0 0
elsif ($positive > 100 and $positive < 1000) { } 0 0 0
elsif ($positive >= 1000 and $positive < 1000000) { } 0 0 0
elsif ($positive >= 1000000 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
45 0 my $tens_idx = int($positive / 10);
46 0 $remain = $positive % 10;
47  
48 0 $out = $ones[$tens_idx] . 'kymmentä';
49 0 ▶ $out .= $ones[$remain] if ($remain);
Branch: 1 branch
BranchTrueFalseTotal
if $remain 0 0 0
50 }
51 elsif ($positive == 100) { # 100
52 0 $out = 'sata';
53 }
54 ▶ elsif ($positive > 100 && $positive < 1000) { # 101 .. 999
Condition0%0/3
$positive > 100 and $positive < 1000
!ll&&!rl&&r
000
MC/DC0%0/2
$positive > 100 and $positive < 1000
$positive > 100 $positive < 1000
Truth table0%0/3
A and B
A= $positive > 100B= $positive < 1000
ABresult
0X0
100
111
55 0 my $hundreds = int($positive / 100);
56 0 $remain = $positive % 100;
57  
58 0 ▶ $out = $hundreds == 1 ? 'sata' : $ones[$hundreds] . 'sataa';
Branch: 1 branch
BranchTrueFalseTotal
$hundreds == 1 ? : 0 0 0
59 0 ▶ $out .= num2fin_cardinal($remain) if ($remain);
Branch: 1 branch
BranchTrueFalseTotal
if $remain 0 0 0
60 }
61 ▶ elsif ($positive >= 1000 && $positive < 1_000_000) { # 1000 .. 999_999
Condition0%0/3
$positive >= 1000 and $positive < 1000000
!ll&&!rl&&r
000
MC/DC0%0/2
$positive >= 1000 and $positive < 1000000
$positive >= 1000 $positive < 1000000
Truth table0%0/3
A and B
A= $positive >= 1000B= $positive < 1000000
ABresult
0X0
100
111
62 0 my $thousands = int($positive / 1000);
63 0 $remain = $positive % 1000;
64  
65 0 ▶ $out = $thousands == 1 ? 'tuhat' : num2fin_cardinal($thousands) . 'tuhatta';
Branch: 1 branch
BranchTrueFalseTotal
$thousands == 1 ? : 0 0 0
66 0 ▶ $out .= num2fin_cardinal($remain) if ($remain);
Branch: 1 branch
BranchTrueFalseTotal
if $remain 0 0 0
67 }
68 ▶ elsif ($positive >= 1_000_000 && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999
Condition0%0/3
$positive >= 1000000 and $positive < 1000000000
!ll&&!rl&&r
000
MC/DC0%0/2
$positive >= 1000000 and $positive < 1000000000
$positive >= 1000000 $positive < 1000000000
Truth table0%0/3
A and B
A= $positive >= 1000000B= $positive < 1000000000
ABresult
0X0
100
111
69 0 my $millions = int($positive / 1_000_000);
70 0 $remain = $positive % 1_000_000;
71  
72 0 ▶ if ($millions == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($millions == 1) { } 0 0 0
73 0 $out = 'miljoona';
74 }
75 else {
76 0 $out = num2fin_cardinal($millions) . ' miljoonaa';
77 }
78 0 ▶ $out .= ' ' . num2fin_cardinal($remain) if ($remain);
Branch: 1 branch
BranchTrueFalseTotal
if $remain 0 0 0
79 }
80  
81 0 return $out;
82 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 # }}}
85  
86 # {{{ num2fin_ordinal convert number to ordinal text
87  
88 sub num2fin_ordinal :Export {
89 0 ▶ my $number = shift;
Subroutine num2fin_ordinal: not called CC=28 SCAR=67.0
Pod documented
90  
91 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
92 if !defined $number
93 || $number !~ m{\A\d+\z}xms
94 || $number < 1
95 || $number > 999_999_999;
96  
97 # In Finnish, ordinals transform ALL components of a compound number.
98 # 1st and 2nd are fully irregular; 3rd onward use the -s suffix
99 # on a (sometimes modified) stem.
100  
101 # Irregular/base ordinal forms for 1-10
102 0 my @ones_ord = (
103 q{}, # 0 unused
104 'ensimmäinen', # 1
105 'toinen', # 2
106 'kolmas', # 3
107 'neljäs', # 4
108 'viides', # 5
109 'kuudes', # 6
110 'seitsemäs', # 7
111 'kahdeksas', # 8
112 'yhdeksäs', # 9
113 );
114  
115 # Ordinal forms used as prefix in compound numbers (stem form)
116 # In compounds like 21st, 1st becomes ensimmäinen, 2nd becomes toinen
117 # but in teens (11-19), 1st->yhdes, 2nd->kahdes
118 0 my @ones_compound = (
119 q{}, # 0
120 'yhdes', # 1 (used in 11th: yhdestoista)
121 'kahdes', # 2 (used in 12th: kahdestoista)
122 'kolmas', # 3
123 'neljäs', # 4
124 'viides', # 5
125 'kuudes', # 6
126 'seitsemäs', # 7
127 'kahdeksas', # 8
128 'yhdeksäs', # 9
129 );
130  
131 # Simple 1-9
132 0 ▶ return $ones_ord[$number] if $number >= 1 && $number <= 9;
Branch: 1 branch
BranchTrueFalseTotal
if $number >= 1 and $number <= 9 0 0 0
Condition0%0/3
$number >= 1 and $number <= 9
!ll&&!rl&&r
000
MC/DC0%0/2
$number >= 1 and $number <= 9
$number >= 1 $number <= 9
Truth table0%0/3
A and B
A= $number >= 1B= $number <= 9
ABresult
0X0
100
111
133  
134 # 10
135 0 ▶ return 'kymmenes' if $number == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 10 0 0 0
136  
137 # 11-19: compound_ones + 'toista'
138 0 ▶ if ($number > 10 && $number < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > 10 and $number < 20) 0 0 0
Condition0%0/3
$number > 10 and $number < 20
!ll&&!rl&&r
000
MC/DC0%0/2
$number > 10 and $number < 20
$number > 10 $number < 20
Truth table0%0/3
A and B
A= $number > 10B= $number < 20
ABresult
0X0
100
111
139 0 return $ones_compound[$number - 10] . 'toista';
140 }
141  
142 # Round tens: 20-90
143 0 my @tens_prefix = (
144 q{}, # 0
145 q{}, # 10 handled above
146 'kahdes', # 20
147 'kolmas', # 30
148 'neljäs', # 40
149 'viides', # 50
150 'kuudes', # 60
151 'seitsemäs', # 70
152 'kahdeksas', # 80
153 'yhdeksäs', # 90
154 );
155  
156 0 ▶ if ($number >= 20 && $number < 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 20 and $number < 100) 0 0 0
Condition0%0/3
$number >= 20 and $number < 100
!ll&&!rl&&r
000
MC/DC0%0/2
$number >= 20 and $number < 100
$number >= 20 $number < 100
Truth table0%0/3
A and B
A= $number >= 20B= $number < 100
ABresult
0X0
100
111
157 0 my $ten_idx = int($number / 10);
158 0 my $remain = $number % 10;
159  
160 0 ▶ if ($remain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain == 0) 0 0 0
161 0 return $tens_prefix[$ten_idx] . 'kymmenes';
162 }
163  
164 # Compound: tens-ordinal + ones-ordinal
165 0 return $tens_prefix[$ten_idx] . 'kymmenes' . $ones_ord[$remain];
166 }
167  
168 # 100-999
169 0 ▶ if ($number >= 100 && $number < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 100 and $number < 1000) 0 0 0
Condition0%0/3
$number >= 100 and $number < 1000
!ll&&!rl&&r
000
MC/DC0%0/2
$number >= 100 and $number < 1000
$number >= 100 $number < 1000
Truth table0%0/3
A and B
A= $number >= 100B= $number < 1000
ABresult
0X0
100
111
170 0 my $hundreds = int($number / 100);
171 0 my $remain = $number % 100;
172  
173 0 ▶ if ($remain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain == 0) 0 0 0
174 0 ▶ return 'sadas' if $hundreds == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $hundreds == 1 0 0 0
175 0 return $ones_ord[$hundreds] . 'sadas';
176 }
177  
178 # Non-terminal hundreds use cardinal prefix form + "sata"
179 0 ▶ my $prefix = $hundreds == 1 ? 'sata' : _fin_cardinal_prefix($hundreds) . 'sata';
Branch: 1 branch
BranchTrueFalseTotal
$hundreds == 1 ? : 0 0 0
180  
181 0 return $prefix . num2fin_ordinal($remain);
182 }
183  
184 # 1000-999_999
185 0 ▶ if ($number >= 1000 && $number < 1_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 1000 and $number < 1000000) 0 0 0
Condition0%0/3
$number >= 1000 and $number < 1000000
!ll&&!rl&&r
000
MC/DC0%0/2
$number >= 1000 and $number < 1000000
$number >= 1000 $number < 1000000
Truth table0%0/3
A and B
A= $number >= 1000B= $number < 1000000
ABresult
0X0
100
111
186 0 my $thousands = int($number / 1000);
187 0 my $remain = $number % 1000;
188  
189 0 ▶ if ($remain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain == 0) 0 0 0
190 0 ▶ return 'tuhannes' if $thousands == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $thousands == 1 0 0 0
191 0 return _fin_cardinal_prefix($thousands) . 'tuhannes';
192 }
193  
194 # Non-terminal thousands use cardinal form
195 0 ▶ my $prefix = $thousands == 1 ? 'tuhat' : num2fin_cardinal($thousands) . 'tuhatta';
Branch: 1 branch
BranchTrueFalseTotal
$thousands == 1 ? : 0 0 0
196  
197 0 return $prefix . num2fin_ordinal($remain);
198 }
199  
200 # 1_000_000-999_999_999
201 0 ▶ if ($number >= 1_000_000 && $number < 1_000_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 1000000 and $number < 1000000000) 0 0 0
Condition0%0/3
$number >= 1000000 and $number < 1000000000
!ll&&!rl&&r
000
MC/DC0%0/2
$number >= 1000000 and $number < 1000000000
$number >= 1000000 $number < 1000000000
Truth table0%0/3
A and B
A= $number >= 1000000B= $number < 1000000000
ABresult
0X0
100
111
202 0 my $millions = int($number / 1_000_000);
203 0 my $remain = $number % 1_000_000;
204  
205 0 ▶ if ($remain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain == 0) 0 0 0
206 0 ▶ return 'miljoonas' if $millions == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $millions == 1 0 0 0
207 0 return _fin_cardinal_prefix($millions) . 'miljoonas';
208 }
209  
210 0 my $prefix;
211 0 ▶ if ($millions == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($millions == 1) { } 0 0 0
212 0 $prefix = 'miljoona';
213 }
214 else {
215 0 $prefix = num2fin_cardinal($millions) . ' miljoonaa';
216 }
217  
218 0 return $prefix . ' ' . num2fin_ordinal($remain);
219 }
220  
221 0 return;
222 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
223  
224 # }}}
225 # {{{ _fin_cardinal_prefix cardinal form for non-terminal compound prefix
226  
227 sub _fin_cardinal_prefix {
228 0 ▶ my $n = shift;
Subroutine _fin_cardinal_prefix: not called CC=3 SCAR=24.8
229  
230 # For small numbers used as prefixes in ordinal compounds
231 0 my @card = qw(nolla yksi kaksi kolme neljä viisi kuusi seitsemän kahdeksan yhdeksän);
232  
233 0 ▶ return $card[$n] if $n >= 1 && $n <= 9;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= 1 and $n <= 9 0 0 0
Condition0%0/3
$n >= 1 and $n <= 9
!ll&&!rl&&r
000
MC/DC0%0/2
$n >= 1 and $n <= 9
$n >= 1 $n <= 9
Truth table0%0/3
A and B
A= $n >= 1B= $n <= 9
ABresult
0X0
100
111
234  
235 0 return num2fin_cardinal($n);
236 }
237  
238 # }}}
239  
240  
241 # {{{ capabilities declare supported features
242  
243 sub capabilities {
244 return {
245 0 ▶ cardinal => 1,
Subroutine capabilities: not called CC=1 SCAR=6.9
Pod undocumented
246 ordinal => 1,
247 };
248 }
249  
250 # }}}
251 1 1;
252  
253 __END__
↑ Summary