File Coverage

blib/lib/Lingua/SWE/Word2Num.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition 2 2 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 36 36 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::SWE::Word2Num;
4             # ABSTRACT: Word 2 number conversion in SWE.
5              
6             # {{{ use block
7              
8 1     1   108011 use 5.10.1;
  1         4  
9              
10 1     1   7 use strict;
  1         3  
  1         72  
11 1     1   7 use warnings;
  1         19  
  1         74  
12              
13 1     1   712 use Export::Attrs;
  1         11874  
  1         8  
14 1     1   1654 use Parse::RecDescent;
  1         51754  
  1         8  
15              
16 1     1   55 use utf8;
  1         6  
  1         8  
17              
18             # }}}
19             # {{{ variable declarations
20             our $VERSION = '0.2603230';
21             my $parser = sv_numerals();
22              
23             # }}}
24              
25             # {{{ w2n convert number to text
26              
27             sub w2n :Export {
28 4   100 4 1 218979 my $input = shift // return;
29              
30 3         39 return $parser->numeral($input);
31 1     1   88 }
  1         1  
  1         6  
32              
33             # }}}
34             # {{{ sv_numerals create parser for numerals
35              
36             sub sv_numerals {
37 1     1 1 6 return Parse::RecDescent->new(q{
38              
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             | number { return $item[1]; }
44             | { return undef; }
45              
46             number: 'nitton' { $return = 19; } # try to find a word from 0 to 19
47             | 'arton' { $return = 18; }
48             | 'sjutton' { $return = 17; }
49             | 'sexton' { $return = 16; }
50             | 'femton' { $return = 15; }
51             | 'fjorton' { $return = 14; }
52             | 'tretton' { $return = 13; }
53             | 'tolv' { $return = 12; }
54             | 'elva' { $return = 11; }
55             | 'tio' { $return = 10; }
56             | 'nio' { $return = 9; }
57             | 'åtta' { $return = 8; }
58             | 'sju' { $return = 7; }
59             | 'sex' { $return = 6; }
60             | 'fem' { $return = 5; }
61             | 'fyra' { $return = 4; }
62             | 'tre' { $return = 3; }
63             | 'två' { $return = 2; }
64             | 'ett' { $return = 1; }
65             | 'noll' { $return = 0; }
66              
67             tens: 'tjugo' { $return = 20; } # try to find a word that representates
68             | 'trettio' { $return = 30; } # values 20,30,..,90
69             | 'fyrtio' { $return = 40; }
70             | 'femtio' { $return = 50; }
71             | 'sextio' { $return = 60; }
72             | 'sjutio' { $return = 70; }
73             | 'åttio' { $return = 80; }
74             | 'nittio' { $return = 90; }
75              
76             decade: 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             $return = undef if(!$return);
82             }
83             century: number(?) 'hundra' decade(?) # try to find words that represents values
84             { $return = 0; # from 100 to 999
85             for (@item) {
86             if (ref $_ && defined $$_[0]) {
87             $return += $$_[0];
88             } elsif ($_ eq 'hundra') {
89             $return = ($return>0) ? $return * 100 : 100;
90             }
91             }
92             $return = undef if(!$return);
93             }
94             millenium: century(?) decade(?) 'tusen' century(?) decade(?) # try to find words that represents values
95             { $return = 0; # from 1.000 to 999.999
96             for (@item) {
97             if (ref $_ && defined $$_[0]) {
98             $return += $$_[0];
99             } elsif ($_ eq "tusen") {
100             $return = ($return>0) ? $return * 1000 : 1000;
101             }
102             }
103             $return = undef if(!$return);
104             }
105              
106             million: millenium(?) century(?) decade(?) # try to find words that represents values
107             'miljoner' # from 1.000.000 to 999.999.999.999
108             millenium(?) century(?) decade(?)
109             { $return = 0;
110             for (@item) {
111             if (ref $_ && defined $$_[0]) {
112             $return += $$_[0];
113             } elsif ($_ eq 'miljoner') {
114             $return = $return ? $return * 1000000 : 1000000;
115             }
116             }
117             $return = undef if(!$return);
118             }
119             });
120             }
121              
122             # }}}
123              
124             1;
125              
126             __END__