File Coverage

blib/lib/Lingua/JPN/Word2Num.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition 2 2 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 39 39 100.0


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; eval: (folding-mode 1); -*-
2              
3             package Lingua::JPN::Word2Num;
4             # ABSTRACT: Word 2 number conversion in JPN.
5              
6             # {{{ use block
7              
8 1     1   94709 use 5.10.1;
  1         3  
9              
10 1     1   5 use strict;
  1         2  
  1         24  
11 1     1   3 use warnings;
  1         11  
  1         48  
12              
13 1     1   586 use Export::Attrs;
  1         9494  
  1         5  
14 1     1   1151 use Parse::RecDescent;
  1         40154  
  1         11  
15              
16             # }}}
17             # {{{ variable declarations
18             our $VERSION = '0.2603230';
19             my $parser = ja_numerals();
20              
21             # }}}
22              
23             # {{{ w2n convert number to text
24             #
25             sub w2n :Export {
26 4   100 4 1 422813 my $input = shift // return;
27              
28 3         12 $input =~ s/san-byaku/san hyaku/g; # Spoken language exceptions, that are being corrected
29 3         10 $input =~ s/ro-p-pyaku/roku hyaku/g; # to use one unique logic
30 3         11 $input =~ s/ha-p-pyaku/hachi hyaku/g;
31 3         11 $input =~ s/san-zen/san sen/g;
32 3         10 $input =~ s/ha-s-sen/hachi sen/g;
33 3         9 $input =~ s/hyaku-man/hyman/g;
34              
35 3         18 $input =~ s/-/ /g; # make space an standard for everything
36              
37 3         49 return $parser->numeral($input);
38 1     1   298 }
  1         7  
  1         10  
39             # }}}
40             # {{{ ja_numerals create parser for numerals
41             #
42             sub ja_numerals {
43 1     1 1 10 return Parse::RecDescent->new(q{
44             numeral:
45             numeral: tenmillion { return $item[1]; }
46             | million { return $item[1]; }
47             | tenmillenium { return $item[1]; }
48             | millenium { return $item[1]; }
49             | century { return $item[1]; }
50             | decade { return $item[1]; }
51             | { return undef; }
52              
53             number: 'ichi' { $return = 1; } # try to find a word from 1 to 9
54             | 'ni' { $return = 2; }
55             | 'san' { $return = 3; }
56             | 'yon' { $return = 4; }
57             | 'go' { $return = 5; }
58             | 'roku' { $return = 6; }
59             | 'nana' { $return = 7; }
60             | 'hachi' { $return = 8; }
61             | 'kyu' { $return = 9; }
62             | 'ju' { $return = 10; }
63              
64             decade: number(?) number(?) number(?) # try to find words that represents values
65             { my @s; # from 0 to 99
66             for (@item) {
67             if (ref $_ && defined $$_[0]) {
68             push(@s,$$_[0]);
69             }
70             }
71              
72             $return = shift @s;
73             $return = $return * $s[0] + $s[1] if (scalar(@s) == 2);
74             $return = $return * $s[0] if (scalar(@s) == 1 && $s[0] == 10); # The order of the 10 multiplier
75             $return = $return + $s[0] if (scalar(@s) == 1 && $s[0] != 10); # defines sum or multiply
76             }
77              
78             century: number(?) 'hyaku' decade(?) # try to find words that represents values
79             { $return = 0; # from 100 to 999
80             for (@item) {
81             if (ref $_ && defined $$_[0]) {
82             $return += $$_[0];
83             } elsif ($_ eq "hyaku") {
84             $return = ($return>0) ? $return * 100 : 100;
85             }
86             }
87             }
88              
89             millenium: century(?) decade(?) 'sen' century(?) decade(?) # try to find words that represents values
90             { $return = 0; # from 1.000 to 9.999
91             for (@item) {
92             if (ref $_ && defined $$_[0]) {
93             $return += $$_[0];
94             } elsif ($_ eq "sen") {
95             $return = ($return>0) ? $return * 1000 : 1000;
96             }
97             }
98             }
99              
100             tenmillenium: millenium(?) century(?) decade(?) # try to find words that represents values
101             'man' # from 10.000 to 999.999
102             millenium(?) century(?) decade(?)
103             { $return = 0;
104             for (@item) {
105             if (ref $_ && defined $$_[0]) {
106             $return += $$_[0];
107             } elsif ($_ eq "man") {
108             $return = ($return>0) ? $return * 10000 : 10000;
109             }
110             }
111             }
112              
113             million: tenmillenium(?) millenium(?) century(?) decade(?) # try to find words that represents values
114             'hyman' # from 1.000.000 to 999.999.999
115             tenmillenium(?) millenium(?) century(?) decade(?)
116             { $return = 0;
117             for (@item) {
118             if (ref $_ && defined $$_[0]) {
119             $return += $$_[0];
120             } elsif ($_ eq "hyman") {
121             $return = ($return>0) ? $return * 1000000 : 1000000;
122             }
123             }
124             }
125              
126             tenmillion: million(?) tenmillenium(?) millenium(?) # try to find words that represents values
127             century(?) decade(?) # from 100.000.000 to 999.999.999.999
128             'oku'
129             million(?) tenmillenium(?) millenium(?)
130             century(?) decade(?)
131             { $return = 0;
132             for (@item) {
133             if (ref $_ && defined $$_[0]) {
134             $return += $$_[0];
135             } elsif ($_ eq "oku") {
136             $return = ($return>0) ? $return * 100000000 : 100000000;
137             }
138             }
139             }
140             });
141             }
142             # }}}
143              
144             1;
145              
146             __END__