| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package Lingua::VIE::Num2Word; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Number to word conversion in Vietnamese | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use 5.16.0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # {{{ use block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Export::Attrs; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # }}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # {{{ variable declarations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | our $VERSION = '0.2603270'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # }}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # {{{ num2vie_cardinal convert number to text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | sub num2vie_cardinal :Export { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | my $positive = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
num2vie_cardinal: called CC=41 SCAR=72.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | croak 'You should specify a number from interval [0, 999_999_999]' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0
defined $positive && $positive =~ /\A\d+\z/msux
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | if !defined $positive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | || $positive !~ m{\A\d+\z}xms | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | || $positive < 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | || $positive > 999_999_999; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | my @units = qw(không một hai ba bốn năm sáu bảy tám chín); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | return $units[$positive] if ($positive >= 0 && $positive < 10); # 0 .. 9 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$positive >= 0 and $positive < 10
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | return 'mười' if ($positive == 10); # 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # 11 .. 19 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($positive > 10 && $positive < 20) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$positive > 10 and $positive < 20
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $unit = $positive % 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | return 'mười' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | . ($unit == 5 ? ' lăm' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | : $unit == 4 ? ' bốn' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | : ' ' . $units[$unit]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | my $remain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # 20 .. 99 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ($positive > 19 && $positive < 100) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition0%0/3
$positive > 19 and $positive < 100
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $tens = int($positive / 10); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | $remain = $positive % 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $out = $units[$tens] . ' mươi'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ($remain == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | elsif ($remain == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $out .= ' mốt'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | elsif ($remain == 5) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $out .= ' lăm'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | elsif ($remain == 4) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $out .= ' tư'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $out .= ' ' . $units[$remain]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # 100 .. 999 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | ▶ | elsif ($positive > 99 && $positive < 1000) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$positive > 99 and $positive < 1000
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $hundreds = int($positive / 100); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $remain = $positive % 100; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $out = $units[$hundreds] . ' trăm'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ($remain > 0 && $remain < 10) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$remain > 0 and $remain < 10
MC/DC0%0/2
$remain > 0 and $remain < 10
$remain > 0
$remain < 10
Truth table0%0/3
A and B
A= $remain > 0B= $remain < 10
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $out .= ' lẻ ' . $units[$remain]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | elsif ($remain >= 10) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $out .= ' ' . num2vie_cardinal($remain); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # 1_000 .. 999_999 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ▶ | elsif ($positive > 999 && $positive < 1_000_000) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$positive > 999 and $positive < 1000000
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $thousands = int($positive / 1000); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $remain = $positive % 1000; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $out = num2vie_cardinal($thousands) . ' nghìn'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($remain > 0 && $remain < 10) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
$remain > 0 and $remain < 10
MC/DC0%0/2
$remain > 0 and $remain < 10
$remain > 0
$remain < 10
Truth table0%0/3
A and B
A= $remain > 0B= $remain < 10
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $out .= ' không trăm lẻ ' . $units[$remain]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | ▶ | elsif ($remain >= 10 && $remain < 100) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$remain >= 10 and $remain < 100
MC/DC0%0/2
$remain >= 10 and $remain < 100
$remain >= 10
$remain < 100
Truth table0%0/3
A and B
A= $remain >= 10B= $remain < 100
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $out .= ' không trăm ' . num2vie_cardinal($remain); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | elsif ($remain >= 100) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $out .= ' ' . num2vie_cardinal($remain); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # 1_000_000 .. 999_999_999 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | ▶ | elsif ($positive > 999_999 && $positive < 1_000_000_000) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$positive > 999999 and $positive < 1000000000
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my $millions = int($positive / 1_000_000); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $remain = $positive % 1_000_000; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $out = num2vie_cardinal($millions) . ' triệu'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ($remain > 0 && $remain < 1000) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$remain > 0 and $remain < 1000
MC/DC0%0/2
$remain > 0 and $remain < 1000
$remain > 0
$remain < 1000
Truth table0%0/3
A and B
A= $remain > 0B= $remain < 1000
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # e.g. 1_000_005 -> một triệu không nghìn không trăm lẻ năm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my $sub_hundreds = $remain % 100; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $sub_tens = int($remain / 100); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $out .= ' không nghìn'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ($sub_tens == 0 && $sub_hundreds > 0 && $sub_hundreds < 10) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/6
$sub_tens == 0 and $sub_hundreds > 0 and $sub_hundreds < 10
$sub_tens == 0 and $sub_hundreds > 0
MC/DC0%0/3
$sub_tens == 0 and $sub_hundreds > 0 and $sub_hundreds < 10
$sub_tens == 0
$sub_hundreds > 0
$sub_hundreds < 10
Truth table0%0/4
A and B and C
A= $sub_tens == 0B= $sub_hundreds > 0C= $sub_hundreds < 10
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $out .= ' không trăm lẻ ' . $units[$sub_hundreds]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | ▶ | elsif ($sub_tens == 0 && $sub_hundreds >= 10) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$sub_tens == 0 and $sub_hundreds >= 10
MC/DC0%0/2
$sub_tens == 0 and $sub_hundreds >= 10
$sub_tens == 0
$sub_hundreds >= 10
Truth table0%0/3
A and B
A= $sub_tens == 0B= $sub_hundreds >= 10
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $out .= ' không trăm ' . num2vie_cardinal($sub_hundreds); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | elsif ($sub_tens > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $out .= ' ' . num2vie_cardinal($remain); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | elsif ($remain >= 1000) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $out .= ' ' . num2vie_cardinal($remain); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # }}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | # {{{ capabilities declare supported features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | sub capabilities { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | cardinal => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
capabilities: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | ordinal => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # }}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||