AlphabeticalLength.pm

filter: Statementstmt 79.3% 73 / 92 Branchbran 39.2% 11 / 28 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 89.4% 17 / 19 Podpod 100% 4 / 4 totaltotal 70.8% 107 / 151 CC 18 SCAR 21.3
CC
18
Cov
69%
ith
20.7
new
11.3
oeis_anum
6.0
CRAP 8.4
↑ Summary AlphabeticalLengthSteps.pm »
1 # Copyright 2012, 2015 Kevin Ryde
2  
3 # This file is part of Math-NumSeq-Alpha.
4 #
5 # Math-NumSeq-Alpha is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-NumSeq-Alpha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-NumSeq-Alpha. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Math::NumSeq::AlphabeticalLength;
19 3 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 $VERSION = 4;
25 3 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Math::NumSeq::Base::IterateIth;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 @ISA = ('Math::NumSeq::Base::IterateIth',
28 'Math::NumSeq');
29 3 *_is_infinite = \&Math::NumSeq::_is_infinite;
30  
31 3 ▶use Lingua::Any::Numbers;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # uncomment this to run the ### lines
34 #use Smart::Comments;
35  
36  
37 # use constant name => Math::NumSeq::__('...');
38 3 ▶use constant description => Math::NumSeq::__('Length of i written out in words.');
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use constant default_i_start => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use constant characteristic_count => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use constant characteristic_smaller => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use constant characteristic_integer => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 3 ▶use constant::defer parameter_info_array => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=3 SCAR=11.0
45 3 require Lingua::Any::Numbers;
46  
47 # Lingua::Any::Numbers version 0.44 excludes Lingua::Conlang::Numbers
48 # which is a front-end to various Esperanto etc, no need to do that here.
49 3 my @choices = sort(Lingua::Any::Numbers::available());
50 ### @choices
51  
52 # move 'EN' to the start, if it exists (which it should)
53 3 foreach my $i (0 .. $#choices) {
54 15 ▶ if ($choices[$i] eq 'EN') {
Branch: 1 branch
BranchTrueFalseTotal
if ($choices[$i] eq "EN") 3 12 15
55 3 unshift @choices, splice(@choices, $i, 1);
56 3 last;
57 }
58 }
59 ### @choices
60  
61 return [
62 {
63 3 name => 'lang',
64 display => ('Language'),
65 type => 'string',
66 default => 'EN',
67 choices => \@choices,
68 width => 8,
69 # description => Math::NumSeq::__('...'),
70 },
71 {
72 name => 'number_type',
73 type => 'enum',
74 default => 'cardinal',
75 choices => ['cardinal','ordinal'],
76 choices_display => [Math::NumSeq::__('Cardinal'),
77 Math::NumSeq::__('Ordinal')],
78 # description => Math::NumSeq::__('...'),
79 },
80 {
81 name => 'conjunctions',
82 display => ('Conjunctions'),
83 type => 'boolean',
84 default => 1,
85 description => ('Whether to include conjunctions "and" or "et" in the words of the sequence.'),
86 },
87  
88 # Not through Lingua::Any::Numbers interface
89 # {
90 # name => 'gender',
91 # type => 'enum',
92 # default => 'M',
93 # choices => ['M','F','N'],
94 # # description => Math::NumSeq::__('...'),
95 # },
96 # {
97 # name => 'declension',
98 # type => 'enum',
99 # default => 'nominative',
100 # choices => ['nominative','genitive','dative','accusative','ablative'],
101 # # description => Math::NumSeq::__('...'),
102 # },
103 ];
104 3 };
105  
106 1 ▶sub values_min {
Subroutine values_min: called CC=2 SCAR=7.0
Pod documented
107 1 my ($self) = @_;
108 ### values_min(): $self
109 1 my $i_start = $self->i_start;
110 return ($self->{'values_min'}
111 1 ▶ = min (map {$self->ith($_)||0}
Condition50%1/2
$self->ith($_) || 0
l!l
210
MC/DC0%0/1
$self->ith($_) || 0
$self->ith($_)
Truth table50%1/2
A
A= $self->ith($_)
Aresult
11
00
112 $i_start .. $i_start + 20));
113 }
114  
115 #------------------------------------------------------------------------------
116 # cf A052360 en cardinal including spaces and hyphens
117 # A052363 new longest alpha
118 # A134629 first requiring n letters
119 # A006968 or A092196 Roman numerals, up to 4000
120 # A037193 num consonants in French
121 #
122 # A164058 Number of curves in n-th letter of capitalized English alphabet (A being first).
123 # A164059 Number of straight plus curved segments in the capitalized English name of n.
124 #
125 # A003078 (Danish) DK
126 # A001050 (Finnish) FI
127 # A001368 (Irish Gaelic) GA
128 # A051785 (Catalan) CA
129 # A056597 (Serbian or Croatian) SR
130 # A132984 (Latin) LA
131 # A140395 (Hindi) HI
132 # A053306 (Galego)
133 # A140396 (Welsh) CY
134 # A140438 (Tamil) TA
135 # A014656 (Bokmal) NB, was NO Lingua::NO::Num2Word
136 # A028292 (Nynorsk) NN
137  
138 # catalogued in Alpha.pm
139 3 my %oeis_anum
140 = ('en,cardinal,0,noand' => 'A005589',
141 'en,ordinal,1,noand' => 'A006944',
142  
143 # Lingua::CS::Num2Word doubtful ...
144 'cs,cardinal,1' => 'A010038', # Czech, Lingua::CS::Num2Word
145  
146 # Lingua::DE::Num2Word doubtful ...
147 # 'de,cardinal,1' => 'A007208', # German, Lingua::DE::Num2Word
148  
149 'eo,cardinal,0' => 'A057853', # Esperanto, Lingua::EO::Numbers
150  
151 'es,cardinal,0' => 'A011762', # Spanish, Lingua::ES::Numeros
152  
153 'fr,ordinal,1' => 'A006969', # French, Lingua::FR::Numbers
154 'fr,cardinal,0,incspaces' => 'A007005',
155  
156 'hu,cardinal,1' => 'A007292', # Hungarian, Lingua::HU::Numbers
157  
158 # Not quite, centottanta in A026858 vs centoottanta in Lingua
159 # 'it,cardinal,0' => 'A026858', # Italian, Lingua::IT::Numbers
160  
161 'ja,cardinal,0' => 'A030166', # Japanese Kanji, Lingua::JA::Numbers
162  
163 'nl,cardinal,1' => 'A090589', # Dutch, Lingua::NL::Numbers
164 # cf A007485 ij as one letter
165  
166 # Not sure about 11=ellve cf A014656(11)=6
167 # # Bokmal NO, lately code change to NB
168 # 'no,cardinal,1' => 'A014656', # Lingua::NO::Num2Word
169 #
170 # cf A028292 Nynorsk A028292(1)=3
171 # 'nn,cardinal,1' => 'A028292',
172  
173 'pl,cardinal,0' => 'A008962', # Polish, Lingua::PL::Numbers
174  
175 # Not quite, A057696 Brazilian Portuguese "catorze"
176 # whereas "quatorze" in Lingua::PT::Nums2Words
177 # 'pt,cardinal,1' => 'A057696', # Lingua::PT::Nums2Words
178 # cf A057697 including spaces
179  
180 # No, Lingua::RU::Number 0.05 is money amounts only.
181 # 'ru,cardinal,1' => 'A006994', # Russian, Lingua::RU::Number
182  
183 'sv,cardinal,0' => 'A059124', # Swedish, Lingua::SV::Numbers
184  
185 'tr,cardinal,1' => 'A057435', # Turkish, Lingua::TR::Numbers
186 );
187 0 ▶sub oeis_anum {
Subroutine oeis_anum: not called CC=2 SCAR=17.9
Pod documented
188 0 my ($self) = @_;
189 ### oeis_anum: $self
190  
191 my $key = join(',',
192 $self->{lang},
193 $self->{number_type},
194 0 $self->i_start);
195 0 ▶ if (! $self->{'conjunctions'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"conjunctions"}) 0 0 0
196 0 $key .= ',noand';
197 }
198 ### $key
199 0 return $oeis_anum{$key};
200 }
201  
202 #------------------------------------------------------------------------------
203  
204 3 my %conjunctions_word = (en => 'and',
205 fr => 'et',
206 );
207 4 ▶sub new {
Subroutine new: called CC=5 SCAR=24.2
Pod documented
208 4 my $self = shift->SUPER::new(@_);
209  
210 4 my $lang = $self->{'lang'} = lc($self->{'lang'});
211  
212 4 ▶ if ($lang eq 'sv') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang eq "sv") 0 4 4
213 0 my $str = Lingua::Any::Numbers::to_string (2, 'sv');
214 ### $str
215 0 ▶ if (length($str) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str == 4) 0 0 0
216 # Lingua::SV::Numbers gives utf-8 bytes, mangle it down to chars
217 ### decode_chars mangle out utf8 ...
218 0 ▶ $self->{'decode_chars'} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 $_[0] =~ s/\303./X/g;
220 0 };
221 }
222 }
223  
224 4 ▶ if (! $self->{'conjunctions'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"conjunctions"}) 0 4 4
225 0 ▶ if (! defined $self->{'conjunctions_word'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"conjunctions_word"}) 0 0 0
226 0 $self->{'conjunctions_word'} = $conjunctions_word{$lang}
227 }
228 }
229  
230 4 return $self;
231 }
232  
233 344 ▶sub ith {
Subroutine ith: called CC=10 SCAR=30.3
Pod documented
234 344 my ($self, $i) = @_;
235 ### AlphabeticalLength ith(): "$i"
236  
237 344 ▶ if (_is_infinite($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_infinite($i)) 0 344 344
238 0 return undef;
239 }
240  
241 344 my $lang = $self->{'lang'};
242 344 my $str;
243 344 ▶ if ($self->{'number_type'} eq 'ordinal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'number_type'} eq 'ordinal') { } 0 344 344
244 0 $str = Lingua::Any::Numbers::to_ordinal ($i, $lang);
245 0 ▶ if ($str eq $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq $i) 0 0 0
246 # some modules without ordinal support return $i as numerals unchanged
247 0 return undef;
248 }
249  
250 } else {
251 344 ▶ if ($lang eq 'eo' && $i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang eq 'eo' and $i == 1) { } 0 344 344
Condition33%1/3
$lang eq "eo" and $i == 1
!ll&&!rl&&r
34400
MC/DC0%0/2
$lang eq "eo" and $i == 1
$lang eq "eo" $i == 1
Truth table33%1/3
A and B
A= $lang eq "eo"B= $i == 1
ABresult
0X0
100
111
252 # HACK: avoid warn() from num2eo(1) in Lingua::EO::Numbers 0.03
253 0 $str = 'unu';
254 } else {
255 344 $str = Lingua::Any::Numbers::to_string ($i, $lang);
256 }
257 }
258 344 ▶ if (my $decode_chars = $self->{'decode_chars'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $decode_chars = $self->{"decode_chars"}) 0 344 344
259 0 $decode_chars->($str);
260 }
261 ### language: $self->{'lang'}
262 ### $str
263  
264 344 ▶ if (! $self->{'conjunctions'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"conjunctions"}) 0 344 344
265 0 ▶ if ($self->{'lang'} eq 'en') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lang"} eq "en") 0 0 0
266 0 $str =~ s/\b$self->{'conjunctions_word'}\b//ig;
267 }
268 }
269  
270 344 my $count = 0;
271 344 ▶ while ($str =~ /(\w|[^[:ascii:]])/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /(\w|[^[:ascii:]])/g) 1758 344 2102
272 1758 $count += length($1);
273 }
274 ### $count
275 344 return $count;
276  
277 # ### letters only: $str
278 # counting whitespace ...
279 # $str =~ s/[^[:word:][:space:]]//g;
280 # return length($str);
281 }
282  
283 3 1;
284 __END__
↑ Summary AlphabeticalLengthSteps.pm »