| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | package Lingua::THA::Num2Word; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Number to word conversion in Thai | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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.2603300'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # }}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # {{{ num2tha_cardinal convert number to text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | sub num2tha_cardinal :Export { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
num2tha_cardinal: called CC=31 SCAR=66.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | my $positive = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | if !defined $positive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | || $positive !~ m{\A\d+\z}xms | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | || $positive < 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | || $positive > 999_999_999; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | my @units = qw(ศูนย์ หนึ่ง สอง สาม สี่ ห้า หก เจ็ด แปด เก้า); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | return $units[$positive] if ($positive >= 0 && $positive <= 9); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$positive >= 0 and $positive <= 9
MC/DC0%0/2
$positive >= 0 and $positive <= 9
$positive >= 0
$positive <= 9
Truth table33%1/3
A and B
A= $positive >= 0B= $positive <= 9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | return 'สิบ' if ($positive == 10); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $remain; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # Helper: append remainder, substituting เอ็ด for bare หนึ่ง in compounds. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # In Thai, the digit 1 in the units position of any compound number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # is always เอ็ด (et), never หนึ่ง (nueng). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $append_remain = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | my ($so_far, $r) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ($r == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | return $so_far . 'เอ็ด'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | return $so_far . num2tha_cardinal($r); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($positive > 10 && $positive < 20) { # 11 .. 19 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 7 branches
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $remain = $positive % 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $out = 'สิบ'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $out .= $remain == 1 ? 'เอ็ด' : $units[$remain]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | ▶ | elsif ($positive >= 20 && $positive < 100) { # 20 .. 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$positive >= 20 and $positive < 100
MC/DC0%0/2
$positive >= 20 and $positive < 100
$positive >= 20
$positive < 100
Truth table0%0/3
A and B
A= $positive >= 20B= $positive < 100
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $tens = int($positive / 10); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $remain = $positive % 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $out = $tens == 2 ? 'ยี่' : $units[$tens]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $out .= 'สิบ'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($remain) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $out .= $remain == 1 ? 'เอ็ด' : $units[$remain]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | ▶ | elsif ($positive >= 100 && $positive < 1000) { # 100 .. 999 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$positive >= 100 and $positive < 1000
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $hundreds = int($positive / 100); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $remain = $positive % 100; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $out = $units[$hundreds] . 'ร้อย'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $out = $append_remain->($out, $remain) if $remain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ▶ | elsif ($positive >= 1000 && $positive < 10_000) { # 1_000 .. 9_999 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$positive >= 1000 and $positive < 10000
MC/DC0%0/2
$positive >= 1000 and $positive < 10000
$positive >= 1000
$positive < 10000
Truth table0%0/3
A and B
A= $positive >= 1000B= $positive < 10000
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $thousands = int($positive / 1000); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $remain = $positive % 1000; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $out = $units[$thousands] . 'พัน'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $out = $append_remain->($out, $remain) if $remain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | ▶ | elsif ($positive >= 10_000 && $positive < 100_000) { # 10_000 .. 99_999 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$positive >= 10000 and $positive < 100000
MC/DC0%0/2
$positive >= 10000 and $positive < 100000
$positive >= 10000
$positive < 100000
Truth table0%0/3
A and B
A= $positive >= 10000B= $positive < 100000
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $ten_thousands = int($positive / 10000); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $remain = $positive % 10000; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $out = $units[$ten_thousands] . 'หมื่น'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $out = $append_remain->($out, $remain) if $remain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ▶ | elsif ($positive >= 100_000 && $positive < 1_000_000) { # 100_000 .. 999_999 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$positive >= 100000 and $positive < 1000000
MC/DC0%0/2
$positive >= 100000 and $positive < 1000000
$positive >= 100000
$positive < 1000000
Truth table0%0/3
A and B
A= $positive >= 100000B= $positive < 1000000
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my $hundred_thousands = int($positive / 100000); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $remain = $positive % 100000; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $out = $units[$hundred_thousands] . 'แสน'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $out = $append_remain->($out, $remain) if $remain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | ▶ | elsif ($positive >= 1_000_000 && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$positive >= 1000000 and $positive < 1000000000
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $millions = int($positive / 1_000_000); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $remain = $positive % 1_000_000; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $out = num2tha_cardinal($millions) . 'ล้าน'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $out = $append_remain->($out, $remain) if $remain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # }}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # {{{ num2tha_ordinal convert number to ordinal text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub num2tha_ordinal :Export { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
num2tha_ordinal: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $number = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | croak 'You should specify a number from interval [1, 999_999_999]' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
not defined $number && $number =~ /\A\d+\z/msux or $number < 1 or $number > 999999999
not defined $number && $number =~ /\A\d+\z/msux or $number < 1
defined $number && $number =~ /\A\d+\z/msux
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | if !defined $number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | || $number !~ m{\A\d+\z}xms | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | || $number < 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | || $number > 999_999_999; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # Thai ordinals: ที่ (thi) + cardinal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | my $cardinal = num2tha_cardinal($number); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | return 'ที่' . $cardinal; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # }}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # {{{ capabilities declare supported features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub capabilities { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
capabilities: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | cardinal => 1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | ordinal => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # }}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||