File Coverage

blib/lib/Lingua/MON/Num2Word.pm
Criterion Covered Total %
statement 52 59 88.1
branch 24 32 75.0
condition 16 27 59.2
subroutine 8 9 88.8
pod 2 2 100.0
total 102 129 79.0


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2              
3             package Lingua::MON::Num2Word;
4             # ABSTRACT: Number to word conversion in Mongolian
5              
6 1     1   154193 use 5.16.0;
  1         4  
7 1     1   8 use utf8;
  1         2  
  1         17  
8 1     1   65 use warnings;
  1         3  
  1         69  
9              
10             # {{{ use block
11              
12 1     1   6 use Carp;
  1         25  
  1         102  
13 1     1   876 use Export::Attrs;
  1         14439  
  1         7  
14 1     1   1163 use Readonly;
  1         6284  
  1         786  
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             # {{{ num2mon_cardinal convert number to text
25              
26             sub num2mon_cardinal :Export {
27 17     17 1 263973 my $positive = shift;
28              
29 17 100 66     269 croak 'You should specify a number from interval [0, 999_999_999]'
      66        
      100        
30             if !defined $positive
31             || $positive !~ m{\A\d+\z}xms
32             || $positive < 0
33             || $positive > 999_999_999;
34              
35             # cardinal forms (standalone)
36 15         74 my @units = qw(тэг нэг хоёр гурав дөрөв тав зургаа долоо найм ес);
37              
38             # combining forms (used before зуу, мянга, or as tens prefix)
39 15         51 my @units_combining = qw(тэг нэг хоёр гурван дөрвөн таван зургаан долоон найман есөн);
40              
41             # tens standalone
42 15         50 my @tens = ('', '', 'хорь', 'гуч', 'дөч', 'тави', 'жар', 'дал', 'ная', 'ер');
43              
44             # tens combining (before units)
45 15         45 my @tens_combining = ('', 'арван', 'хорин', 'гучин', 'дөчин', 'тавин', 'жаран', 'далан', 'наян', 'ерэн');
46              
47 15 100 66     87 return $units[$positive] if ($positive >= 0 && $positive < 10);
48 11 100       33 return 'арав' if ($positive == 10);
49              
50 10         32 my $out;
51             my $idx;
52 10         0 my $remain;
53              
54 10 100 66     102 if ($positive > 10 && $positive < 20) { # 11 .. 19
    100 66        
    100 66        
    100 33        
    50 0        
    0          
55 2         7 $remain = $positive % 10;
56 2         7 $out = "арван $units[$remain]";
57             }
58             elsif ($positive > 19 && $positive < 100) { # 20 .. 99
59 3         11 $idx = int ($positive / 10);
60 3         7 $remain = $positive % 10;
61              
62 3 100       18 if ($remain) {
63 2         8 $out = "$tens_combining[$idx] $units[$remain]";
64             }
65             else {
66 1         4 $out = $tens[$idx];
67             }
68             }
69             elsif ($positive == 100) { # 100
70 1         4 $out = 'зуу';
71             }
72             elsif ($positive > 100 && $positive < 1000) { # 101 .. 999
73 2         7 $idx = int ($positive / 100);
74 2         5 $remain = $positive % 100;
75              
76 2 100       7 if ($idx == 1) {
77 1         3 $out = 'зуун';
78             }
79             else {
80 1         5 $out = "$units_combining[$idx] зуун";
81             }
82 2 100       17 $out .= $remain ? ' ' . num2mon_cardinal($remain) : '';
83              
84             # standalone зуу if no remainder and idx == 1 already handled
85             }
86             elsif ($positive > 999 && $positive < 1_000_000) { # 1000 .. 999_999
87 2         8 $idx = int ($positive / 1000);
88 2         6 $remain = $positive % 1000;
89              
90 2 100       6 if ($idx == 1) {
91 1         3 $out = 'мянга';
92             }
93             else {
94 1         5 $out = num2mon_cardinal($idx) . ' мянга';
95             }
96 2 50       7 $out .= $remain ? ' ' . num2mon_cardinal($remain) : '';
97             }
98             elsif ( $positive > 999_999
99             && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999
100 0         0 $idx = int ($positive / 1000000);
101 0         0 $remain = $positive % 1000000;
102              
103 0 0       0 if ($idx == 1) {
104 0         0 $out = 'нэг сая';
105             }
106             else {
107 0         0 $out = num2mon_cardinal($idx) . ' сая';
108             }
109 0 0       0 $out .= $remain ? ' ' . num2mon_cardinal($remain) : '';
110             }
111              
112 10         61 return $out;
113 1     1   10 }
  1         3  
  1         10  
114              
115             # }}}
116              
117             # {{{ capabilities declare supported features
118              
119             sub capabilities {
120             return {
121 0     0 1   cardinal => 1,
122             };
123             }
124              
125             # }}}
126             1;
127              
128             __END__