| 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 | use 5.16.0; | ||
| 6 | use utf8; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | # {{{ use block | ||
| 10 | |||
| 11 | use Export::Attrs; | ||
| 12 | use Parse::RecDescent; | ||
| 13 | |||
| 14 | # }}} | ||
| 15 | # {{{ var block | ||
| 16 | our $VERSION = '0.2603260'; | ||
| 17 | my $parser = tha_numerals(); | ||
| 18 | |||
| 19 | # }}} | ||
| 20 | |||
| 21 | # {{{ w2n convert text to number | ||
| 22 | |||
| 23 | sub w2n :Export { | ||
| 24 | my $input = shift // return; | ||
| 25 | |||
| 26 | return $parser->numeral($input); | ||
| 27 | } | ||
| 28 | |||
| 29 | # }}} | ||
| 30 | # {{{ tha_numerals create parser for thai numerals | ||
| 31 | |||
| 32 | sub tha_numerals { | ||
| 33 | 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 | |||
| 106 | 1; | ||
| 107 | |||
| 108 | __END__ |