| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||
| 2 | |||
| 3 | package Lingua::AZE::Num2Word; | ||
| 4 | # ABSTRACT: Number to word conversion in Azerbaijani | ||
| 5 | |||
| 6 | 1 | ▶ | use 5.16.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | # {{{ use block | ||
| 11 | |||
| 12 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use Export::Attrs; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use Readonly; | ||
| 15 | |||
| 16 | # }}} | ||
| 17 | # {{{ variable declarations | ||
| 18 | |||
| 19 | my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present'; | ||
| 20 | our $VERSION = '0.2603300'; | ||
| 21 | |||
| 22 | # }}} | ||
| 23 | |||
| 24 | # {{{ num2aze_cardinal convert number to text | ||
| 25 | |||
| 26 | sub num2aze_cardinal :Export { | ||
| 27 | my $positive = shift; | ||
| 28 | |||
| 29 | croak 'You should specify a number from interval [0, 999_999_999]' | ||
| 30 | if !defined $positive | ||
| 31 | || $positive !~ m{\A\d+\z}xms | ||
| 32 | || $positive < 0 | ||
| 33 | || $positive > 999_999_999; | ||
| 34 | |||
| 35 | my @ones = qw(sıfır bir iki üç dörd beş altı yeddi səkkiz doqquz); | ||
| 36 | my @tens = qw(on iyirmi otuz qırx əlli altmış yetmiş səksən doxsan); | ||
| 37 | |||
| 38 | return $ones[$positive] if ($positive >= 0 && $positive < 10); # 0 .. 9 | ||
| 39 | return $tens[$positive / 10 - 1] if ($positive >= 10 && $positive < 100 && $positive % 10 == 0); # 10,20,..,90 | ||
| 40 | |||
| 41 | my $out; | ||
| 42 | my $remain; | ||
| 43 | |||
| 44 | if ($positive > 9 && $positive < 100) { # 11 .. 99 | ||
| 45 | my $ten_idx = int($positive / 10); | ||
| 46 | $remain = $positive % 10; | ||
| 47 | |||
| 48 | $out = $tens[$ten_idx - 1] . ' ' . $ones[$remain]; | ||
| 49 | } | ||
| 50 | elsif ($positive == 100) { # 100 | ||
| 51 | $out = 'yüz'; | ||
| 52 | } | ||
| 53 | elsif ($positive > 100 && $positive < 1000) { # 101 .. 999 | ||
| 54 | my $hun_idx = int($positive / 100); | ||
| 55 | $remain = $positive % 100; | ||
| 56 | |||
| 57 | $out = $hun_idx == 1 ? 'yüz' : $ones[$hun_idx] . ' yüz'; | ||
| 58 | $out .= $remain ? ' ' . num2aze_cardinal($remain) : ''; | ||
| 59 | } | ||
| 60 | elsif ($positive >= 1000 && $positive < 1_000_000) { # 1000 .. 999_999 | ||
| 61 | my $tho_idx = int($positive / 1000); | ||
| 62 | $remain = $positive % 1000; | ||
| 63 | |||
| 64 | $out = $tho_idx == 1 ? 'min' : num2aze_cardinal($tho_idx) . ' min'; | ||
| 65 | $out .= $remain ? ' ' . num2aze_cardinal($remain) : ''; | ||
| 66 | } | ||
| 67 | elsif ($positive >= 1_000_000 && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999 | ||
| 68 | my $mil_idx = int($positive / 1_000_000); | ||
| 69 | $remain = $positive % 1_000_000; | ||
| 70 | |||
| 71 | $out = num2aze_cardinal($mil_idx) . ' milyon'; | ||
| 72 | $out .= $remain ? ' ' . num2aze_cardinal($remain) : ''; | ||
| 73 | } | ||
| 74 | |||
| 75 | return $out; | ||
| 76 | } | ||
| 77 | |||
| 78 | # }}} | ||
| 79 | |||
| 80 | # {{{ num2aze_ordinal convert number to ordinal text | ||
| 81 | |||
| 82 | sub num2aze_ordinal :Export { | ||
| 83 | my $number = shift; | ||
| 84 | |||
| 85 | croak 'You should specify a number from interval [1, 999_999_999]' | ||
| 86 | if !defined $number | ||
| 87 | || $number !~ m{\A\d+\z}xms | ||
| 88 | || $number < 1 | ||
| 89 | || $number > 999_999_999; | ||
| 90 | |||
| 91 | my $cardinal = num2aze_cardinal($number); | ||
| 92 | |||
| 93 | # Azerbaijani ordinals: cardinal + suffix determined by vowel harmony. | ||
| 94 | # Suffix has two forms depending on whether the cardinal ends in | ||
| 95 | # a vowel or consonant: | ||
| 96 | # ends in consonant: ıncı / inci / uncu / üncü | ||
| 97 | # ends in vowel: ncı / nci / ncu / ncü | ||
| 98 | |||
| 99 | my $last_vowel = q{}; | ||
| 100 | if ($cardinal =~ m{([aeıioöuü])[^aeıioöuü]*\z}xms) { | ||
| 101 | $last_vowel = $1; | ||
| 102 | } | ||
| 103 | |||
| 104 | my $ends_in_vowel = $cardinal =~ m{[aeıioöuü]\z}xms; | ||
| 105 | |||
| 106 | my %suffix_v = ( # after vowel | ||
| 107 | 'a' => 'ncı', 'ı' => 'ncı', | ||
| 108 | 'e' => 'nci', 'i' => 'nci', | ||
| 109 | 'o' => 'ncu', 'u' => 'ncu', | ||
| 110 | 'ö' => 'ncü', 'ü' => 'ncü', | ||
| 111 | ); | ||
| 112 | my %suffix_c = ( # after consonant | ||
| 113 | 'a' => 'ıncı', 'ı' => 'ıncı', | ||
| 114 | 'e' => 'inci', 'i' => 'inci', | ||
| 115 | 'o' => 'uncu', 'u' => 'uncu', | ||
| 116 | 'ö' => 'üncü', 'ü' => 'üncü', | ||
| 117 | ); | ||
| 118 | |||
| 119 | my $suffix_table = $ends_in_vowel ? \%suffix_v : \%suffix_c; | ||
| 120 | my $suffix = $suffix_table->{$last_vowel} // 'inci'; | ||
| 121 | |||
| 122 | return $cardinal . $suffix; | ||
| 123 | } | ||
| 124 | |||
| 125 | # }}} | ||
| 126 | |||
| 127 | # {{{ capabilities declare supported features | ||
| 128 | |||
| 129 | sub capabilities { | ||
| 130 | return { | ||
| 131 | cardinal => 1, | ||
| 132 | ordinal => 1, | ||
| 133 | }; | ||
| 134 | } | ||
| 135 | |||
| 136 | # }}} | ||
| 137 | 1; | ||
| 138 | |||
| 139 | __END__ | ||