File Coverage

blib/lib/Lingua/NLD/Word2Num.pm
Criterion Covered Total %
statement 26 33 78.7
branch 0 10 0.0
condition 2 4 50.0
subroutine 9 10 90.0
pod 3 3 100.0
total 40 60 66.6


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 to number conversion in Dutch
5              
6 1     1   83475 use 5.16.0;
  1         3  
7 1     1   8 use utf8;
  1         2  
  1         14  
8 1     1   41 use warnings;
  1         2  
  1         58  
9              
10             # {{{ use block
11              
12 1     1   581 use Export::Attrs;
  1         9421  
  1         5  
13 1     1   1259 use Parse::RecDescent;
  1         37404  
  1         8  
14              
15             # }}}
16             # {{{ variable declarations
17             our $VERSION = '0.2603300';
18             my $parser = nld_numerals();
19              
20             # }}}
21             # {{{ w2n convert text to number
22              
23             sub w2n :Export {
24 9   100 9 1 491022 my $input = shift // return;
25              
26 8         37 $input = lc $input;
27 8         38 $input =~ s/,//g;
28 8         36 $input =~ s/ //g;
29              
30 8         94 return $parser->numeral($input);
31 1     1   174 }
  1         2  
  1         9  
32              
33             # }}}
34             # {{{ nld_numerals create parser for numerals
35              
36             sub nld_numerals {
37 1     1 1 5 return Parse::RecDescent->new(q{
38             numeral:
39             numeral: million { return $item[1]; } # root parse. go from maximum to minimum value
40             | millenium { return $item[1]; }
41             | century { return $item[1]; }
42             | decade { return $item[1]; }
43             | { return undef; }
44              
45             number: 'negentien' { $return = 19; } # try to find a word from 0 to 19
46             | 'achtien' { $return = 18; }
47             | 'zeventien' { $return = 17; }
48             | 'zestien' { $return = 16; }
49             | 'vijftien' { $return = 15; }
50             | 'veertien' { $return = 14; }
51             | 'dertien' { $return = 13; }
52             | 'twaalf' { $return = 12; }
53             | 'elf' { $return = 11; }
54             | 'tien' { $return = 10; }
55             | 'negen' { $return = 9; }
56             | 'acht' { $return = 8; }
57             | 'zeven' { $return = 7; }
58             | 'zes' { $return = 6; }
59             | 'vijf' { $return = 5; }
60             | 'vier' { $return = 4; }
61             | 'drie' { $return = 3; }
62             | 'twee' { $return = 2; }
63             | /(een|één)/ { $return = 1; }
64             | 'nul' { $return = 0; }
65              
66             tens: 'twintig' { $return = 20; } # try to find a word that represents
67             | 'dertig' { $return = 30; } # values 20,30,..,90
68             | 'veertig' { $return = 40; }
69             | 'vijftig' { $return = 50; }
70             | 'zestig' { $return = 60; }
71             | 'zeventig' { $return = 70; }
72             | 'tachtig' { $return = 80; }
73             | 'negentig' { $return = 90; }
74              
75             decade: number(?) 'en' tens(?) number(?) # try to find words that represents values
76             { $return = 0; # from 0 to 99
77             for (@item) {
78             $return += $$_[0] if (ref $_ && defined $$_[0]);
79             }
80             }
81             | tens(?) number(?)
82             { $return = -1;
83             for (@item) {
84             if (ref $_ && defined $$_[0]) {
85             $return += $$_[0] if ($return != -1);
86             $return = $$_[0] if ($return == -1);
87             }
88             }
89             $return = undef if ($return == -1);
90             }
91              
92             century: number(?) 'honderd' decade(?) # try to find words that represents values
93             { $return = 0; # from 100 to 999
94             for (@item) {
95             if (ref $_ && defined $$_[0]) {
96             $return += $$_[0];
97             } elsif ($_ eq "honderd") {
98             $return = ($return>0) ? $return * 100 : 100;
99             }
100             }
101             $return ||= undef;
102             }
103              
104             millenium: century(?) decade(?) 'duizend' century(?) decade(?) # try to find words that represents values
105             { $return = 0; # from 1.000 to 999.999
106             for (@item) {
107             if (ref $_ && defined $$_[0]) {
108             $return += $$_[0];
109             } elsif ($_ eq "duizend") {
110             $return = ($return>0) ? $return * 1000 : 1000;
111             }
112             }
113             $return ||= undef;
114             }
115              
116             million: millenium(?) century(?) decade(?) # try to find words that represents values
117             'miljoen' # from 1.000.000 to 999.999.999.999
118             millenium(?) century(?) decade(?)
119             { $return = 0;
120             for (@item) {
121             if (ref $_ && defined $$_[0]) {
122             $return += $$_[0];
123             } elsif ($_ eq "miljoen") {
124             $return = ($return>0) ? $return * 1000000 : 1000000;
125             }
126             }
127             $return ||= undef;
128             }
129             });
130             }
131              
132             # }}}
133             # {{{ ordinal2cardinal convert ordinal text to cardinal text
134              
135             sub ordinal2cardinal :Export {
136 0   0 0 1   my $input = shift // return;
137              
138             # Inverse of Dutch ordinal morphology: restore cardinal from ordinal text.
139             # Irregulars (standalone or as final element of compound)
140 0 0         $input =~ s{eerste\z}{een}xms and return $input;
141 0 0         $input =~ s{tweede\z}{twee}xms and return $input;
142 0 0         $input =~ s{derde\z}{drie}xms and return $input;
143              
144             # Regular: strip -ste (20+ and compounds ending in tens/honderd/duizend)
145 0 0         $input =~ s{ste\z}{}xms and return $input;
146              
147             # Regular: strip -de (4-19)
148 0 0         $input =~ s{de\z}{}xms and return $input;
149              
150 0           return; # not an ordinal
151 1     1   497 }
  1         2  
  1         4  
152              
153             # }}}
154              
155             1;
156              
157             __END__