blib/lib/Lingua/Word2Num.pm

filter: Statementstmt 64.3% 47 / 73 Branchbran 20.8% 5 / 24 Conditioncond 13.0% 3 / 23 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 91.6% 11 / 12 Podpod 100% 4 / 4 totaltotal 47.6% 71 / 149 CC 14 SCAR 36.5
CC
14
Cov
46%
cardinal
83.7
preprocess_code
8.7
known_langs
2.0
CRAP 38.7
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1) -*-
2  
3 package Lingua::Word2Num;
4 # ABSTRACT: Word to number conversion
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 1 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # }}}
17 # {{{ var block
18  
19 1 my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2004-present';
20 1 our $VERSION = '0.2603260';
21  
22 # templates for functional and object interface
23  
24 1 my $template_func = 'use __PACKAGE_WITH_VERSION__ ();'."\n".
25 '$result = __PACKAGE__::__FUNCTION__($word);'."\n";
26  
27 1 my $template_obj = 'use __PACKAGE_WITH_VERSION__ ();'."\n".
28 'my $tmp_obj = new __PACKAGE__;'."\n".
29 '$result = $tmp_obj->__FUNCTION__("$word");'."\n";
30  
31 # {{{ ISO 639-1 to 639-3 mapping
32 1 my %iso1_to_3 = (
33 af => 'afr', ar => 'ara', bg => 'bul', ca => 'cat',
34 cs => 'ces', da => 'dan', de => 'deu', el => 'ell',
35 en => 'eng', es => 'spa', et => 'est', eu => 'eus',
36 fa => 'fas', fi => 'fin', fr => 'fra', he => 'heb',
37 hi => 'hin', hr => 'hrv', hu => 'hun', id => 'ind',
38 is => 'isl', it => 'ita', ja => 'jpn', ko => 'kor',
39 lt => 'lit', lv => 'lav', nl => 'nld', 'no' => 'nor',
40 pl => 'pol', pt => 'por', ro => 'ron', ru => 'rus',
41 sk => 'slk', sv => 'swe', sw => 'swa', th => 'tha',
42 tr => 'tur',
43 uk => 'ukr', vi => 'vie', zh => 'zho',
44 );
45 # }}}
46  
47 # {{{ %known — auto-discovered from lib/Lingua/*/Word2Num.pm
48  
49 # Override table for legacy modules with non-standard API
50 1 my %w2n_override = (
51 ind => { package => 'Words2Nums', function => 'words2nums' },
52 por => { package => 'Words2Nums', function => 'word2num' },
53 );
54  
55 1 our %known;
56  
57 # auto-discover: scan for Lingua::*::Word2Num modules
58 {
59 1 my $lingua_dir;
60 1 for my $inc (@INC) {
61 2 my $try = "$inc/Lingua";
62 2 ▶ if (-d $try) { $lingua_dir = $try; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $try) 1 1 2
63 }
64  
65 1 ▶ if ($lingua_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lingua_dir) 1 0 1
66 1 for my $dir (glob "$lingua_dir/*/") {
67 0 my ($lang) = $dir =~ m{/([A-Z]{3})/\z};
68 0 ▶ next unless $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless $lang 0 0 0
69 0 $lang = lc $lang;
70  
71 0 ▶ if (-e "$dir/Word2Num.pm" || exists $w2n_override{$lang}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$dir/Word2Num.pm" or exists $w2n_override{$lang}) 0 0 0
Condition0%0/3
-e "$dir/Word2Num.pm" or exists $w2n_override{$lang}
l!l&&r!l&&!r
000
MC/DC0%0/2
-e "$dir/Word2Num.pm" or exists $w2n_override{$lang}
-e "$dir/Word2Num.pm" exists $w2n_override{$lang}
Truth table0%0/3
A or B
A= -e "$dir/Word2Num.pm"B= exists $w2n_override{$lang}
ABresult
1X1
011
000
72 0 ▶ my $ov = $w2n_override{$lang} // {};
Condition0%0/2
$w2n_override{$lang} // {}
l!l
00
MC/DC0%0/1
$w2n_override{$lang} // {}
$w2n_override{$lang}
Truth table0%0/2
A
A= $w2n_override{$lang}
Aresult
11
00
73 $known{$lang} = {
74 package => $ov->{package} // 'Word2Num',
75 version => $ov->{version} // '',
76 function => $ov->{function} // 'w2n',
77 0 ▶ code => $ov->{code} // $template_func,
Condition0%0/9
$ov->{"package"} // "Word2Num"
l!l
00
$ov->{"version"} // ""
l!l
00
$ov->{"function"} // "w2n"
l!l
00
$ov->{"code"} // $template_func
l!l&&r!l&&!r
000
MC/DC0%0/1
$ov->{"package"} // "Word2Num"
$ov->{"package"}
MC/DC0%0/1
$ov->{"version"} // ""
$ov->{"version"}
MC/DC0%0/1
$ov->{"function"} // "w2n"
$ov->{"function"}
MC/DC0%0/2
$ov->{"code"} // $template_func
$ov->{"code"} $template_func
Truth table0%0/2
A
A= $ov->{"package"}
Aresult
11
00
Truth table0%0/2
A
A= $ov->{"version"}
Aresult
11
00
Truth table0%0/2
A
A= $ov->{"function"}
Aresult
11
00
Truth table0%0/3
A // B
A= $ov->{"code"}B= $template_func
ABresult
1X1
011
000
78 };
79 }
80 }
81 }
82 }
83  
84 # }}}
85  
86 # {{{ new constructor
87  
88 sub new {
89 2 ▶ return bless {}, shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
90 }
91  
92 # }}}
93 # {{{ cardinal convert text to number
94  
95 sub cardinal :Export {
96 2 ▶ my $self = ref($_[0]) ? shift : __PACKAGE__->new();
Subroutine cardinal: called CC=11 SCAR=44.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
97 2 my $result = '';
98  
99 2 ▶ my $lang = shift // return $result;
Condition50%1/2
shift() // (return $result)
l!l
02
MC/DC0%0/1
shift() // (return $result)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
100 0 ▶ my $word = shift // return $result;
Condition0%0/2
shift() // (return $result)
l!l
00
MC/DC0%0/1
shift() // (return $result)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
101  
102 0 $lang = lc $lang;
103 0 ▶ $lang = $iso1_to_3{$lang} if exists $iso1_to_3{$lang};
Branch: 1 branch
BranchTrueFalseTotal
if exists $iso1_to_3{$lang} 0 0 0
104  
105 0 ▶ my @langs = ($lang eq '*')
Branch: 1 branch
BranchTrueFalseTotal
$lang eq '*' ? : 0 0 0
106 ? sort keys %known
107 : ($lang)
108 ;
109  
110 0 ▶ return $result if (!defined $known{$langs[0]});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Lingua::Word2Num::known{$langs[0]} 0 0 0
111  
112 0 for my $lang (@langs) {
113 0 eval $self->preprocess_code($lang); ## no critic 'eval'
114 0 ▶ carp $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
115  
116 0 ▶ return $result if (defined $result && $result ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $result and $result ne "" 0 0 0
Condition0%0/3
defined $result and $result ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $result and $result ne ""
defined $result $result ne ""
Truth table0%0/3
A and B
A= defined $resultB= $result ne ""
ABresult
0X0
100
111
117 }
118  
119 0 return '';
120 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 # }}}
123 # {{{ known_langs list of currently supported languages
124  
125 sub known_langs :Export {
126 0 ▶ return [sort keys %known];
Subroutine known_langs: not called CC=1 SCAR=6.9
Pod documented
127 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 # }}}
130 # {{{ preprocess_code prepare code for evaluation
131  
132 sub preprocess_code {
133 2 ▶ my $self = shift;
Subroutine preprocess_code: called CC=4 SCAR=21.7
Pod documented
134 2 ▶ my $lang = shift // return;
Condition100%2/2
shift() // (return)
l!l
11
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
135  
136 1 ▶ return if !defined $known{$lang};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Lingua::Word2Num::known{$lang} 1 0 1
137  
138 0 my $result = $known{$lang}{code};
139 0 my $pkg_name = 'Lingua::' . uc($lang) . '::' . $known{$lang}{package};
140 0 ▶ my $pkg_name_with_version = $known{$lang}{version} ne ''
Branch: 1 branch
BranchTrueFalseTotal
$Lingua::Word2Num::known{$lang}{'version'} ne '' ? : 0 0 0
141 ? "$pkg_name $known{$lang}{version}" : $pkg_name;
142 0 my $function = $known{$lang}{function};
143  
144 0 $result =~ s/__PACKAGE_WITH_VERSION__/$pkg_name_with_version/g;
145 0 $result =~ s/__PACKAGE__/$pkg_name/g;
146 0 $result =~ s/__FUNCTION__/$function/g;
147 0 $result =~ s/__CHARSET__/$known{$lang}{charset}/g;
148  
149 0 return $result;
150 }
151  
152 # }}}
153  
154 1 1;
155 __END__
↑ Summary