blib/lib/Lingua/KIR/Num2Word.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2  
3 package Lingua::KIR::Num2Word;
4 # ABSTRACT: Number to word conversion in Kyrgyz
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 # {{{ num2kir_cardinal convert number to text
25  
26 sub num2kir_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 = ('нөл', 'бир', 'эки', 'үч', 'төрт', 'беш', 'алты', 'жети', 'сегиз', 'тогуз');
36 my @tens = ('он', 'жыйырма', 'отуз', 'кырк', 'элүү', 'алтымыш', 'жетимиш', 'сексен', 'токсон');
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 = 'жүз';
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 ? 'жүз' : $ones[$hun_idx] . ' жүз';
58 $out .= $remain ? ' ' . num2kir_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 ? 'миң' : num2kir_cardinal($tho_idx) . ' миң';
65 $out .= $remain ? ' ' . num2kir_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 = num2kir_cardinal($mil_idx) . ' миллион';
72 $out .= $remain ? ' ' . num2kir_cardinal($remain) : '';
73 }
74  
75 return $out;
76 }
77  
78 # }}}
79  
80 # {{{ num2kir_ordinal convert number to ordinal text
81  
82 sub num2kir_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 = num2kir_cardinal($number);
92  
93 # Kyrgyz ordinals: cardinal + suffix determined by vowel harmony.
94 # Cyrillic vowels: а, е, и, о, ө, у, ү, ы
95 # After consonant: -(ы)нчы / -(и)нчи / -(у)нчу / -(ү)нчү
96 # After vowel: -нчы / -нчи / -нчу / -нчү
97  
98 my $last_vowel = q{};
99 if ($cardinal =~ m{([аеиоөуүы])[^аеиоөуүы]*\z}xms) {
100 $last_vowel = $1;
101 }
102  
103 my $ends_in_vowel = $cardinal =~ m{[аеиоөуүы]\z}xms;
104  
105 my %suffix_v = ( # after vowel
106 'а' => 'нчы', 'ы' => 'нчы',
107 'е' => 'нчи', 'и' => 'нчи',
108 'о' => 'нчу', 'у' => 'нчу',
109 'ө' => 'нчү', 'ү' => 'нчү',
110 );
111 my %suffix_c = ( # after consonant
112 'а' => 'ынчы', 'ы' => 'ынчы',
113 'е' => 'инчи', 'и' => 'инчи',
114 'о' => 'унчу', 'у' => 'унчу',
115 'ө' => 'үнчү', 'ү' => 'үнчү',
116 );
117  
118 my $suffix_table = $ends_in_vowel ? \%suffix_v : \%suffix_c;
119 my $suffix = $suffix_table->{$last_vowel} // 'инчи';
120  
121 return $cardinal . $suffix;
122 }
123  
124 # }}}
125  
126 # {{{ capabilities declare supported features
127  
128 sub capabilities {
129 return {
130 cardinal => 1,
131 ordinal => 1,
132 };
133 }
134  
135 # }}}
136 1;
137  
138 __END__
↑ Summary