# For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8; -*-

package Lingua::ZHO::Word2Num;
# ABSTRACT: Word 2 number conversion in ZHO.

# {{{ use block

use 5.10.1;

use strict;
use warnings;

use Export::Attrs;
use Parse::RecDescent;

# }}}
# {{{ variable declarations
our $VERSION = '0.2603230';
my  $parser  = zho_numerals();

# }}}

# {{{ w2n                                         convert number to text

sub w2n :Export {
    my $input = shift // return;

    return 0 if ($input =~ m{\b(nul|ling)\b}xmsi);

    $input .= " "; # Grant space at the end

    return $parser->numeral($input) || undef;
}

# }}}
# {{{ zho_numerals                                create parser for numerals

sub zho_numerals {
    return Parse::RecDescent->new(q{
      numeral: <rulevar: local $number = 0>
      numeral: million    { return $item[1]; }                        # root parse. go from maximum to minimum value
        |      millenium2 { return $item[1]; }
        |      millenium1 { return $item[1]; }
        |      century    { return $item[1]; }
        |      decade     { return $item[1]; }
        |                 { return undef; }

      number: 'nul ' { $return = 0; }                                 # try to find a word from 0 to 10
        |     'Yi '  { $return = 1; }
        |     'Er '  { $return = 2; }
        |     'San ' { $return = 3; }
        |     'Si '  { $return = 4; }
        |     'Wu '  { $return = 5; }
        |     'Liu ' { $return = 6; }
        |     'Qi '  { $return = 7; }
        |     'Ba '  { $return = 8; }
        |     'Jiu ' { $return = 9; }
        |     'Shi ' { $return = 10; }

      tens: 'YiShi '  { $return = 10; }                               # try to find a word that representates
        |   'ErShi '  { $return = 20; }                               # values 20,30,..,90
        |   'SanShi ' { $return = 30; }
        |   'SiShi '  { $return = 40; }
        |   'WuShi '  { $return = 50; }
        |   'LiuShi ' { $return = 60; }
        |   'QiShi '  { $return = 70; }
        |   'BaShi '  { $return = 80; }
        |   'JiuShi ' { $return = 90; }

      hundreds: 'YiBai '  { $return = 100; }                          # try to find a word that representates
        |       'ErBai '  { $return = 200; }                          # values 100,200,..,900
        |       'SanBai ' { $return = 300; }
        |       'SiBai '  { $return = 400; }
        |       'WuBai '  { $return = 500; }
        |       'LiuBai ' { $return = 600; }
        |       'QiBai '  { $return = 700; }
        |       'BaBai '  { $return = 800; }
        |       'JiuBai ' { $return = 900; }

      thousands: 'YiQian '  { $return = 1000; }                       # try to find a word that representates
        |        'ErQian '  { $return = 2000; }                       # values 1000,2000,..,9000
        |        'SanQian ' { $return = 3000; }
        |        'SiQian '  { $return = 4000; }
        |        'WuQian '  { $return = 5000; }
        |        'LiuQian ' { $return = 6000; }
        |        'QiQian '  { $return = 7000; }
        |        'BaQian '  { $return = 8000; }
        |        'JiuQian ' { $return = 9000; }

      tenthousands: 'YiWan '  { $return = 10000; }                    # try to find a word that representates
        |           'ErWan '  { $return = 20000; }                    # values 10000,20000,..,90000
        |           'SanWan ' { $return = 30000; }
        |           'SiWan '  { $return = 40000; }
        |           'WuWan '  { $return = 50000; }
        |           'LiuWan ' { $return = 60000; }
        |           'QiWan '  { $return = 70000; }
        |           'BaWan '  { $return = 80000; }
        |           'JiuWan ' { $return = 90000; }

      decade: tens(?) number(?) number(?)                             # try to find words that represents values
              { $return = 0;                                          # from 0 to 20
                for (@item) {
                  $return += $$_[0] if (ref $_ && defined $$_[0]);
                }
              }

      century:  hundreds(?) decade(?)                                 # try to find words that represents values
               { $return = 0;                                         # from 100 to 999
                 for (@item) {
                  $return += $$_[0] if (ref $_ && defined $$_[0]);
                 }
               }

    millenium1: thousands(1) century(?)                               # try to find words that represents values
               { $return = 0;                                         # from 1.000 to 999.999
                 for (@item) {
                   if (ref $_ && defined $$_[0]) {
                     $return += $$_[0] if (ref $_ && defined $$_[0]);
                   }
                 }
               }

    millenium2: tenthousands(1) thousands(?)  century(?) decade(?)    # try to find words that represents values
               { $return = 0;                                         # from 1.000 to 999.999
                 for (@item) {
                   if (ref $_ && defined $$_[0]) {
                     $return += $$_[0] if (ref $_ && defined $$_[0]);
                   }
                 }
               }

      million: millenium2(?) millenium1(?) century(?) decade(?)       # try to find words that represents values
               ' Wan '                                                # from 1.000.000 to 999.999.999.999
               millenium2(?) millenium1(?) century(?) decade(?)
               { $return = 0;
                 for (@item) {
                   if (ref $_ && defined $$_[0]) {
                     $return += $$_[0];
                   } elsif ($_ eq "Wan") {
                     $return = ($return>0) ? $return * 100000 : 100000;
                   }
                 }
               }
    });
}

# }}}

1;

__END__

# {{{ POD HEAD

=pod

=head1 NAME

=head2 Lingua::ZHO::Word2Num 

=head1 VERSION

version 0.2603230

Word 2 number conversion in ZHO.

Lingua::ZHO::Word2Num is module for converting text containing number
representation in Chinese back into number. Converts whole numbers
from 0 up to 999 999 999 999.

Input text must be encoded in UTF-8.

=cut

# }}}
# {{{ SYNOPSIS

=pod

=head1 SYNOPSIS

 use Lingua::ZHO::Word2Num;

 my $num = Lingua::ZHO::Word2Num::w2n( 'SiShi Er' );

 print defined($num) ? $num : "sorry, can't convert this text into number.";

=cut

# }}}
# {{{ Functions Reference

=pod

=head1 Functions Reference

=over 2

=item B<w2n> (positional)

  1   str  string to convert
  =>  num  converted number

Convert text representation to number.


=item B<zho_numerals> (void)

  =>  obj  new parser object

Internal parser.


=back

=cut

# }}}
# {{{ EXPORTED FUNCTIONS

=pod

=head1 EXPORT_OK

=over 2

=item w2n

=back

=cut

# }}}
# {{{ POD FOOTER

=pod

=head1 AUTHOR

 coding, maintenance, refactoring, extensions, specifications:

   Vitor Serra Mori <info@petamem.com>

=head1 COPYRIGHT

Copyright (c) PetaMem, s.r.o. 2003-present

=cut

# }}}
