blib/lib/Lingua/JPN/Num2Word.pm

filter: Statementstmt 97.5% 119 / 122 Branchbran 82.1% 46 / 56 Conditioncond 52.1% 24 / 46 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 80.2% 215 / 268 CC 46 SCAR 20.7
CC
46
Cov
84%
_render_block4
11.0
num2jpn_cardinal
10.1
to_string
9.8
CRAP 7.9
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8; -*-
2 #
3 # Copyright (c) Mike Schilli, 2001 (m@perlmeister.com)
4 # Copyright (c) PetaMem, s.r.o. 2002-present
5  
6 package Lingua::JPN::Num2Word;
7 # ABSTRACT: Number to word conversion in Japanese
8  
9 2 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # {{{ use block
14  
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # }}}
19 # {{{ variables declaration
20 2 our $VERSION = '0.2604300';
21  
22 # {{{ lexicons — kanji / hiragana / romaji per script
23  
24 2 my %DIGIT = (
25 kanji => [ qw( 〇 一 二 三 四 五 六 七 八 九 ) ],
26 hiragana => [ qw( ぜろ いち に さん よん ご ろく なな はち きゅう ) ],
27 romaji => [ qw( zero ichi ni san yon go roku nana hachi kyu ) ],
28 );
29  
30 # Scale words at each magnitude. Hundred (百), thousand (千), ten-thousand (万),
31 # hundred-million (億), trillion (兆) — the canonical Japanese scale ladder.
32 2 my %SCALE = (
33 kanji => { 10 => '十', 100 => '百', 1000 => '千', 10_000 => '万',
34 100_000_000 => '億', 1_000_000_000_000 => '兆' },
35 hiragana => { 10 => 'じゅう', 100 => 'ひゃく', 1000 => 'せん', 10_000 => 'まん',
36 100_000_000 => 'おく', 1_000_000_000_000 => 'ちょう' },
37 romaji => { 10 => 'ju', 100 => 'hyaku', 1000 => 'sen', 10_000 => 'man',
38 100_000_000 => 'oku', 1_000_000_000_000 => 'cho' },
39 );
40  
41 # Irregular hundreds (rendaku/gemination):
42 # 300 さんびゃく sanbyaku
43 # 600 ろっぴゃく roppyaku
44 # 800 はっぴゃく happyaku
45 2 my %HUNDRED_IRREGULAR = (
46 kanji => { 3 => '三百', 6 => '六百', 8 => '八百' },
47 hiragana => { 3 => 'さんびゃく', 6 => 'ろっぴゃく', 8 => 'はっぴゃく' },
48 romaji => { 3 => 'sanbyaku', 6 => 'roppyaku', 8 => 'happyaku' },
49 );
50  
51 # Irregular thousands:
52 # 3000 さんぜん sanzen (rendaku)
53 # 8000 はっせん hassen (gemination)
54 2 my %THOUSAND_IRREGULAR = (
55 kanji => { 3 => '三千', 8 => '八千' },
56 hiragana => { 3 => 'さんぜん', 8 => 'はっせん' },
57 romaji => { 3 => 'sanzen', 8 => 'hassen' },
58 );
59  
60 # Irregular trillion-block leaders (cho-prefixes):
61 # 1兆 いっちょう itcho
62 # 8兆 はっちょう hatcho
63 # 10兆 じゅっちょう jutcho
64 2 my %CHO_IRREGULAR = (
65 kanji => { 1 => '一兆', 8 => '八兆', 10 => '十兆' },
66 hiragana => { 1 => 'いっちょう', 8 => 'はっちょう', 10 => 'じゅっちょう' },
67 romaji => { 1 => 'itcho', 8 => 'hatcho', 10 => 'jutcho' },
68 );
69  
70 # }}}
71  
72 # }}}
73  
74 # {{{ num2jpn_cardinal number → text in chosen script
75  
76 31 ▶sub num2jpn_cardinal :Export {
Subroutine num2jpn_cardinal: called CC=7 SCAR=23.2
Pod documented
77 31 my $n = shift;
78 31 ▶ my $script = shift // 'kanji';
Condition100%2/2
shift() // "kanji"
l!l
1714
MC/DC100%1/1
shift() // "kanji"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
79  
80 croak "Unknown script '$script' (expected: kanji, hiragana, romaji)"
81 31 ▶ unless exists $DIGIT{$script};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DIGIT{$script} 0 31 31
82  
83 31 ▶ croak "Number must be in range [1, 1E16)"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n && $n =~ /\A\d+\z/msux or $n < 1 or $n >= "1e+16" 0 31 31
Condition33%3/9
not defined $n && $n =~ /\A\d+\z/msux or $n < 1 or $n >= "1e+16"
l!l&&r!l&&!r
0031
not defined $n && $n =~ /\A\d+\z/msux or $n < 1
l!l&&r!l&&!r
0031
defined $n && $n =~ /\A\d+\z/msux
!ll&&!rl&&r
0031
MC/DC0%0/4
not defined $n && $n =~ /\A\d+\z/msux or $n < 1 or $n >= "1e+16"
defined $n $n =~ /\A\d+\z/msux $n < 1 $n >= "1e+16"
Truth table20%1/5
not(A && B) or C or D
A= defined $nB= $n =~ /\A\d+\z/msuxC= $n < 1D= $n >= "1e+16"
ABCDresult
0XXX1
10XX1
111X1
11011
11000
84 if !defined $n || $n !~ m{\A\d+\z}xms || $n < 1 || $n >= 1E16;
85  
86 31 return _render($n, $script);
87 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 # }}}
90 # {{{ num2jpn_ordinal number → ordinal text in chosen script
91  
92 8 ▶sub num2jpn_ordinal :Export {
Subroutine num2jpn_ordinal: called CC=8 SCAR=22.7
Pod documented
93 8 my $n = shift;
94 8 ▶ my $script = shift // 'kanji';
Condition100%2/2
shift() // "kanji"
l!l
44
MC/DC100%1/1
shift() // "kanji"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
95  
96 croak "Unknown script '$script' (expected: kanji, hiragana, romaji)"
97 8 ▶ unless exists $DIGIT{$script};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DIGIT{$script} 0 8 8
98  
99 8 ▶ croak "Number must be in range [1, 1E16)"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n && $n =~ /\A\d+\z/msux or $n < 1 or $n >= "1e+16" 0 8 8
Condition33%3/9
not defined $n && $n =~ /\A\d+\z/msux or $n < 1 or $n >= "1e+16"
l!l&&r!l&&!r
008
not defined $n && $n =~ /\A\d+\z/msux or $n < 1
l!l&&r!l&&!r
008
defined $n && $n =~ /\A\d+\z/msux
!ll&&!rl&&r
008
MC/DC0%0/4
not defined $n && $n =~ /\A\d+\z/msux or $n < 1 or $n >= "1e+16"
defined $n $n =~ /\A\d+\z/msux $n < 1 $n >= "1e+16"
Truth table20%1/5
not(A && B) or C or D
A= defined $nB= $n =~ /\A\d+\z/msuxC= $n < 1D= $n >= "1e+16"
ABCDresult
0XXX1
10XX1
111X1
11011
11000
100 if !defined $n || $n !~ m{\A\d+\z}xms || $n < 1 || $n >= 1E16;
101  
102 8 my $cardinal = _render($n, $script);
103 8 ▶ if ($script eq 'romaji') {
Branch: 1 branch
BranchTrueFalseTotal
if ($script eq "romaji") 3 5 8
104 # Conventional ordinal romaji is fully hyphen-joined: spaces between
105 # block-tokens become hyphens, and the suffix attaches with a hyphen.
106 3 $cardinal =~ tr/ /-/;
107 3 return $cardinal . '-ban-me';
108 }
109 5 my %suffix = (kanji => '番目', hiragana => 'ばんめ');
110 5 return $cardinal . $suffix{$script};
111 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 # }}}
114 # {{{ to_string legacy romaji-list interface (DEPRECATED)
115  
116 # Mike Schilli's 2001 lexicon — preserved verbatim so to_string output is
117 # bit-for-bit identical to the historical contract. New code should use
118 # num2jpn_cardinal($n, 'romaji') instead.
119 2 my %_LEGACY_N2J = qw(
120 1 ichi 2 ni 3 san 4 yon 5 go 6 roku 7 nana
121 8 hachi 9 kyu 10 ju 100 hyaku 1000 sen);
122  
123 2 my %_LEGACY_N2J_EXCP = qw(
124 300 san-byaku 600 ro-p-pyaku 800 ha-p-pyaku
125 3000 san-zen 8000 ha-s-sen);
126  
127 2 my @_LEGACY_N2J_BLOCK = ('', 'man', 'oku', 'cho');
128  
129 2 my %_LEGACY_N2J_BLOCK_EXCP = qw( 1 i-t-cho 8 ha-t-cho 0 ju-t-cho );
130  
131 5 ▶sub to_string :Export {
Subroutine to_string: called CC=9 SCAR=22.8
Pod documented
132 5 my $n = shift;
133  
134 5 ▶ if (!defined $n || $n < 1 || $n >= 1E16) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $n or $n < 1 or $n >= "1e+16") 0 5 5
Condition33%2/6
not defined $n or $n < 1 or $n >= "1e+16"
l!l&&r!l&&!r
005
not defined $n or $n < 1
l!l&&r!l&&!r
005
MC/DC0%0/3
not defined $n or $n < 1 or $n >= "1e+16"
not defined $n $n < 1 $n >= "1e+16"
Truth table25%1/4
not(A) or B or C
A= not defined $nB= $n < 1C= $n >= "1e+16"
ABCresult
1XX1
01X1
0011
0000
135 0 warn "$n needs to be >=1 and <1E16.\n";
136 0 return;
137 }
138  
139 5 my @result;
140 5 $n = reverse $n;
141 5 my $bix = 0;
142  
143 5 ▶ while ($n =~ /(\d{1,4})/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /(\d{1,4})/gu) 9 5 14
144 9 my $b = scalar reverse($1);
145 9 my @r = _legacy_blockof4($b);
146  
147 9 ▶ if ($bix && @r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bix and @r) 2 7 9
Condition100%3/3
$bix and @r
!ll&&!rl&&r
522
MC/DC100%2/2
$bix and @r
$bix @r
Truth table100%3/3
A and B
A= $bixB= @r
ABresult
0X0
100
111
148 2 ▶ if ($bix == 3 && $b =~ /[1-9]0$|[18]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bix == 3 and $b =~ /[1-9]0$|[18]$/u) { } 1 1 2
Condition66%2/3
$bix == 3 and $b =~ /[1-9]0$|[18]$/u
!ll&&!rl&&r
101
MC/DC50%1/2
$bix == 3 and $b =~ /[1-9]0$|[18]$/u
$bix == 3 $b =~ /[1-9]0$|[18]$/u
Truth table66%2/3
A and B
A= $bix == 3B= $b =~ /[1-9]0$|[18]$/u
ABresult
0X0
100
111
149 1 $r[-1] = $_LEGACY_N2J_BLOCK_EXCP{$b % 10};
150 }
151 else {
152 1 push @r, $_LEGACY_N2J_BLOCK[$bix];
153 }
154 }
155 9 unshift @result, @r;
156 9 $bix++;
157 }
158  
159 5 return @result;
160 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 9 ▶sub _legacy_blockof4 {
Subroutine _legacy_blockof4: called CC=8 SCAR=21.7
163 9 my $n = shift;
164 9 ▶ return if $n > 9999 or $n < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n > 9999 or $n < 0 0 9 9
Condition33%1/3
$n > 9999 or $n < 0
l!l&&r!l&&!r
009
MC/DC0%0/2
$n > 9999 or $n < 0
$n > 9999 $n < 0
Truth table33%1/3
A or B
A= $n > 9999B= $n < 0
ABresult
1X1
011
000
165 9 ▶ return '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 9 9
166  
167 9 my @result;
168 9 my @digits = split //, sprintf('%04d', $n);
169 9 my @weights = (1000, 100, 10, 1);
170  
171 9 for my $i (0..3) {
172 36 ▶ next unless $digits[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $digits[$i] 23 13 36
173 13 my $v = $digits[$i] * $weights[$i];
174 push @result, $_LEGACY_N2J_EXCP{$v}
175 || $_LEGACY_N2J{$v}
176 13 ▶ || ($_LEGACY_N2J{$digits[$i]}, $_LEGACY_N2J{$weights[$i]});
Condition66%4/6
$_LEGACY_N2J_EXCP{$v} || $_LEGACY_N2J{$v} || ($_LEGACY_N2J{$digits[$i]}, $_LEGACY_N2J{$weights[$i]})
l!l&&r!l&&!r
760
$_LEGACY_N2J_EXCP{$v} or $_LEGACY_N2J{$v}
l!l&&r!l&&!r
076
MC/DC0%0/3
$_LEGACY_N2J_EXCP{$v} || $_LEGACY_N2J{$v} || ($_LEGACY_N2J{$digits[$i]}, $_LEGACY_N2J{$weights[$i]})
$_LEGACY_N2J_EXCP{$v} $_LEGACY_N2J{$v} ($_LEGACY_N2J{$digits[$i]}, $_LEGACY_N2J{$weights[$i]})
Truth table50%2/4
A or B || C
A= $_LEGACY_N2J_EXCP{$v}B= $_LEGACY_N2J{$v}C= ($_LEGACY_N2J{$digits[$i]}, $_LEGACY_N2J{$weights[$i]})
ABCresult
1XX1
01X1
0011
0000
177 }
178  
179 9 return @result;
180 }
181  
182 # }}}
183 # {{{ _render core: number → string in given script
184  
185 39 ▶sub _render {
Subroutine _render: called CC=6 SCAR=17.9
186 39 my ($n, $script) = @_;
187  
188 # Decompose into 4-digit blocks, weighted by 10^(4*k):
189 # block 0 → 1
190 # block 1 → 万 (10^4)
191 # block 2 → 億 (10^8)
192 # block 3 → 兆 (10^12)
193 39 my @block_scales = (1, 10_000, 100_000_000, 1_000_000_000_000);
194 39 my @parts;
195 39 my $bix = 0;
196  
197 39 ▶ while ($n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) 46 39 85
198 46 my $block = $n % 10_000;
199 46 $n = int($n / 10_000);
200 46 ▶ if ($block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block) 39 7 46
201 39 my $piece;
202 39 ▶ if ($bix == 3 && exists $CHO_IRREGULAR{$script}{$block}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bix == 3 and exists $CHO_IRREGULAR{$script}{$block}) { } 1 38 39
Condition66%2/3
$bix == 3 and exists $CHO_IRREGULAR{$script}{$block}
!ll&&!rl&&r
3801
MC/DC50%1/2
$bix == 3 and exists $CHO_IRREGULAR{$script}{$block}
$bix == 3 exists $CHO_IRREGULAR{$script}{$block}
Truth table66%2/3
A and B
A= $bix == 3B= exists $CHO_IRREGULAR{$script}{$block}
ABresult
0X0
100
111
203 # Whole-block chō irregulars: 1兆, 8兆, 10兆 fuse digit+scale.
204 1 $piece = $CHO_IRREGULAR{$script}{$block};
205 }
206 else {
207 38 $piece = _render_block4($block, $script);
208 38 ▶ $piece .= $SCALE{$script}{$block_scales[$bix]} if $bix > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bix > 0 3 35 38
209 }
210 39 unshift @parts, $piece;
211 }
212 46 $bix++;
213 }
214  
215 39 return _join_parts(\@parts, $script);
216 }
217  
218 # }}}
219 # {{{ _render_block4 render integer 1..9999 in given script
220  
221 38 ▶sub _render_block4 {
Subroutine _render_block4: called CC=11 SCAR=24.0
222 38 my ($n, $script) = @_;
223  
224 38 ▶ return '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 38 38
225  
226 # In romaji, digit and scale fuse into a single word ("nihyaku", "sanju",
227 # "yonsen"). Block boundaries get whitespace ("sen nihyaku sanju yon"
228 # for 1234). Kanji and hiragana have no whitespace at all.
229 38 my @parts;
230  
231 # Thousands (1000..9999)
232 38 ▶ if (my $th = int($n / 1000)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $th = int $n / 1000) 11 27 38
233 11 ▶ if (exists $THOUSAND_IRREGULAR{$script}{$th}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $THOUSAND_IRREGULAR{$script}{$th}) { } 6 5 11
elsif ($th == 1) { } 5 0 5
234 6 push @parts, $THOUSAND_IRREGULAR{$script}{$th};
235 }
236 elsif ($th == 1) {
237 5 push @parts, $SCALE{$script}{1000};
238 }
239 else {
240 0 push @parts, $DIGIT{$script}[$th] . $SCALE{$script}{1000};
241 }
242 11 $n %= 1000;
243 }
244  
245 # Hundreds (100..999)
246 38 ▶ if (my $h = int($n / 100)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = int $n / 100) 14 24 38
247 14 ▶ if (exists $HUNDRED_IRREGULAR{$script}{$h}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $HUNDRED_IRREGULAR{$script}{$h}) { } 8 6 14
elsif ($h == 1) { } 3 3 6
248 8 push @parts, $HUNDRED_IRREGULAR{$script}{$h};
249 }
250 elsif ($h == 1) {
251 3 push @parts, $SCALE{$script}{100};
252 }
253 else {
254 3 push @parts, $DIGIT{$script}[$h] . $SCALE{$script}{100};
255 }
256 14 $n %= 100;
257 }
258  
259 # Tens (10..99)
260 38 ▶ if (my $t = int($n / 10)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = int $n / 10) 9 29 38
261 9 ▶ if ($t == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 1) { } 5 4 9
262 5 push @parts, $SCALE{$script}{10};
263 }
264 else {
265 4 push @parts, $DIGIT{$script}[$t] . $SCALE{$script}{10};
266 }
267 9 $n %= 10;
268 }
269  
270 # Units (1..9)
271 38 ▶ push @parts, $DIGIT{$script}[$n] if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 15 23 38
272  
273 38 return _join_parts(\@parts, $script);
274 }
275  
276 # }}}
277 # {{{ _join_parts concatenate parts with script-appropriate sep
278  
279 77 ▶sub _join_parts {
Subroutine _join_parts: called CC=3 SCAR=11.0
280 77 my ($parts_lr, $script) = @_;
281 77 ▶ return '' unless @{$parts_lr};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$parts_lr;} 0 77 77
282 # Romaji is the only script that uses whitespace as a word separator.
283 # Kanji and hiragana are written as a continuous string.
284 77 ▶ return $script eq 'romaji' ? join(' ', @{$parts_lr}) : join('', @{$parts_lr});
Branch: 1 branch
BranchTrueFalseTotal
$script eq 'romaji' ? : 26 51 77
285 }
286  
287 # }}}
288 # {{{ capabilities declare supported features
289  
290 1 ▶sub capabilities {
Subroutine capabilities: called CC=1 SCAR=0.0
Pod documented
291 return {
292 1 cardinal => 1,
293 ordinal => 1,
294 scripts => [ 'kanji', 'hiragana', 'romaji' ],
295 };
296 }
297  
298 # }}}
299 2 1;
300  
301 __END__
↑ Summary