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