File Coverage

blib/lib/Lingua/THA/Word2Num.pm
Criterion Covered Total %
statement 23 26 88.4
branch 0 2 0.0
condition 2 4 50.0
subroutine 9 10 90.0
pod 3 3 100.0
total 37 45 82.2


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2             package Lingua::THA::Word2Num;
3             # ABSTRACT: Word to number conversion in Thai
4              
5 1     1   128464 use 5.16.0;
  1         4  
6 1     1   4 use utf8;
  1         3  
  1         12  
7 1     1   29 use warnings;
  1         2  
  1         68  
8              
9             # {{{ use block
10              
11 1     1   582 use Export::Attrs;
  1         12847  
  1         8  
12 1     1   1407 use Parse::RecDescent;
  1         50447  
  1         11  
13              
14             # }}}
15             # {{{ var block
16             our $VERSION = '0.2603300';
17             my $parser = tha_numerals();
18              
19             # }}}
20              
21             # {{{ w2n convert text to number
22              
23             sub w2n :Export {
24 2   100 2 1 275930 my $input = shift // return;
25              
26 1         19 return $parser->numeral($input);
27 1     1   139 }
  1         2  
  1         8  
28              
29             # }}}
30             # {{{ tha_numerals create parser for thai numerals
31              
32             sub tha_numerals {
33 1     1 1 7 return Parse::RecDescent->new(q{
34             numeral: mega
35             | saen_group
36             | { }
37              
38             # --- units ---
39             unit: 'เก้า' { 9 }
40             | 'แปด' { 8 }
41             | 'เจ็ด' { 7 }
42             | 'หก' { 6 }
43             | 'ห้า' { 5 }
44             | 'สี่' { 4 }
45             | 'สาม' { 3 }
46             | 'สอง' { 2 }
47             | 'หนึ่ง' { 1 }
48              
49             # --- zero (standalone only) ---
50             zero: 'ศูนย์' { 0 }
51              
52             # --- ones in compound (เอ็ด replaces หนึ่ง) ---
53             compound_one: 'เอ็ด' { 1 }
54             | unit
55              
56             # --- tens digit (ยี่ replaces สอง for 20s) ---
57             tens_digit: 'ยี่' { 2 }
58             | unit
59              
60             # --- สิบ (ten) constructs ---
61             deca: tens_digit 'สิบ' compound_one { $item[1] * 10 + $item[3] }
62             | tens_digit 'สิบ' { $item[1] * 10 }
63             | 'สิบ' compound_one { 10 + $item[2] }
64             | 'สิบ' { 10 }
65              
66             deca_or_unit: deca
67             | compound_one
68             | zero
69              
70             # --- ร้อย (hundred) ---
71             roi: unit 'ร้อย' deca_or_unit { $item[1] * 100 + $item[3] }
72             | unit 'ร้อย' { $item[1] * 100 }
73              
74             roi_group: roi
75             | deca_or_unit
76              
77             # --- พัน (thousand) ---
78             phan: unit 'พัน' roi_group { $item[1] * 1000 + $item[3] }
79             | unit 'พัน' { $item[1] * 1000 }
80              
81             phan_group: phan
82             | roi_group
83              
84             # --- หมื่น (ten thousand) ---
85             muen: unit 'หมื่น' phan_group { $item[1] * 10_000 + $item[3] }
86             | unit 'หมื่น' { $item[1] * 10_000 }
87              
88             muen_group: muen
89             | phan_group
90              
91             # --- แสน (hundred thousand) ---
92             saen: unit 'แสน' muen_group { $item[1] * 100_000 + $item[3] }
93             | unit 'แสน' { $item[1] * 100_000 }
94              
95             saen_group: saen
96             | muen_group
97              
98             # --- ล้าน (million) ---
99             mega: saen_group 'ล้าน' saen_group { $item[1] * 1_000_000 + $item[3] }
100             | saen_group 'ล้าน' { $item[1] * 1_000_000 }
101             });
102             }
103              
104             # }}}
105             # {{{ ordinal2cardinal convert ordinal text to cardinal text
106              
107             sub ordinal2cardinal :Export {
108 0   0 0 1   my $input = shift // return;
109              
110             # Thai ordinals: prefix ที่ (thîi) to cardinal.
111             # Strip ที่ prefix; return cardinal remainder.
112 0 0         $input =~ s{\Aที่}{}xms and return $input;
113              
114 0           return; # not an ordinal
115 1     1   489 }
  1         2  
  1         4  
116              
117             # }}}
118              
119             1;
120              
121             __END__