File Coverage

blib/lib/Lingua/SWE/Word2Num.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition 2 2 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 37 37 100.0


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8; -*-
2              
3             package Lingua::SWE::Word2Num;
4             # ABSTRACT: Word 2 number conversion in SWE.
5              
6             # {{{ use block
7              
8 1     1   25839 use 5.10.1;
  1         4  
  1         47  
9              
10 1     1   6 use strict;
  1         2  
  1         77  
11 1     1   6 use warnings;
  1         12  
  1         29  
12              
13 1     1   4401 use Perl6::Export::Attrs;
  1         10240  
  1         7  
14 1     1   1719 use Parse::RecDescent;
  1         50398  
  1         9  
15              
16 1     1   1056 use encoding 'utf8';
  1         18312  
  1         8  
17              
18             # }}}
19             # {{{ variable declarations
20              
21             our $VERSION = 0.0682;
22             our $INFO = {
23             rev => '$Rev: 682 $',
24             };
25              
26             my $parser = sv_numerals();
27              
28             # }}}
29              
30             # {{{ w2n convert number to text
31              
32             sub w2n :Export {
33 4   100 4 1 82537 my $input = shift // return;
34              
35 3         37 return $parser->numeral($input);
36 1     1   553 }
  1         3  
  1         7  
37              
38             # }}}
39             # {{{ sv_numerals create parser for numerals
40              
41             sub sv_numerals {
42 1     1 1 10 return Parse::RecDescent->new(q{
43              
44             numeral: million { return $item[1]; } # root parse. go from maximum to minimum value
45             | millenium { return $item[1]; }
46             | century { return $item[1]; }
47             | decade { return $item[1]; }
48             | number { return $item[1]; }
49             | { return undef; }
50              
51             number: 'nitton' { $return = 19; } # try to find a word from 0 to 19
52             | 'arton' { $return = 18; }
53             | 'sjutton' { $return = 17; }
54             | 'sexton' { $return = 16; }
55             | 'femton' { $return = 15; }
56             | 'fjorton' { $return = 14; }
57             | 'tretton' { $return = 13; }
58             | 'tolv' { $return = 12; }
59             | 'elva' { $return = 11; }
60             | 'tio' { $return = 10; }
61             | 'nio' { $return = 9; }
62             | 'åtta' { $return = 8; }
63             | 'sju' { $return = 7; }
64             | 'sex' { $return = 6; }
65             | 'fem' { $return = 5; }
66             | 'fyra' { $return = 4; }
67             | 'tre' { $return = 3; }
68             | 'två' { $return = 2; }
69             | 'ett' { $return = 1; }
70             | 'noll' { $return = 0; }
71              
72             tens: 'tjugo' { $return = 20; } # try to find a word that representates
73             | 'trettio' { $return = 30; } # values 20,30,..,90
74             | 'fyrtio' { $return = 40; }
75             | 'femtio' { $return = 50; }
76             | 'sextio' { $return = 60; }
77             | 'sjutio' { $return = 70; }
78             | 'åttio' { $return = 80; }
79             | 'nittio' { $return = 90; }
80              
81             decade: tens(?) number(?) # try to find words that represents values
82             { $return = 0; # from 0 to 99
83             for (@item) {
84             $return += $$_[0] if (ref $_ && defined $$_[0]);
85             }
86             $return = undef if(!$return);
87             }
88             century: number(?) 'hundra' decade(?) # try to find words that represents values
89             { $return = 0; # from 100 to 999
90             for (@item) {
91             if (ref $_ && defined $$_[0]) {
92             $return += $$_[0];
93             } elsif ($_ eq 'hundra') {
94             $return = ($return>0) ? $return * 100 : 100;
95             }
96             }
97             $return = undef if(!$return);
98             }
99             millenium: century(?) decade(?) 'tusen' century(?) decade(?) # try to find words that represents values
100             { $return = 0; # from 1.000 to 999.999
101             for (@item) {
102             if (ref $_ && defined $$_[0]) {
103             $return += $$_[0];
104             } elsif ($_ eq "tusen") {
105             $return = ($return>0) ? $return * 1000 : 1000;
106             }
107             }
108             $return = undef if(!$return);
109             }
110              
111             million: millenium(?) century(?) decade(?) # try to find words that represents values
112             'miljoner' # from 1.000.000 to 999.999.999.999
113             millenium(?) century(?) decade(?)
114             { $return = 0;
115             for (@item) {
116             if (ref $_ && defined $$_[0]) {
117             $return += $$_[0];
118             } elsif ($_ eq 'miljoner') {
119             $return = $return ? $return * 1000000 : 1000000;
120             }
121             }
122             $return = undef if(!$return);
123             }
124             });
125             }
126              
127             # }}}
128              
129             1;
130              
131             __END__