| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*- | ||
| 2 | |||
| 3 | package Lingua::ISL::Num2Word; | ||
| 4 | # ABSTRACT: Number to word conversion in Icelandic | ||
| 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 | # {{{ num2isl_cardinal convert number to text | ||
| 25 | |||
| 26 | sub num2isl_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 | # 0 .. 19: unique words | ||
| 36 | my @tokens1 = qw(núll einn tveir þrír fjórir fimm sex sjö átta níu | ||
| 37 | tíu ellefu tólf); | ||
| 38 | my @teens = qw(þrettán fjórtán fimmtán sextán sautján átján nítján); | ||
| 39 | |||
| 40 | # tens | ||
| 41 | my @tokens2 = qw(tuttugu þrjátíu fjörutíu fimmtíu sextíu sjötíu áttatíu níutíu); | ||
| 42 | |||
| 43 | # neuter forms for use with hundrað/þúsund (both neuter nouns) | ||
| 44 | my @neuter = qw(núll eitt tvö þrjú fjögur fimm sex sjö átta níu); | ||
| 45 | |||
| 46 | return $tokens1[$positive] if ($positive >= 0 && $positive < 13); # 0 .. 12 | ||
| 47 | return $teens[$positive - 13] if ($positive > 12 && $positive < 20); # 13 .. 19 | ||
| 48 | |||
| 49 | my $out; # string for return value construction | ||
| 50 | my $one_idx; # index for tokens arrays | ||
| 51 | my $remain; # remainder | ||
| 52 | |||
| 53 | if ($positive > 19 && $positive < 100) { # 20 .. 99 | ||
| 54 | $one_idx = int ($positive / 10); | ||
| 55 | $remain = $positive % 10; | ||
| 56 | |||
| 57 | $out = $tokens2[$one_idx - 2]; | ||
| 58 | $out .= " og $tokens1[$remain]" if ($remain); | ||
| 59 | } | ||
| 60 | elsif ($positive > 99 && $positive < 1000) { # 100 .. 999 | ||
| 61 | $one_idx = int ($positive / 100); | ||
| 62 | $remain = $positive % 100; | ||
| 63 | |||
| 64 | # hundrað is neuter: eitt hundrað, tvö hundruð, ... | ||
| 65 | if ($one_idx == 1) { | ||
| 66 | $out = 'hundrað'; | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | $out = "$neuter[$one_idx] hundruð"; | ||
| 70 | } | ||
| 71 | $out .= $remain ? ' og ' . num2isl_cardinal($remain) : ''; | ||
| 72 | } | ||
| 73 | elsif ($positive > 999 && $positive < 1_000_000) { # 1000 .. 999_999 | ||
| 74 | $one_idx = int ($positive / 1000); | ||
| 75 | $remain = $positive % 1000; | ||
| 76 | |||
| 77 | # þúsund is neuter and invariable | ||
| 78 | if ($one_idx == 1) { | ||
| 79 | $out = 'þúsund'; | ||
| 80 | } | ||
| 81 | elsif ($one_idx < 5) { | ||
| 82 | $out = num2isl_neuter($one_idx) . ' þúsund'; | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | $out = num2isl_cardinal($one_idx) . ' þúsund'; | ||
| 86 | } | ||
| 87 | $out .= $remain ? ' og ' . num2isl_cardinal($remain) : ''; | ||
| 88 | } | ||
| 89 | elsif ( $positive > 999_999 | ||
| 90 | && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999 | ||
| 91 | $one_idx = int ($positive / 1000000); | ||
| 92 | $remain = $positive % 1000000; | ||
| 93 | |||
| 94 | if ($one_idx == 1) { | ||
| 95 | $out = 'ein milljón'; | ||
| 96 | } | ||
| 97 | else { | ||
| 98 | $out = num2isl_cardinal($one_idx) . ' milljónir'; | ||
| 99 | } | ||
| 100 | $out .= $remain ? ' og ' . num2isl_cardinal($remain) : ''; | ||
| 101 | } | ||
| 102 | |||
| 103 | return $out; | ||
| 104 | } | ||
| 105 | |||
| 106 | # }}} | ||
| 107 | # {{{ num2isl_neuter neuter form for small numbers | ||
| 108 | |||
| 109 | sub num2isl_neuter { | ||
| 110 | my $n = shift; | ||
| 111 | my @neuter = qw(núll eitt tvö þrjú fjögur); | ||
| 112 | return $neuter[$n] if ($n >= 0 && $n <= 4); | ||
| 113 | return num2isl_cardinal($n); | ||
| 114 | } | ||
| 115 | |||
| 116 | # }}} | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | __END__ |