| 1 | # For Emacs: -*- mode:cperl; mode:folding -*- | ||||||||||||
| 2 | |||||||||||||
| 3 | package Lingua::AFR::Word2Num; | ||||||||||||
| 4 | # ABSTRACT: Word 2 number conversion in AFR. | ||||||||||||
| 5 | |||||||||||||
| 6 | # {{{ use block | ||||||||||||
| 7 | # | ||||||||||||
| 8 | |||||||||||||
| 9 | 1 | ▶ | use 5.10.1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 1 | ▶ | use Perl6::Export::Attrs; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 1 | ▶ | use Parse::RecDescent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | # }}} | ||||||||||||
| 20 | # {{{ variable declarations | ||||||||||||
| 21 | |||||||||||||
| 22 | 1 | our $VERSION = 0.1106; | |||||||||||
| 23 | |||||||||||||
| 24 | 1 | our $INFO = { | |||||||||||
| 25 | rev => '$Rev: 1106 $', | ||||||||||||
| 26 | }; | ||||||||||||
| 27 | |||||||||||||
| 28 | 1 | my $parser = af_numerals(); | |||||||||||
| 29 | # }}} | ||||||||||||
| 30 | |||||||||||||
| 31 | # {{{ w2n convert number to text | ||||||||||||
| 32 | # | ||||||||||||
| 33 | sub w2n :Export { | ||||||||||||
| 34 | 3 | ▶ | my $input = shift // return; | ||||||||||
|
Subroutine
w2n: called CC=2 SCAR=7.0
Pod
documented
Condition50%1/2
shift() // (return)
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 35 | |||||||||||||
| 36 | 3 | $input =~ s/,//g; | |||||||||||
| 37 | 3 | $input =~ s/ //g; | |||||||||||
| 38 | |||||||||||||
| 39 | 3 | return $parser->numeral($input); | |||||||||||
| 40 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 41 | # }}} | ||||||||||||
| 42 | # {{{ af_numerals create parser for numerals | ||||||||||||
| 43 | |||||||||||||
| 44 | sub af_numerals { | ||||||||||||
| 45 | 1 | ▶ | return Parse::RecDescent->new(q{ | ||||||||||
|
Subroutine
af_numerals: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 46 | numeral: scrap { return undef; } # root parse. go from maximum to minimum value | ||||||||||||
| 47 | | million { return $item[1]; } # scrap is a container rule for cases out of bounds | ||||||||||||
| 48 | | millenium { return $item[1]; } | ||||||||||||
| 49 | | century { return $item[1]; } | ||||||||||||
| 50 | | decade { return $item[1]; } | ||||||||||||
| 51 | |||||||||||||
| 52 | number: 'nul' { $return = 0; } # try to find a word from 0 to 19 | ||||||||||||
| 53 | | 'een' { $return = 1; } | ||||||||||||
| 54 | | 'twee' { $return = 2; } | ||||||||||||
| 55 | | 'drie' { $return = 3; } | ||||||||||||
| 56 | | 'vier' { $return = 4; } | ||||||||||||
| 57 | | 'vyf' { $return = 5; } | ||||||||||||
| 58 | | 'ses' { $return = 6; } | ||||||||||||
| 59 | | 'sewe' { $return = 7; } | ||||||||||||
| 60 | | 'agt' { $return = 8; } | ||||||||||||
| 61 | | 'nege' { $return = 9; } | ||||||||||||
| 62 | | 'tien' { $return = 10; } | ||||||||||||
| 63 | | 'elf' { $return = 11; } | ||||||||||||
| 64 | | 'twaalf' { $return = 12; } | ||||||||||||
| 65 | | 'dertien' { $return = 13; } | ||||||||||||
| 66 | | 'viertien' { $return = 14; } | ||||||||||||
| 67 | | 'vyftien' { $return = 15; } | ||||||||||||
| 68 | | 'sestien' { $return = 16; } | ||||||||||||
| 69 | | 'sewentien' { $return = 17; } | ||||||||||||
| 70 | | 'agtien' { $return = 18; } | ||||||||||||
| 71 | | 'negentien' { $return = 19; } | ||||||||||||
| 72 | |||||||||||||
| 73 | tens: 'twintig' { $return = 20; } # try to find a word that representates | ||||||||||||
| 74 | | 'dertig' { $return = 30; } # values 20,30,..,90 | ||||||||||||
| 75 | | 'viertig' { $return = 40; } | ||||||||||||
| 76 | | 'vyftig' { $return = 50; } | ||||||||||||
| 77 | | 'sestig' { $return = 60; } | ||||||||||||
| 78 | | 'sewentig' { $return = 70; } | ||||||||||||
| 79 | | 'tagtig' { $return = 80; } | ||||||||||||
| 80 | | 'negentig' { $return = 90; } | ||||||||||||
| 81 | |||||||||||||
| 82 | decade: number(?) 'en' tens(?) # try to find words that represents values | ||||||||||||
| 83 | { $return = -1; # from 0 to 99 | ||||||||||||
| 84 | for (@item) { | ||||||||||||
| 85 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 86 | $return += $$_[0] if ($return != -1); # -1 if the non-zero identifier, since | ||||||||||||
| 87 | $return = $$_[0] if ($return == -1); # zero is a valid result | ||||||||||||
| 88 | } | ||||||||||||
| 89 | } | ||||||||||||
| 90 | $return = undef if ($return == -1); | ||||||||||||
| 91 | } | ||||||||||||
| 92 | | number(?) tens(?) | ||||||||||||
| 93 | { $return = -1; | ||||||||||||
| 94 | for (@item) { | ||||||||||||
| 95 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 96 | $return += $$_[0] if ($return != -1); # -1 if the non-zero identifier, since | ||||||||||||
| 97 | $return = $$_[0] if ($return == -1); # zero is a valid result | ||||||||||||
| 98 | } | ||||||||||||
| 99 | } | ||||||||||||
| 100 | $return = undef if ($return == -1); | ||||||||||||
| 101 | } | ||||||||||||
| 102 | | 'en' number(?) | ||||||||||||
| 103 | { $return = -1; | ||||||||||||
| 104 | for (@item) { | ||||||||||||
| 105 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 106 | $return += $$_[0] if ($return != -1); # -1 if the non-zero identifier, since | ||||||||||||
| 107 | $return = $$_[0] if ($return == -1); # zero is a valid result | ||||||||||||
| 108 | } | ||||||||||||
| 109 | } | ||||||||||||
| 110 | $return = undef if ($return == -1); | ||||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | |||||||||||||
| 114 | century: number(?) 'honderd' decade(?) # try to find words that represents values | ||||||||||||
| 115 | { $return = 0; # from 100 to 999 | ||||||||||||
| 116 | for (@item) { | ||||||||||||
| 117 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 118 | $return += $$_[0]; | ||||||||||||
| 119 | } elsif ($_ eq "honderd") { | ||||||||||||
| 120 | $return = ($return>0) ? $return * 100 : 100; | ||||||||||||
| 121 | } | ||||||||||||
| 122 | } | ||||||||||||
| 123 | $return = undef if (!$return); | ||||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | millenium: century(?) decade(?) 'duisend' century(?) decade(?) # try to find words that represents values | ||||||||||||
| 127 | { $return = 0; # from 1.000 to 999.999 | ||||||||||||
| 128 | for (@item) { | ||||||||||||
| 129 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 130 | $return += $$_[0]; | ||||||||||||
| 131 | } elsif ($_ eq "duisend") { | ||||||||||||
| 132 | $return = ($return>0) ? $return * 1000 : 1000; | ||||||||||||
| 133 | } | ||||||||||||
| 134 | } | ||||||||||||
| 135 | $return = undef if (!$return); | ||||||||||||
| 136 | } | ||||||||||||
| 137 | |||||||||||||
| 138 | million: millenium(?) century(?) decade(?) # try to find words that represents values | ||||||||||||
| 139 | 'miljoen' # from 1.000.000 to 999.999.999.999 | ||||||||||||
| 140 | millenium(?) century(?) decade(?) | ||||||||||||
| 141 | { $return = 0; | ||||||||||||
| 142 | for (@item) { | ||||||||||||
| 143 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 144 | $return += $$_[0]; | ||||||||||||
| 145 | } elsif ($_ eq "miljoen") { | ||||||||||||
| 146 | $return = ($return>0) ? $return * 1000000 : 1000000; | ||||||||||||
| 147 | } | ||||||||||||
| 148 | } | ||||||||||||
| 149 | $return = undef if (!$return); | ||||||||||||
| 150 | } | ||||||||||||
| 151 | |||||||||||||
| 152 | scrap: million(?) millenium(?) century(?) decade(?) # if there is something else then the numerals defined above | ||||||||||||
| 153 | /(.+)/ | ||||||||||||
| 154 | million(?) millenium(?) century(?) decade(?) # return undef and give a word of warning | ||||||||||||
| 155 | { | ||||||||||||
| 156 | carp("unknown numeral '$1' !\n"); | ||||||||||||
| 157 | } | ||||||||||||
| 158 | }); | ||||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | # }}} | ||||||||||||
| 162 | |||||||||||||
| 163 | 1 | 1; | |||||||||||
| 164 | |||||||||||||
| 165 | __END__ | ||||||||||||