File Coverage

blib/lib/Lingua/NOR/Word2Num.pm
Criterion Covered Total %
statement 28 28 100.0
branch 1 2 50.0
condition 4 4 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 44 45 97.7


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8; -*-
2              
3             package Lingua::NOR::Word2Num;
4             # ABSTRACT: Word 2 number conversion in NOR.
5              
6             # {{{ use block
7              
8 1     1   143482 use 5.10.1;
  1         5  
9              
10 1     1   7 use strict;
  1         3  
  1         38  
11 1     1   6 use warnings;
  1         13  
  1         98  
12 1     1   7 use utf8;
  1         4  
  1         8  
13              
14 1     1   726 use Export::Attrs;
  1         13171  
  1         8  
15 1     1   1467 use Parse::RecDescent;
  1         51741  
  1         10  
16              
17             # }}}
18             # {{{ variable declarations
19             our $VERSION = '0.2603230';
20             my $parser = no_numerals();
21              
22             # }}}
23              
24             # {{{ w2n convert number to text
25              
26             sub w2n :Export {
27 4   100 4 1 316575 my $input = shift // return;
28              
29 3         18 $input =~ s/ og / /g; # Spoke only relevant keywords
30 3         11 $input =~ s/ million / millioner /g; # equal
31              
32 3         7 $input =~ s/,//g;
33 3         17 $input =~ s/ //g;
34              
35 3 50       10 return 0 if $input eq 'null';
36              
37 3   100     31 return $parser->numeral($input) || undef;
38 1     1   238 }
  1         3  
  1         8  
39              
40             # }}}
41             # {{{ no_numerals create parser for numerals
42              
43             sub no_numerals {
44 1     1 1 11 return Parse::RecDescent->new (q{
45             numeral:
46             numeral: million { return $item[1]; } # root parse. go from maximum to minimum value
47             | millenium { return $item[1]; }
48             | century { return $item[1]; }
49             | decade { return $item[1]; }
50             | { return undef; }
51              
52             number: 'null' { $return = 0; } # try to find a word from 0 to 19
53             | 'nitten' { $return = 19; }
54             | 'atten' { $return = 18; }
55             | 'sytten' { $return = 17; }
56             | 'seksten' { $return = 16; }
57             | 'femten' { $return = 15; }
58             | 'fjorten' { $return = 14; }
59             | 'tretten' { $return = 13; }
60             | 'tolv' { $return = 12; }
61             | 'ellve' { $return = 11; }
62             | 'ti' { $return = 10; }
63             | 'ni' { $return = 9; }
64             | 'åtte' { $return = 8; }
65             | 'sju' { $return = 7; }
66             | 'seks' { $return = 6; }
67             | 'fem' { $return = 5; }
68             | 'fire' { $return = 4; }
69             | 'tre' { $return = 3; }
70             | 'to' { $return = 2; }
71             | 'en' { $return = 1; }
72             | 'ett' { $return = 1; }
73              
74             tens: 'tjue' { $return = 20; } # try to find a word that representates
75             | 'tretti' { $return = 30; } # values 20,30,..,90
76             | 'førti' { $return = 40; }
77             | 'femti' { $return = 50; }
78             | 'seksti' { $return = 60; }
79             | 'sytti' { $return = 70; }
80             | 'åtti' { $return = 80; }
81             | 'nitti' { $return = 90; }
82              
83             decade: tens(?) number(?) # try to find words that represents values
84             { $return = 0; # from 0 to 99
85             for (@item) {
86             $return += $$_[0] if (ref $_ && defined $$_[0]);
87             }
88             }
89              
90             century: number(?) 'hundre' decade(?) # try to find words that represents values
91             { $return = 0; # from 100 to 999
92             for (@item) {
93             if (ref $_ && defined $$_[0]) {
94             $return += $$_[0];
95             } elsif ($_ eq "hundre") {
96             $return = ($return>0) ? $return * 100 : 100;
97             }
98             }
99             }
100              
101             millenium: century(?) decade(?) 'tusen' century(?) decade(?) # try to find words that represents values
102             { $return = 0; # from 1.000 to 999.999
103             for (@item) {
104             if (ref $_ && defined $$_[0]) {
105             $return += $$_[0];
106             } elsif ($_ eq "tusen") {
107             $return = ($return>0) ? $return * 1000 : 1000;
108             }
109             }
110             }
111              
112             million: millenium(?) century(?) decade(?) # try to find words that represents values
113             'millioner' # from 1.000.000 to 999.999.999
114             millenium(?) century(?) decade(?)
115             { $return = 0;
116             for (@item) {
117             if (ref $_ && defined $$_[0]) {
118             $return += $$_[0];
119             } elsif ($_ eq "millioner") {
120             $return = ($return>0) ? $return * 1000000 : 1000000;
121             }
122             }
123             }
124             });
125             }
126              
127             # }}}
128              
129             1;
130              
131             __END__