| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||||||||||||
| 2 | package Lingua::ARA::Word2Num; | ||||||||||||
| 3 | # ABSTRACT: Word to number conversion in Arabic | ||||||||||||
| 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 Export::Attrs; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use Parse::RecDescent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | # }}} | ||||||||||||
| 15 | # {{{ var block | ||||||||||||
| 16 | 1 | our $VERSION = '0.2603270'; | |||||||||||
| 17 | 1 | my $parser = ara_numerals(); | |||||||||||
| 18 | |||||||||||||
| 19 | # }}} | ||||||||||||
| 20 | |||||||||||||
| 21 | # {{{ w2n convert text to number | ||||||||||||
| 22 | |||||||||||||
| 23 | sub w2n :Export { | ||||||||||||
| 24 | 2 | ▶ | my $input = shift // return; | ||||||||||
|
Subroutine
w2n: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
shift() // (return)
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 25 | |||||||||||||
| 26 | # Normalize whitespace and strip diacritics (tashkeel) | ||||||||||||
| 27 | 1 | $input =~ s/[ً-ْٰ]//g; # remove tashkeel | |||||||||||
| 28 | 1 | $input =~ s/\A\s+//; | |||||||||||
| 29 | 1 | $input =~ s/\s+\z//; | |||||||||||
| 30 | 1 | $input =~ s/\s+/ /g; | |||||||||||
| 31 | |||||||||||||
| 32 | 1 | return $parser->numeral($input); | |||||||||||
| 33 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | |||||||||||||
| 35 | # }}} | ||||||||||||
| 36 | # {{{ ara_numerals create parser for arabic numerals | ||||||||||||
| 37 | |||||||||||||
| 38 | sub ara_numerals { | ||||||||||||
| 39 | 1 | ▶ | return Parse::RecDescent->new(q{ | ||||||||||
|
Subroutine
ara_numerals: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 40 | <autoaction: { $item[1] } > | ||||||||||||
| 41 | |||||||||||||
| 42 | numeral: mega | ||||||||||||
| 43 | | kOhOd | ||||||||||||
| 44 | | { } | ||||||||||||
| 45 | |||||||||||||
| 46 | # --- Units --- | ||||||||||||
| 47 | number: "صفر" { 0 } | ||||||||||||
| 48 | | "واحد" { 1 } | ||||||||||||
| 49 | | "اثنان" { 2 } | ||||||||||||
| 50 | | "اثنين" { 2 } | ||||||||||||
| 51 | | "ثلاثة" { 3 } | ||||||||||||
| 52 | | "أربعة" { 4 } | ||||||||||||
| 53 | | "خمسة" { 5 } | ||||||||||||
| 54 | | "ستة" { 6 } | ||||||||||||
| 55 | | "سبعة" { 7 } | ||||||||||||
| 56 | | "ثمانية" { 8 } | ||||||||||||
| 57 | | "تسعة" { 9 } | ||||||||||||
| 58 | |||||||||||||
| 59 | ten: "عشرة" { 10 } | ||||||||||||
| 60 | |||||||||||||
| 61 | # --- Teens (11-19) --- | ||||||||||||
| 62 | # 11: أحد عشر | ||||||||||||
| 63 | teen: "أحد عشر" { 11 } | ||||||||||||
| 64 | | "اثنا عشر" { 12 } | ||||||||||||
| 65 | | "ثلاثة عشر" { 13 } | ||||||||||||
| 66 | | "أربعة عشر" { 14 } | ||||||||||||
| 67 | | "خمسة عشر" { 15 } | ||||||||||||
| 68 | | "ستة عشر" { 16 } | ||||||||||||
| 69 | | "سبعة عشر" { 17 } | ||||||||||||
| 70 | | "ثمانية عشر" { 18 } | ||||||||||||
| 71 | | "تسعة عشر" { 19 } | ||||||||||||
| 72 | |||||||||||||
| 73 | # --- Tens (20-90) --- | ||||||||||||
| 74 | tens: "عشرون" { 20 } | ||||||||||||
| 75 | | "ثلاثون" { 30 } | ||||||||||||
| 76 | | "أربعون" { 40 } | ||||||||||||
| 77 | | "خمسون" { 50 } | ||||||||||||
| 78 | | "ستون" { 60 } | ||||||||||||
| 79 | | "سبعون" { 70 } | ||||||||||||
| 80 | | "ثمانون" { 80 } | ||||||||||||
| 81 | | "تسعون" { 90 } | ||||||||||||
| 82 | |||||||||||||
| 83 | # --- Connector (wa = and) --- | ||||||||||||
| 84 | wa: "و" | ||||||||||||
| 85 | |||||||||||||
| 86 | # --- Deca: units + wa + tens (Arabic order) or standalone --- | ||||||||||||
| 87 | deca: number wa tens { $item[1] + $item[3] } | ||||||||||||
| 88 | | wa deca { $item[2] } | ||||||||||||
| 89 | | teen | ||||||||||||
| 90 | | tens | ||||||||||||
| 91 | | ten | ||||||||||||
| 92 | | number | ||||||||||||
| 93 | |||||||||||||
| 94 | # --- Hundreds --- | ||||||||||||
| 95 | # مئتان = 200 (dual) | ||||||||||||
| 96 | mi_atan: "مئتان" { 200 } | ||||||||||||
| 97 | |||||||||||||
| 98 | # Compound hundreds: ثلاثمئة etc. (no space) | ||||||||||||
| 99 | hund_pfx: "ثلاث" { 3 } | ||||||||||||
| 100 | | "أربع" { 4 } | ||||||||||||
| 101 | | "خمس" { 5 } | ||||||||||||
| 102 | | "ست" { 6 } | ||||||||||||
| 103 | | "سبع" { 7 } | ||||||||||||
| 104 | | "ثمان" { 8 } | ||||||||||||
| 105 | | "تسع" { 9 } | ||||||||||||
| 106 | |||||||||||||
| 107 | # مئة (mi'a) or مائة (maa'a) - both spellings | ||||||||||||
| 108 | mi_a: "مئة" | ||||||||||||
| 109 | | "مائة" | ||||||||||||
| 110 | |||||||||||||
| 111 | hecto: mi_atan wa deca { 200 + $item[3] } | ||||||||||||
| 112 | | mi_atan { 200 } | ||||||||||||
| 113 | | hund_pfx mi_a wa deca { $item[1] * 100 + $item[4] } | ||||||||||||
| 114 | | hund_pfx mi_a { $item[1] * 100 } | ||||||||||||
| 115 | | mi_a wa deca { 100 + $item[3] } | ||||||||||||
| 116 | | mi_a { 100 } | ||||||||||||
| 117 | |||||||||||||
| 118 | hOd: hecto | ||||||||||||
| 119 | | deca | ||||||||||||
| 120 | |||||||||||||
| 121 | # --- Thousands --- | ||||||||||||
| 122 | # ألفان = 2000 (dual) | ||||||||||||
| 123 | alfan: "ألفان" { 2000 } | ||||||||||||
| 124 | |||||||||||||
| 125 | # ألف (alf) - singular | ||||||||||||
| 126 | alf: "ألف" | ||||||||||||
| 127 | |||||||||||||
| 128 | # آلاف (alaf) - plural (3-10) | ||||||||||||
| 129 | alaf: "آلاف" | ||||||||||||
| 130 | |||||||||||||
| 131 | # alf_or_alaf: singular or plural thousand marker | ||||||||||||
| 132 | alf_any: alaf | alf | ||||||||||||
| 133 | |||||||||||||
| 134 | kilo: alfan wa hOd { 2000 + $item[3] } | ||||||||||||
| 135 | | alfan { 2000 } | ||||||||||||
| 136 | | hOd alf_any wa hOd { $item[1] * 1000 + $item[4] } | ||||||||||||
| 137 | | hOd alf_any { $item[1] * 1000 } | ||||||||||||
| 138 | | alf wa hOd { 1000 + $item[3] } | ||||||||||||
| 139 | | alf { 1000 } | ||||||||||||
| 140 | |||||||||||||
| 141 | kOhOd: kilo | ||||||||||||
| 142 | | hOd | ||||||||||||
| 143 | |||||||||||||
| 144 | # --- Millions --- | ||||||||||||
| 145 | # مليونان = 2_000_000 (dual) | ||||||||||||
| 146 | milyunan: "مليونان" { 2_000_000 } | ||||||||||||
| 147 | |||||||||||||
| 148 | # مليون (milyun) - singular | ||||||||||||
| 149 | milyun: "مليون" | ||||||||||||
| 150 | |||||||||||||
| 151 | # ملايين (malayin) - plural (3-10) | ||||||||||||
| 152 | malayin: "ملايين" | ||||||||||||
| 153 | |||||||||||||
| 154 | # milyun_or_malayin: singular or plural million marker | ||||||||||||
| 155 | mily_any: malayin | milyun | ||||||||||||
| 156 | |||||||||||||
| 157 | mega: milyunan wa kOhOd { 2_000_000 + $item[3] } | ||||||||||||
| 158 | | milyunan { 2_000_000 } | ||||||||||||
| 159 | | hOd mily_any wa kOhOd { $item[1] * 1_000_000 + $item[4] } | ||||||||||||
| 160 | | hOd mily_any { $item[1] * 1_000_000 } | ||||||||||||
| 161 | | milyun wa kOhOd { 1_000_000 + $item[3] } | ||||||||||||
| 162 | | milyun { 1_000_000 } | ||||||||||||
| 163 | }); | ||||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | # }}} | ||||||||||||
| 167 | |||||||||||||
| 168 | 1 | 1; | |||||||||||
| 169 | |||||||||||||
| 170 | __END__ | ||||||||||||