| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||
| 2 | |||
| 3 | package Lingua::HUN::Num2Word; | ||
| 4 | # ABSTRACT: Number to word conversion in Hungarian | ||
| 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 | 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.2603260'; | ||
| 21 | |||
| 22 | # }}} | ||
| 23 | |||
| 24 | # {{{ num2hun_cardinal convert number to text | ||
| 25 | |||
| 26 | sub num2hun_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(nulla egy kettő három négy öt hat hét nyolc kilenc); | ||
| 36 | my @tens = qw(tíz húsz harminc negyven ötven hatvan hetven nyolcvan kilencven); | ||
| 37 | |||
| 38 | # 0 .. 9 | ||
| 39 | return $ones[$positive] if $positive < 10; | ||
| 40 | |||
| 41 | # 10 .. 19 | ||
| 42 | if ($positive >= 10 && $positive < 20) { | ||
| 43 | return 'tíz' if $positive == 10; | ||
| 44 | return 'tizen' . $ones[$positive - 10]; | ||
| 45 | } | ||
| 46 | |||
| 47 | # 20 .. 29 | ||
| 48 | if ($positive >= 20 && $positive < 30) { | ||
| 49 | return 'húsz' if $positive == 20; | ||
| 50 | return 'huszon' . $ones[$positive - 20]; | ||
| 51 | } | ||
| 52 | |||
| 53 | # 30 .. 99 | ||
| 54 | if ($positive >= 30 && $positive < 100) { | ||
| 55 | my $ten_idx = int($positive / 10); | ||
| 56 | my $remain = $positive % 10; | ||
| 57 | |||
| 58 | my $out = $tens[$ten_idx - 1]; | ||
| 59 | $out .= $ones[$remain] if $remain; | ||
| 60 | return $out; | ||
| 61 | } | ||
| 62 | |||
| 63 | my $out; | ||
| 64 | my $idx; | ||
| 65 | my $remain; | ||
| 66 | |||
| 67 | # 100 .. 999 | ||
| 68 | if ($positive >= 100 && $positive < 1000) { | ||
| 69 | $idx = int($positive / 100); | ||
| 70 | $remain = $positive % 100; | ||
| 71 | |||
| 72 | $out = $idx == 1 ? 'száz' : _compound_cardinal($idx) . 'száz'; | ||
| 73 | $out .= $remain ? num2hun_cardinal($remain) : ''; | ||
| 74 | } | ||
| 75 | # 1000 .. 999_999 | ||
| 76 | elsif ($positive >= 1000 && $positive < 1_000_000) { | ||
| 77 | $idx = int($positive / 1000); | ||
| 78 | $remain = $positive % 1000; | ||
| 79 | |||
| 80 | $out = $idx == 1 ? 'ezer' : _compound_cardinal($idx) . 'ezer'; | ||
| 81 | $out .= $remain ? num2hun_cardinal($remain) : ''; | ||
| 82 | } | ||
| 83 | # 1_000_000 .. 999_999_999 | ||
| 84 | elsif ($positive >= 1_000_000 && $positive < 1_000_000_000) { | ||
| 85 | $idx = int($positive / 1_000_000); | ||
| 86 | $remain = $positive % 1_000_000; | ||
| 87 | |||
| 88 | $out = _compound_cardinal($idx) . 'millió'; | ||
| 89 | $out .= $remain ? '-' . num2hun_cardinal($remain) : ''; | ||
| 90 | } | ||
| 91 | |||
| 92 | return $out; | ||
| 93 | } | ||
| 94 | |||
| 95 | # }}} | ||
| 96 | # {{{ _compound_cardinal cardinal form using két instead of kettő | ||
| 97 | |||
| 98 | sub _compound_cardinal { | ||
| 99 | my $positive = shift; | ||
| 100 | |||
| 101 | my $text = num2hun_cardinal($positive); | ||
| 102 | $text =~ s{kettő}{két}gxms; | ||
| 103 | |||
| 104 | return $text; | ||
| 105 | } | ||
| 106 | |||
| 107 | # }}} | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | __END__ |