File Coverage

blib/lib/Lingua/NLD/Word2Num.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition 2 2 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 35 35 100.0


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8; -*-
2              
3             package Lingua::NLD::Word2Num;
4             # ABSTRACT: Word 2 number conversion in NLD.
5              
6             # {{{ use block
7              
8 1     1   131203 use 5.10.1;
  1         4  
9              
10 1     1   7 use strict;
  1         2  
  1         38  
11 1     1   4 use warnings;
  1         16  
  1         78  
12              
13 1     1   688 use Export::Attrs;
  1         13164  
  1         8  
14 1     1   1387 use Parse::RecDescent;
  1         60594  
  1         10  
15              
16             # }}}
17             # {{{ variable declarations
18             our $VERSION = '0.2603230';
19             my $parser = nld_numerals();
20              
21             # }}}
22             # {{{ w2n convert number to text
23              
24             sub w2n :Export {
25 9   100 9 1 477199 my $input = shift // return;
26              
27 8         38 $input = lc $input;
28 8         44 $input =~ s/,//g;
29 8         43 $input =~ s/ //g;
30              
31 8         129 return $parser->numeral($input);
32 1     1   194 }
  1         2  
  1         9  
33              
34             # }}}
35             # {{{ nld_numerals create parser for numerals
36              
37             sub nld_numerals {
38 1     1 1 7 return Parse::RecDescent->new(q{
39             numeral:
40             numeral: million { return $item[1]; } # root parse. go from maximum to minimum value
41             | millenium { return $item[1]; }
42             | century { return $item[1]; }
43             | decade { return $item[1]; }
44             | { return undef; }
45              
46             number: 'negentien' { $return = 19; } # try to find a word from 0 to 19
47             | 'achtien' { $return = 18; }
48             | 'zeventien' { $return = 17; }
49             | 'zestien' { $return = 16; }
50             | 'vijftien' { $return = 15; }
51             | 'veertien' { $return = 14; }
52             | 'dertien' { $return = 13; }
53             | 'twaalf' { $return = 12; }
54             | 'elf' { $return = 11; }
55             | 'tien' { $return = 10; }
56             | 'negen' { $return = 9; }
57             | 'acht' { $return = 8; }
58             | 'zeven' { $return = 7; }
59             | 'zes' { $return = 6; }
60             | 'vijf' { $return = 5; }
61             | 'vier' { $return = 4; }
62             | 'drie' { $return = 3; }
63             | 'twee' { $return = 2; }
64             | /(een|één)/ { $return = 1; }
65             | 'nul' { $return = 0; }
66              
67             tens: 'twintig' { $return = 20; } # try to find a word that representates
68             | 'dertig' { $return = 30; } # values 20,30,..,90
69             | 'veertig' { $return = 40; }
70             | 'vijftig' { $return = 50; }
71             | 'zestig' { $return = 60; }
72             | 'zeventig' { $return = 70; }
73             | 'tachtig' { $return = 80; }
74             | 'negentig' { $return = 90; }
75              
76             decade: number(?) 'en' tens(?) number(?) # try to find words that represents values
77             { $return = 0; # from 0 to 99
78             for (@item) {
79             $return += $$_[0] if (ref $_ && defined $$_[0]);
80             }
81             }
82             | tens(?) number(?)
83             { $return = -1;
84             for (@item) {
85             if (ref $_ && defined $$_[0]) {
86             $return += $$_[0] if ($return != -1);
87             $return = $$_[0] if ($return == -1);
88             }
89             }
90             $return = undef if ($return == -1);
91             }
92              
93             century: number(?) 'honderd' decade(?) # try to find words that represents values
94             { $return = 0; # from 100 to 999
95             for (@item) {
96             if (ref $_ && defined $$_[0]) {
97             $return += $$_[0];
98             } elsif ($_ eq "honderd") {
99             $return = ($return>0) ? $return * 100 : 100;
100             }
101             }
102             $return ||= undef;
103             }
104              
105             millenium: century(?) decade(?) 'duizend' century(?) decade(?) # try to find words that represents values
106             { $return = 0; # from 1.000 to 999.999
107             for (@item) {
108             if (ref $_ && defined $$_[0]) {
109             $return += $$_[0];
110             } elsif ($_ eq "duizend") {
111             $return = ($return>0) ? $return * 1000 : 1000;
112             }
113             }
114             $return ||= undef;
115             }
116              
117             million: millenium(?) century(?) decade(?) # try to find words that represents values
118             'miljoen' # from 1.000.000 to 999.999.999.999
119             millenium(?) century(?) decade(?)
120             { $return = 0;
121             for (@item) {
122             if (ref $_ && defined $$_[0]) {
123             $return += $$_[0];
124             } elsif ($_ eq "miljoen") {
125             $return = ($return>0) ? $return * 1000000 : 1000000;
126             }
127             }
128             $return ||= undef;
129             }
130             });
131             }
132              
133             # }}}
134              
135             1;
136              
137             __END__