blib/lib/Lingua/Num2Word.pm

filter: Statementstmt 67.8% 57 / 84 Branchbran 23.5% 8 / 34 Conditioncond 19.2% 5 / 26 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 93.3% 14 / 15 Podpod 100% 5 / 5 totaltotal 51.1% 91 / 178 CC 18 SCAR 29.3
CC
18
Cov
49%
cardinal
54.9
get_interval
8.5
preprocess_code
8.3
CRAP 18.8
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1) -*-
2 #
3 # Copyright (c) PetaMem, s.r.o. 2002-present
4 #
5 package Lingua::Num2Word;
6 # ABSTRACT: Number to word conversion
7  
8 1 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # {{{ use block
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # }}}
19 # {{{ var block
20 1 our $VERSION = '0.2603260';
21 1 our %known;
22  
23 # }}}
24  
25 # {{{ templates for functional and object interface
26  
27 1 my $template_func = q{ use __PACKAGE_WITH_VERSION__ ();
28 $result = __PACKAGE__::__FUNCTION__($number);
29 };
30  
31 1 my $template_obj = q{ use __PACKAGE_WITH_VERSION__ ();
32 my $tmp_obj = new __PACKAGE__;
33 $result = $tmp_obj->__FUNCTION__($number);
34 };
35  
36 # ISO 639-1 to 639-3 mapping for supported languages
37 # {{{ ISO 639-1 to 639-3 mapping
38 1 my %iso1_to_3 = (
39 af => 'afr', ar => 'ara', bg => 'bul', ca => 'cat',
40 cs => 'ces', da => 'dan', de => 'deu', el => 'ell',
41 en => 'eng', es => 'spa', et => 'est', eu => 'eus',
42 fa => 'fas', fi => 'fin', fr => 'fra', he => 'heb',
43 hi => 'hin', hr => 'hrv', hu => 'hun', id => 'ind',
44 is => 'isl', it => 'ita', ja => 'jpn', ko => 'kor',
45 lt => 'lit', lv => 'lav', nl => 'nld', 'no' => 'nor',
46 pl => 'pol', pt => 'por', ro => 'ron', ru => 'rus',
47 sk => 'slk', sv => 'swe', sw => 'swa', th => 'tha',
48 tr => 'tur',
49 uk => 'ukr', vi => 'vie', zh => 'zho',
50 );
51 # }}}
52  
53 # {{{ %known — auto-discovered from lib/Lingua/*/Num2Word.pm
54  
55 # Override table for legacy modules with non-standard API
56 1 my %n2w_override = (
57 afr => { package => 'Numbers', function => 'parse', code => $template_obj, limit_hi => 99_999_999_999 },
58 eng => { package => 'Numbers', limit_lo => 1, limit_hi => 999_999_999_999_999,
59 code => q{ use __PACKAGE_WITH_VERSION__ qw(American);
60 my $tmp_obj = new __PACKAGE__;
61 $tmp_obj->parse($number);
62 $result = $tmp_obj->get_string;
63 },
64 },
65 eus => { package => 'Numbers', function => 'cardinal2alpha', limit_hi => 999_999_999_999 },
66 fra => { package => 'Numbers', limit_hi => 999_999_999_999_999,
67 code => q{ use Lingua::FRA::Numbers;
68 my $tmp_obj = Lingua::FRA::Numbers->new($number);
69 $result = $tmp_obj->get_string;
70 },
71 },
72 ind => { package => 'Nums2Words', function => 'nums2words', limit_hi => 999_999_999_999_999 },
73 ita => { package => 'Numbers', function => 'number_to_it', limit_hi => 999_999_999_999 },
74 jpn => { package => 'Number', function => 'to_string', limit_lo => 1, limit_hi => 999_999_999_999_999,
75 code => q{ use __PACKAGE_WITH_VERSION__ ();
76 my @words = __PACKAGE__::__FUNCTION__($number);
77 $result = join ' ', @words;
78 },
79 },
80 nor => { function => 'num2no_cardinal', code => $template_obj },
81 pol => { package => 'Numbers', function => 'parse', code => $template_obj, limit_hi => 9_999_999_999_999 },
82 por => { package => 'Nums2Words', function => 'num2word', limit_hi => 999_999_999_999_999 },
83 rus => { package => 'Number', function => 'rur_in_words', limit_hi => 999_999_999_999_999,
84 code => q{ use __PACKAGE_WITH_VERSION__ ();
85 $result = __PACKAGE__::__FUNCTION__($number);
86 if ($result) {
87 if ($number) {
88 $result =~ s/\s+\S+\s+\S+\s+\S+$//;
89 }
90 else {
91 $result =~ s/\s+\S+$//;
92 }
93 $result =~ s/^\s+//;
94 }
95 },
96 },
97 spa => { package => 'Numeros', function => 'cardinal', code => $template_obj, limit_hi => 999_999_999_999_999 },
98 swe => { function => 'num2sv_cardinal' },
99 zho => { package => 'Numbers', limit_lo => 1, limit_hi => 999_999_999_999_999,
100 code => q{ use __PACKAGE_WITH_VERSION__ qw(traditional);
101 my $tmp_obj = new __PACKAGE__;
102 $tmp_obj->parse($number);
103 $result = $tmp_obj->get_string;
104 },
105 },
106 );
107  
108 # auto-discover: scan for Lingua::*::Num2Word modules
109 {
110 1 my $lingua_dir;
111 1 for my $inc (@INC) {
112 2 my $try = "$inc/Lingua";
113 2 ▶ if (-d $try) { $lingua_dir = $try; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $try) 1 1 2
114 }
115  
116 1 ▶ if ($lingua_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lingua_dir) 1 0 1
117 1 for my $dir (glob "$lingua_dir/*/") {
118 0 my ($lang) = $dir =~ m{/([A-Z]{3})/\z};
119 0 ▶ next unless $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless $lang 0 0 0
120 0 $lang = lc $lang;
121  
122 # standard pattern: Num2Word.pm with num2XXX_cardinal function
123 0 ▶ if (-e "$dir/Num2Word.pm" || exists $n2w_override{$lang}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$dir/Num2Word.pm" or exists $n2w_override{$lang}) 0 0 0
Condition0%0/3
-e "$dir/Num2Word.pm" or exists $n2w_override{$lang}
l!l&&r!l&&!r
000
MC/DC0%0/2
-e "$dir/Num2Word.pm" or exists $n2w_override{$lang}
-e "$dir/Num2Word.pm" exists $n2w_override{$lang}
Truth table0%0/3
A or B
A= -e "$dir/Num2Word.pm"B= exists $n2w_override{$lang}
ABresult
1X1
011
000
124 0 ▶ my $ov = $n2w_override{$lang} // {};
Condition0%0/2
$n2w_override{$lang} // {}
l!l
00
MC/DC0%0/1
$n2w_override{$lang} // {}
$n2w_override{$lang}
Truth table0%0/2
A
A= $n2w_override{$lang}
Aresult
11
00
125 $known{$lang} = {
126 package => $ov->{package} // 'Num2Word',
127 version => $ov->{version} // '',
128 limit_lo => $ov->{limit_lo} // 0,
129 limit_hi => $ov->{limit_hi} // 999_999_999,
130 function => $ov->{function} // "num2${lang}_cardinal",
131 0 ▶ code => $ov->{code} // $template_func,
Condition0%0/13
$ov->{"package"} // "Num2Word"
l!l
00
$ov->{"version"} // ""
l!l
00
$ov->{"limit_lo"} // 0
l!l
00
$ov->{"limit_hi"} // 999999999
l!l
00
$ov->{"function"} // "num2${lang}_cardinal"
l!l
00
$ov->{"code"} // $template_func
l!l&&r!l&&!r
000
MC/DC0%0/1
$ov->{"package"} // "Num2Word"
$ov->{"package"}
MC/DC0%0/1
$ov->{"version"} // ""
$ov->{"version"}
MC/DC0%0/1
$ov->{"limit_lo"} // 0
$ov->{"limit_lo"}
MC/DC0%0/1
$ov->{"limit_hi"} // 999999999
$ov->{"limit_hi"}
MC/DC0%0/1
$ov->{"function"} // "num2${lang}_cardinal"
$ov->{"function"}
MC/DC0%0/2
$ov->{"code"} // $template_func
$ov->{"code"} $template_func
Truth table0%0/2
A
A= $ov->{"package"}
Aresult
11
00
Truth table0%0/2
A
A= $ov->{"version"}
Aresult
11
00
Truth table0%0/2
A
A= $ov->{"limit_lo"}
Aresult
11
00
Truth table0%0/2
A
A= $ov->{"limit_hi"}
Aresult
11
00
Truth table0%0/2
A
A= $ov->{"function"}
Aresult
11
00
Truth table0%0/3
A // B
A= $ov->{"code"}B= $template_func
ABresult
1X1
011
000
132 };
133 }
134 }
135 }
136 }
137  
138 # }}}
139 # {{{ new constructor
140  
141 sub new {
142 5 ▶ return bless {}, shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
143 }
144  
145 # }}}
146 # {{{ known_langs list of currently supported languages
147  
148 sub known_langs :Export {
149 0 ▶ return wantarray ? sort keys %known : [ sort keys %known ];
Subroutine known_langs: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
150 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
151  
152 # }}}
153 # {{{ get_interval get minimal and maximal supported number
154  
155 # Return:
156 # undef for unsupported language
157 # list or list reference (depending to calling context) with
158 # minimal and maximal supported number
159 #
160 sub get_interval :Export {
161 2 ▶ my $self = ref($_[0]) ? shift : Lingua::Num2Word->new();
Subroutine get_interval: called CC=6 SCAR=21.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
162 2 ▶ my $lang = shift // return;
Condition100%2/2
shift() // (return)
l!l
11
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
163 1 $lang = lc $lang;
164 1 ▶ $lang = $iso1_to_3{$lang} if exists $iso1_to_3{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $iso1_to_3{$lang} 0 1 1
165  
166 1 ▶ return if (!defined $known{$lang});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Lingua::Num2Word::known{$lang} 1 0 1
167  
168 0 my @limits = ($known{$lang}{limit_lo}, $known{$lang}{limit_hi});
169  
170 0 ▶ return wantarray ? @limits : \@limits;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
171 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 # }}}
174 # {{{ cardinal convert number to text
175  
176 sub cardinal :Export {
177 2 ▶ my $self = ref($_[0]) ? shift : Lingua::Num2Word->new();
Subroutine cardinal: called CC=9 SCAR=40.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
178 2 my $result = '';
179 2 ▶ my $lang = shift // return $result;
Condition50%1/2
shift() // (return $result)
l!l
02
MC/DC0%0/1
shift() // (return $result)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
180 0 ▶ my $number = shift // return $result;
Condition0%0/2
shift() // (return $result)
l!l
00
MC/DC0%0/1
shift() // (return $result)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
181  
182 0 $lang = lc $lang;
183 0 ▶ $lang = $iso1_to_3{$lang} if exists $iso1_to_3{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $iso1_to_3{$lang} 0 0 0
184  
185 0 ▶ return $result if (!defined $known{$lang});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Lingua::Num2Word::known{$lang} 0 0 0
186  
187 0 ▶ if (defined $known{$lang}{lang}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Lingua::Num2Word::known{$lang}{'lang'}) { } 0 0 0
188 0 eval $self->preprocess_code($known{$lang}{lang}); ## no critic
189 0 ▶ carp $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
190 }
191 else {
192 0 eval $self->preprocess_code($lang); ## no critic
193 0 ▶ carp $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
194 }
195  
196 0 return $result;
197 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
198  
199 # }}}
200 # {{{ preprocess_code prepare code for evaluation
201  
202 sub preprocess_code :Export {
203 2 ▶ my $self = shift;
Subroutine preprocess_code: called CC=4 SCAR=21.2
Pod documented
204 2 ▶ my $lang = shift // return;
Condition100%2/2
shift() // (return)
l!l
11
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
205  
206 1 ▶ return if !exists $known{$lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Lingua::Num2Word::known{$lang} 1 0 1
207  
208 0 my $result = $known{$lang}{code};
209 0 my $pkg_name = 'Lingua::' . uc($lang) . '::' . $known{$lang}{package};
210 0 ▶ my $pkg_name_with_version = $known{$lang}{version} ne ''
Branch: 1 branch
BranchTrueFalseTotal
$Lingua::Num2Word::known{$lang}{'version'} ne '' ? : 0 0 0
211 ? "$pkg_name $known{$lang}{version}"
212 : $pkg_name
213 ;
214  
215 0 my $function = $known{$lang}{function};
216  
217 0 $result =~ s/__PACKAGE_WITH_VERSION__/$pkg_name_with_version/g;
218 0 $result =~ s/__PACKAGE__/$pkg_name/g;
219 0 $result =~ s/__FUNCTION__/$function/g;
220  
221 0 return $result;
222 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
223 # }}}
224  
225 1 1;
226 __END__
↑ Summary