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

package Lingua::EUS::Word2Num;
# ABSTRACT: Word to number conversion in Basque

use 5.16.0;
use utf8;
use warnings;

# {{{ use block

use Export::Attrs;

use Parse::RecDescent;
# }}}
# {{{ variable declarations
our $VERSION = '0.2603300';

my $parser = eu_numerals();

# }}}

# {{{ w2n                                         convert text to number
#
sub w2n :Export {
    my $input = shift // return;

    $input =~ s/eta / /g;             # Remove word that represents tone in speaking, but nothing for the language
    $input =~ s/ta / /g;              # *the same
    $input =~ s/milioi bat/milioi/g;  # *the same
    $input =~ s/,//g;                 # Remove trick chars

    return 0 if $input eq 'zero';

    return $parser->numeral($input) || undef;
}
# }}}
# {{{ eu_numerals                                 create parser for numerals
#
sub eu_numerals {
    return Parse::RecDescent->new(q{
      numeral: <rulevar: local $number = 0>
      numeral: millions  { return $item[1]; }                        # root parse. go from maximum to minimum value
        |      million   { return $item[1]; }
        |      millenium { return $item[1]; }
        |      century   { return $item[1]; }
        |      decade    { return $item[1]; }
        |                { return undef; }

      number: 'zero'          { $return = 0; }                       # try to find a word from 0 to 19
        |     'bat'           { $return = 1; }
        |     'bi'            { $return = 2; }
        |     'hiru'          { $return = 3; }
        |     'lau'           { $return = 4; }
        |     'bost'          { $return = 5; }
        |     'sei'           { $return = 6; }
        |     'zazpi'         { $return = 7; }
        |     'zortzi'        { $return = 8; }
        |     'bederatzi'     { $return = 9; }
        |     'hamar'         { $return = 10; }
        |     'hamaika'       { $return = 11; }
        |     'hamabi'        { $return = 12; }
        |     'hamahiru'      { $return = 13; }
        |     'hamalau'       { $return = 14; }
        |     'hamabost'      { $return = 15; }
        |     'hamasei'       { $return = 16; }
        |     'hamazazpi'     { $return = 17; }
        |     'hemezortzi'    { $return = 18; }
        |     'hemeretzi'     { $return = 19; }

       base20: 'hogei'         { $return = 20; }                     # Base20: 20,40,60 and 80. All
        |      'berrogei'      { $return = 40; }                     # other numbers are an composition
        |      'hirurogei'     { $return = 60; }                     # of number and base 20.
        |      'laurogei'      { $return = 80; }

     centuries: 'ehun'          { $return = 100; }                   # try to find a word that represents
        |       'berrehun'      { $return = 200; }                   # values 100,200,300,...900
        |       'hirurehun'     { $return = 300; }
        |       'laurehun'      { $return = 400; }
        |       'bostehun'      { $return = 500; }
        |       'seiehun'       { $return = 600; }
        |       'zazpiehun'     { $return = 700; }
        |       'zortziehun'    { $return = 800; }
        |       'bederatziehun' { $return = 900; }

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

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

    millenium: century(?) decade(?) 'mila' 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];
                   } elsif ($_ eq "mila") {
                     $return = ($return>0) ? $return * 1000 : 1000;
                   }
                 }
               }

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

      millions: century(?) decade(?)                                  # try to find words that represents values
               'mila milioi'                                          # from 1.000.000.000 to 999.999.999.999
               million(?) millenium(?) century(?) decade(?)
               { $return = 0;
                 for (@item) {
                   if (ref $_ && defined $$_[0]) {
                     $return += $$_[0];
                   } elsif ($_ eq "mila milioi") {
                     $return = ($return>0) ? $return * 1000000000 : 1000000000;
                   }
                 }
               }

    });
}
# }}}

1;

__END__

# {{{ POD HEAD

=pod

=encoding utf-8

=head1 NAME

Lingua::EUS::Word2Num - Word to number conversion in Basque


=head1 VERSION

version 0.2603300

Lingua::EUS::Word2Num is module for converting text containing number
representation in Basque (Euskara) 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::EUS::Word2Num;

 my $num = Lingua::EUS::Word2Num::w2n( 'ehun eta hogeita hiru' );

 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
      undef  if the input string is not known

Convert text representation to number.

=item B<eu_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 AUTHORS

 specification, maintenance:
   Richard C. Jelinek E<lt>rj@petamem.comE<gt>
 maintenance, coding (2025-present):
   PetaMem AI Coding Agents

=head1 COPYRIGHT

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

=head1 LICENSE

This module is free software; you can redistribute it and/or modify it
under the same terms as the Artistic License 2.0 or the BSD 2-Clause
License. See the LICENSE file in the distribution for details.

=cut

# }}}
