| 1 | # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8; -*- | ||
| 2 | |||
| 3 | package Lingua::LIT::Num2Word; | ||
| 4 | # ABSTRACT: Number to word conversion in Lithuanian | ||
| 5 | |||
| 6 | use 5.16.0; | ||
| 7 | use utf8; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | # {{{ use block | ||
| 11 | |||
| 12 | use Carp; | ||
| 13 | use Export::Attrs; | ||
| 14 | |||
| 15 | # }}} | ||
| 16 | # {{{ var block | ||
| 17 | our $VERSION = '0.2603260'; | ||
| 18 | my %token1 = qw( 0 nulis 1 vienas 2 du | ||
| 19 | 3 trys 4 keturi 5 penki | ||
| 20 | 6 šeši 7 septyni 8 aštuoni | ||
| 21 | 9 devyni 10 dešimt 11 vienuolika | ||
| 22 | 12 dvylika 13 trylika 14 keturiolika | ||
| 23 | 15 penkiolika 16 šešiolika 17 septyniolika | ||
| 24 | 18 aštuoniolika 19 devyniolika | ||
| 25 | ); | ||
| 26 | my %token2 = qw( 20 dvidešimt 30 trisdešimt | ||
| 27 | 40 keturiasdešimt 50 penkiasdešimt | ||
| 28 | 60 šešiasdešimt 70 septyniasdešimt | ||
| 29 | 80 aštuoniasdešimt 90 devyniasdešimt | ||
| 30 | ); | ||
| 31 | |||
| 32 | # }}} | ||
| 33 | |||
| 34 | # {{{ _decline choose singular/plural/genitive form | ||
| 35 | |||
| 36 | sub _decline { | ||
| 37 | my ($count, $singular, $plural, $genitive) = @_; | ||
| 38 | |||
| 39 | my $last_two = $count % 100; | ||
| 40 | my $last_one = $count % 10; | ||
| 41 | |||
| 42 | # teens (11-19) always take genitive plural | ||
| 43 | if ($last_two >= 10 && $last_two <= 19) { | ||
| 44 | return $genitive; | ||
| 45 | } | ||
| 46 | |||
| 47 | # last digit 1 => singular | ||
| 48 | if ($last_one == 1) { | ||
| 49 | return $singular; | ||
| 50 | } | ||
| 51 | |||
| 52 | # last digit 0 => genitive plural | ||
| 53 | if ($last_one == 0) { | ||
| 54 | return $genitive; | ||
| 55 | } | ||
| 56 | |||
| 57 | # last digit 2-9 => plural nominative | ||
| 58 | return $plural; | ||
| 59 | } | ||
| 60 | |||
| 61 | # }}} | ||
| 62 | # {{{ _hundreds convert hundreds part | ||
| 63 | |||
| 64 | sub _hundreds { | ||
| 65 | my ($number) = @_; | ||
| 66 | |||
| 67 | my $h = int($number / 100); | ||
| 68 | |||
| 69 | if ($h == 1) { | ||
| 70 | return 'vienas šimtas'; | ||
| 71 | } | ||
| 72 | |||
| 73 | return $token1{$h} . ' ' . _decline($h, 'šimtas', 'šimtai', 'šimtų'); | ||
| 74 | } | ||
| 75 | |||
| 76 | # }}} | ||
| 77 | # {{{ num2lit_cardinal number to string conversion | ||
| 78 | |||
| 79 | sub num2lit_cardinal :Export { | ||
| 80 | my $result = ''; | ||
| 81 | my $number = shift; | ||
| 82 | |||
| 83 | croak 'You should specify a number from interval [0, 999_999_999]' | ||
| 84 | if !defined $number | ||
| 85 | || $number !~ m{\A\d+\z}xms | ||
| 86 | || $number < 0 | ||
| 87 | || $number > 999_999_999; | ||
| 88 | |||
| 89 | # 0-19 | ||
| 90 | if ($number < 20) { | ||
| 91 | return $token1{$number}; | ||
| 92 | } | ||
| 93 | |||
| 94 | # 20-99 | ||
| 95 | if ($number < 100) { | ||
| 96 | my $remainder = $number % 10; | ||
| 97 | $result = $token2{$number - $remainder}; | ||
| 98 | if ($remainder != 0) { | ||
| 99 | $result .= ' ' . $token1{$remainder}; | ||
| 100 | } | ||
| 101 | return $result; | ||
| 102 | } | ||
| 103 | |||
| 104 | # 100-999 | ||
| 105 | if ($number < 1_000) { | ||
| 106 | my $remainder = $number % 100; | ||
| 107 | $result = _hundreds($number); | ||
| 108 | if ($remainder != 0) { | ||
| 109 | $result .= ' ' . num2lit_cardinal($remainder); | ||
| 110 | } | ||
| 111 | return $result; | ||
| 112 | } | ||
| 113 | |||
| 114 | # 1_000-999_999 | ||
| 115 | if ($number < 1_000_000) { | ||
| 116 | my $remainder = $number % 1_000; | ||
| 117 | my $thousands = int($number / 1_000); | ||
| 118 | my $suffix = _decline($thousands, 'tūkstantis', 'tūkstančiai', 'tūkstančių'); | ||
| 119 | |||
| 120 | if ($thousands == 1) { | ||
| 121 | $result = 'vienas tūkstantis'; | ||
| 122 | } | ||
| 123 | elsif ($thousands < 20) { | ||
| 124 | $result = $token1{$thousands} . ' ' . $suffix; | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | $result = num2lit_cardinal($thousands) . ' ' . $suffix; | ||
| 128 | } | ||
| 129 | |||
| 130 | if ($remainder != 0) { | ||
| 131 | $result .= ' ' . num2lit_cardinal($remainder); | ||
| 132 | } | ||
| 133 | return $result; | ||
| 134 | } | ||
| 135 | |||
| 136 | # 1_000_000-999_999_999 | ||
| 137 | if ($number < 1_000_000_000) { | ||
| 138 | my $remainder = $number % 1_000_000; | ||
| 139 | my $millions = int($number / 1_000_000); | ||
| 140 | my $suffix = _decline($millions, 'milijonas', 'milijonai', 'milijonų'); | ||
| 141 | |||
| 142 | if ($millions == 1) { | ||
| 143 | $result = 'vienas milijonas'; | ||
| 144 | } | ||
| 145 | elsif ($millions < 20) { | ||
| 146 | $result = $token1{$millions} . ' ' . $suffix; | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | $result = num2lit_cardinal($millions) . ' ' . $suffix; | ||
| 150 | } | ||
| 151 | |||
| 152 | if ($remainder != 0) { | ||
| 153 | $result .= ' ' . num2lit_cardinal($remainder); | ||
| 154 | } | ||
| 155 | return $result; | ||
| 156 | } | ||
| 157 | |||
| 158 | return $result; | ||
| 159 | } | ||
| 160 | |||
| 161 | # }}} | ||
| 162 | |||
| 163 | 1; | ||
| 164 | |||
| 165 | __END__ |