File Coverage

blib/lib/Lingua/ITA/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; mode:folding; coding:utf-8; -*-
2              
3             package Lingua::ITA::Word2Num;
4             # ABSTRACT: Word 2 number conversion in ITA.
5              
6             # {{{ use block
7              
8 1     1   24757 use 5.10.1;
  1         4  
  1         47  
9              
10 1     1   5 use strict;
  1         2  
  1         42  
11 1     1   4 use warnings;
  1         20  
  1         28  
12              
13 1     1   951 use Perl6::Export::Attrs;
  1         23902  
  1         7  
14 1     1   2038 use Parse::RecDescent;
  1         57548  
  1         9  
15              
16             # }}}
17             # {{{ variable declarations
18              
19             our $VERSION = 0.0682;
20             our $INFO = {
21             rev => '$Rev: 682 $',
22             };
23              
24             our @EXPORT_OK = qw(cardinal2num w2n);
25             my $parser = it_numerals();
26              
27             # }}}
28              
29             # {{{ w2n convert number to text
30             #
31             sub w2n :Export {
32 3   100 3 1 62752 my $input = shift // return;
33              
34 2         9 $input =~ s/,//g;
35 2         15 $input =~ s/ //g;
36              
37 2         32 return $parser->numeral($input);
38 1     1   183 }
  1         2  
  1         10  
39             # }}}
40             # {{{ it_numerals create parser for numerals
41             sub it_numerals {
42 1     1 1 6 return Parse::RecDescent->new(q{
43             numeral: million { return $item[1]; } # root parse. go from maximum to minimum value
44             | millenium { return $item[1]; }
45             | century { return $item[1]; }
46             | decade { return $item[1]; }
47             | { return undef; }
48              
49             number: 'zero' { $return = 0; } # try to find a word from 0 to 19
50             | 'diciannove' { $return = 19; }
51             | 'diciotto' { $return = 18; }
52             | 'diciassette' { $return = 17; }
53             | 'sedici' { $return = 16; }
54             | 'quindici' { $return = 15; }
55             | 'quattordici' { $return = 14; }
56             | 'tredici' { $return = 13; }
57             | 'dodici' { $return = 12; }
58             | 'undici' { $return = 11; }
59             | 'dieci' { $return = 10; }
60             | 'nove' { $return = 9; }
61             | 'otto' { $return = 8; }
62             | 'sette' { $return = 7; }
63             | 'sei' { $return = 6; }
64             | 'cinque' { $return = 5; }
65             | 'quattro' { $return = 4; }
66             | 'tre' { $return = 3; }
67             | 'due' { $return = 2; }
68             | 'un' { $return = 1; }
69              
70             tens: 'venti' { $return = 20; } # try to find a word that representates
71             | 'trenta' { $return = 30; } # values 20,30,..,90
72             | 'trent' { $return = 30; }
73             | 'quaranta' { $return = 40; }
74             | 'quarant' { $return = 40; }
75             | 'cinquanta' { $return = 50; }
76             | 'cinquant' { $return = 50; }
77             | 'sessanta' { $return = 60; }
78             | 'sessant' { $return = 60; }
79             | 'settanta' { $return = 70; }
80             | 'settant' { $return = 70; }
81             | 'ottanta' { $return = 80; }
82             | 'ottant' { $return = 80; }
83             | 'novanta' { $return = 90; }
84             | 'novant' { $return = 90; }
85              
86             decade: tens(?) number(?) # try to find words that represents values
87             { $return = -1; # from 0 to 99
88             for (@item) {
89             if (ref $_ && defined $$_[0]) {
90             $return += $$_[0] if ($return != -1); # -1 if the non-zero identifier, since
91             $return = $$_[0] if ($return == -1); # zero is a valid result
92             }
93             }
94             $return = undef if ($return == -1);
95             }
96              
97             century: number(?) 'cento' decade(?) # try to find words that represents values
98             { $return = 0; # from 100 to 999
99             for (@item) {
100             if (ref $_ && defined $$_[0]) {
101             $return += $$_[0];
102             } elsif ($_ eq "cento") {
103             $return = ($return>0) ? $return * 100 : 100;
104             }
105             }
106             $return ||= undef;
107             }
108              
109             millenium: century(?) decade(?) # try to find words that represents values
110             ('mille' | 'mila') # from 1.000 to 999.999
111             century(?) decade(?)
112             { $return = 0;
113             for (@item) {
114             if ($_ eq "mille" || $_ eq "mila") {
115             $return = ($return>0) ? $return * 1000 : 1000;
116             }
117             next if (!ref $_);
118             $return += $$_[0];
119             }
120             $return ||= undef;
121             }
122              
123             million: millenium(?) century(?) decade(?) # try to find words that represents values
124             'milioni' # from 1.000.000 to 999.999.999.999
125             millenium(?) century(?) decade(?)
126             { $return = 0;
127             for (@item) {
128             if ($_ eq "milioni" || $_ eq "milione") {
129             $return = ($return>0) ? $return * 1000000 : 1000000;
130             }
131              
132             next if ( ! ref $_);
133             $return += $$_[0];
134             }
135             $return ||= undef;
136             }
137             });
138             }
139             # }}}
140              
141             1;
142              
143             __END__