blib/lib/Lingua/DEU/Num2Word.pm

filter: Statementstmt 74.6% 50 / 67 Branchbran 34.0% 15 / 44 Conditioncond 41.6% 15 / 36 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 81.8% 9 / 11 Podpod 66.6% 2 / 3 totaltotal 53.0% 96 / 181 CC 35 SCAR 41.8
CC
35
Cov
54%
num2deu_ordinal
132.0
num2deu_cardinal
59.1
capabilities
2.0
CRAP 65.3
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2  
3 package Lingua::DEU::Num2Word;
4 # ABSTRACT: Number to word conversion in German
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 # {{{ num2deu_cardinal convert number to text
25  
26 sub num2deu_cardinal :Export {
27 7 ▶ my $positive = shift;
Subroutine num2deu_cardinal: called CC=25 SCAR=40.8
Pod documented
28  
29 7 ▶ 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 5 6
Condition77%7/9
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999
l!l&&r!l&&!r
115
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0
l!l&&r!l&&!r
106
defined $positive && $positive =~ /\A\d+\z/msux
!ll&&!rl&&r
106
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 5 my @tokens1 = qw(null ein zwei drei vier fünf sechs sieben acht neun zehn elf zwölf);
36 5 my @tokens2 = qw(zwanzig dreissig vierzig fünfzig sechzig siebzig achtzig neunzig hundert);
37  
38 5 ▶ return $tokens1[$positive] if ($positive >= 0 && $positive < 13); # 0 .. 12
Branch: 1 branch
BranchTrueFalseTotal
if $positive >= 0 and $positive < 13 2 3 5
Condition66%2/3
$positive >= 0 and $positive < 13
!ll&&!rl&&r
032
MC/DC50%1/2
$positive >= 0 and $positive < 13
$positive >= 0 $positive < 13
Truth table66%2/3
A and B
A= $positive >= 0B= $positive < 13
ABresult
0X0
100
111
39 3 ▶ return 'sechzehn' if ($positive == 16); # 16 exception
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 16 0 3 3
40 3 ▶ return 'siebzehn' if ($positive == 17); # 17 exception
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 17 0 3 3
41 3 ▶ return $tokens1[$positive-10] . 'zehn' if ($positive > 12 && $positive < 20); # 13 .. 19
Branch: 1 branch
BranchTrueFalseTotal
if $positive > 12 and $positive < 20 0 3 3
Condition33%1/3
$positive > 12 and $positive < 20
!ll&&!rl&&r
030
MC/DC0%0/2
$positive > 12 and $positive < 20
$positive > 12 $positive < 20
Truth table33%1/3
A and B
A= $positive > 12B= $positive < 20
ABresult
0X0
100
111
42  
43 3 my $out; # string for return value construction
44 my $one_idx; # index for tokens1 array
45 3 my $remain; # remainder
46  
47 3 ▶ if ($positive > 19 && $positive < 101) { # 20 .. 100
Branch: 4 branches
BranchTrueFalseTotal
if ($positive > 19 and $positive < 101) { } 1 2 3
elsif ($positive > 100 and $positive < 1000) { } 1 1 2
elsif ($positive > 999 and $positive < 1000000) { } 1 0 1
elsif ($positive > 999999 and $positive < 1000000000) { } 0 0 0
Condition66%2/3
$positive > 19 and $positive < 101
!ll&&!rl&&r
021
MC/DC50%1/2
$positive > 19 and $positive < 101
$positive > 19 $positive < 101
Truth table66%2/3
A and B
A= $positive > 19B= $positive < 101
ABresult
0X0
100
111
48 1 $one_idx = int ($positive / 10);
49 1 $remain = $positive % 10;
50  
51 1 ▶ $out = "$tokens1[$remain]und" if ($remain);
Branch: 1 branch
BranchTrueFalseTotal
if $remain 1 0 1
52 1 $out .= $tokens2[$one_idx - 2];
53 }
54 ▶ elsif ($positive > 100 && $positive < 1000) { # 101 .. 999
Condition66%2/3
$positive > 100 and $positive < 1000
!ll&&!rl&&r
011
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
55 1 $one_idx = int ($positive / 100);
56 1 $remain = $positive % 100;
57  
58 1 $out = "$tokens1[$one_idx]hundert";
59 1 ▶ $out .= $remain ? num2deu_cardinal($remain) : '';
Branch: 1 branch
BranchTrueFalseTotal
$remain ? : 1 0 1
60 }
61 ▶ elsif ($positive > 999 && $positive < 1_000_000) { # 1000 .. 999_999
Condition33%1/3
$positive > 999 and $positive < 1000000
!ll&&!rl&&r
001
MC/DC0%0/2
$positive > 999 and $positive < 1000000
$positive > 999 $positive < 1000000
Truth table33%1/3
A and B
A= $positive > 999B= $positive < 1000000
ABresult
0X0
100
111
62 1 $one_idx = int ($positive / 1000);
63 1 $remain = $positive % 1000;
64  
65 1 $out = num2deu_cardinal($one_idx).'tausend';
66 1 ▶ $out .= $remain ? num2deu_cardinal($remain) : '';
Branch: 1 branch
BranchTrueFalseTotal
$remain ? : 0 1 1
67 }
68 ▶ 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
69 && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999
70 0 $one_idx = int ($positive / 1000000);
71 0 $remain = $positive % 1000000;
72 0 ▶ my $one = $one_idx == 1 ? 'e' : '';
Branch: 1 branch
BranchTrueFalseTotal
$one_idx == 1 ? : 0 0 0
73  
74 0 $out = num2deu_cardinal($one_idx) . "$one million";
75 0 ▶ $out .= 'en' if ($one_idx > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $one_idx > 1 0 0 0
76 0 ▶ $out .= $remain ? ' ' . num2deu_cardinal($remain) : '';
Branch: 1 branch
BranchTrueFalseTotal
$remain ? : 0 0 0
77 }
78  
79 3 return $out;
80 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 # }}}
83  
84  
85 # {{{ num2deu_ordinal convert number to ordinal text
86  
87 sub num2deu_ordinal :Export {
88 0 ▶ my $number = shift;
Subroutine num2deu_ordinal: not called CC=11 SCAR=48.8
Pod documented
89  
90 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
91 if !defined $number
92 || $number !~ m{\A\d+\z}xms
93 || $number < 1
94 || $number > 999_999_999;
95  
96 # Fully irregular forms
97 0 ▶ return 'erste' if $number == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 1 0 0 0
98 0 ▶ return 'zweite' if $number == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 2 0 0 0
99 0 ▶ return 'dritte' if $number == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 3 0 0 0
100  
101 # Stem irregulars: siebte (not siebente), achte (not achtte)
102 0 ▶ return 'siebte' if $number == 7;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 7 0 0 0
103 0 ▶ return 'achte' if $number == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 8 0 0 0
104  
105 0 my $cardinal = num2deu_cardinal($number);
106  
107 # Numbers 4-19 get suffix "te", 20+ get "ste"
108 0 ▶ my $suffix = $number < 20 ? 'te' : 'ste';
Branch: 1 branch
BranchTrueFalseTotal
$number < 20 ? : 0 0 0
109  
110 0 return $cardinal . $suffix;
111 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 # }}}
114  
115 # {{{ capabilities declare supported features
116  
117 sub capabilities {
118 return {
119 0 ▶ cardinal => 1,
Subroutine capabilities: not called CC=1 SCAR=6.9
Pod undocumented
120 ordinal => 1,
121 };
122 }
123  
124 # }}}
125 1 1;
126  
127 __END__
↑ Summary