Deva.pm

filter: Statementstmt 88.2% 196 / 222 Branchbran 85.8% 103 / 120 Conditioncond 66.6% 38 / 57 MC/DCmcdc 42.8% 12 / 28 Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 82.6% 372 / 450 CC 86 SCAR 34.3
CC
86
Cov
84%
d_to_aksaras
61.0
l_to_aksaras
30.4
to_deva
9.1
CRAP 31.0
↑ Summary Deva/Aksara.pm »
1 package Lingua::Deva;
2  
3 6 ▶use v5.12.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use charnames qw( :full );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use open qw( :encoding(UTF-8) :std );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Unicode::Normalize qw( NFD NFC );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Lingua::Deva::Aksara;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Lingua::Deva::Maps qw( %Consonants %Vowels %Diacritics %Finals
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 $Inherent $Virama $Avagraha );
15  
16 =encoding UTF-8
17  
18 =head1 NAME
19  
20 Lingua::Deva - Convert between Latin and Devanagari Sanskrit text
21  
22 =cut
23  
24 6 our $VERSION = '1.20';
25  
26 =head1 SYNOPSIS
27  
28 use v5.12.1;
29 use strict;
30 use utf8;
31 use charnames ':full';
32 use Lingua::Deva;
33  
34 # Basic usage
35 my $d = Lingua::Deva->new();
36 say $d->to_latin('आसीद्राजा'); # prints 'āsīdrājā'
37 say $d->to_deva('Nalo nāma'); # prints 'नलो नाम'
38  
39 # With configuration: strict, allow Danda, 'w' for 'v'
40 my %c = %Lingua::Deva::Maps::Consonants;
41 $d = Lingua::Deva->new(
42 strict => 1,
43 allow => [ "\N{DEVANAGARI DANDA}" ],
44 C => do { $c{'w'} = delete $c{'v'}; \%c },
45 );
46 say $d->to_deva('ziwāya'); # 'zइवाय', warning for 'z'
47 say $d->to_latin('सर्वम्।'); # 'sarwam।', no warnings
48  
49 =head1 DESCRIPTION
50  
51 The C<Lingua::Deva> module provides facilities for converting Sanskrit in
52 various Latin transliterations to Devanagari and vice-versa. "Deva" is the
53 name for the Devanagari (I<devanāgarī>) script according to ISO 15924.
54  
55 The facilities of this module are exposed through a simple interface in the
56 form of instances of the L<Lingua::Deva> class. A number of configuration
57 options can be passed to it during initialization.
58  
59 Using the module is as simple as creating a C<Lingua::Deva> instance and
60 calling its methods L<to_deva()> or L<to_latin()> with appropriate string
61 arguments.
62  
63 my $d = Lingua::Deva->new();
64 say $d->to_latin('कामसूत्र');
65 say $d->to_deva('Kāmasūtra');
66  
67 By default, transliteration follows the widely used IAST conventions. Three
68 other ready-made transliteration schemes are also included with this module,
69 ISO 15919 (C<ISO15919>), Harvard-Kyoto (C<HK>), and ITRANS.
70  
71 my $d = Lingua::Deva->new(map => 'HK');
72 say $d->to_latin('कामसूत्र'); # prints 'kAmasUtra'
73  
74 For additional flexibility all mappings can be completely customized; users
75 can also provide their own.
76  
77 use Lingua::Deva::Maps::ISO15919;
78 my %f = %Lingua::Deva::Maps::ISO15919::Finals;
79 my $d = Lingua::Deva->new(
80 map => 'IAST', # use IAST transliteration
81 casesensitive => 1, # do not case fold
82 F => \%f, # ISO 15919 mappings for finals
83 );
84 say $d->to_deva('Vṛtraṁ'); # prints 'Vऋत्रं'
85  
86 For more information on customization see L<Lingua::Deva::Maps>.
87  
88 Behind the scenes, all translation is done via an intermediate object
89 representation called "Aksara" (Sanskrit I<akṣara>). These objects are
90 instances of L<Lingua::Deva::Aksara>, which provides an interface to inspect
91 and manipulate individual Aksaras.
92  
93 # Create an array of Aksaras
94 my $a = $d->l_to_aksaras('Kāmasūtra');
95  
96 # Print vowel in the fourth Aksara
97 say $a->[3]->vowel();
98  
99 The methods and options of C<Lingua::Deva> are described below.
100  
101 =head2 Methods
102  
103 =over 4
104  
105 =item new()
106  
107 Constructor. Takes the following optional arguments.
108  
109 =over 4
110  
111 =item C<< map => 'IAST'|'ISO15919'|'HK'|'ITRANS' >>
112  
113 Selects one of the ready-made transliteration schemes.
114  
115 =item C<< casesensitive => (0|1) >>
116  
117 Determines whether case is treated as distinctive or not. Some schemes (eg.
118 Harvard-Kyoto) set this to C<1> while others (eg. IAST) set it to C<0>.
119  
120 Default is C<0>.
121  
122 =item C<< strict => (0|1) >>
123  
124 In I<strict> mode invalid input is flagged with warnings. Invalid means
125 either not a Devanagari token (eg. I<q>) or structurally ill-formed (eg. a
126 Devanagari diacritic vowel following an independent vowel).
127  
128 Default is C<0>.
129  
130 =item C<< allow => [ ... ] >>
131  
132 In strict mode, the C<allow> array can be used to exempt certain characters
133 from being flagged as invalid even though they normally would be.
134  
135 =item C<< avagraha => "'" >>
136  
137 Specifies the Latin character used for the transcription of I<avagraha> (ऽ).
138  
139 Default is C<"'"> (apostrophe).
140  
141 =item C<< C => { consonants map } >>
142  
143 =item C<< V => { independent vowels map } >>
144  
145 =item C<< D => { diacritic vowels map } >>
146  
147 =item C<< F => { finals map } >>
148  
149 Transliteration maps in the direction from Latin to Devanagari script.
150  
151 =item C<< DC => { consonants map } >>
152  
153 =item C<< DV => { independent vowels map } >>
154  
155 =item C<< DD => { diacritic vowels map } >>
156  
157 =item C<< DF => { finals map } >>
158  
159 Transliteration maps in the direction from Devanagari to Latin script. When
160 these are not given, reversed versions of the Latin to Devanagari maps are
161 used.
162  
163 The default maps are in L<Lingua::Deva::Maps>. To customize, make a copy of
164 an existing mapping hash (or create your own) and pass it to one of these
165 parameters.
166  
167 =back
168  
169 =cut
170  
171 16 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
172 16 my ($class, %opts) = @_;
173  
174 16 my $self = {
175 casesensitive => 0,
176 strict => 0,
177 allow => [], # converted to a hash for efficiency
178 avagraha => "'",
179 C => \%Consonants,
180 V => \%Vowels,
181 D => \%Diacritics,
182 F => \%Finals,
183 %opts,
184 };
185  
186 # Transliteration scheme setup
187 16 ▶ if (defined $self->{map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"map"}) 4 12 16
188 4 ▶ if ($self->{map} =~ /^(ISO15919|ITRANS|IAST|HK)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'map'} =~ /^(ISO15919|ITRANS|IAST|HK)$/u) { } 3 1 4
189 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
190 3 my $pkg = "Lingua::Deva::Maps::$1";
191 3 eval "require $pkg";
192 3 for (qw(Consonants Vowels Diacritics Finals)) {
193 12 my $k = substr $_, 0, 1;
194 12 ▶ $self->{$k} = do { my %c = %{"${pkg}::$_"}; \%c } unless defined $opts{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{$k} 10 2 12
195 }
196 3 ▶ if (!defined $opts{casesensitive} and defined ${"${pkg}::CASE"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{"casesensitive"} and defined ${"${pkg}::CASE";}) 2 1 3
Condition66%2/3
not defined $opts{"casesensitive"} and defined ${"${pkg}::CASE";}
!ll&&!rl&&r
102
MC/DC50%1/2
not defined $opts{"casesensitive"} and defined ${"${pkg}::CASE";}
not defined $opts{"casesensitive"} defined ${"${pkg}::CASE";}
Truth table66%2/3
not(A) and B
A= not defined $opts{"casesensitive"}B= defined ${"${pkg}::CASE";}
ABresult
0X0
100
111
197 2 $self->{casesensitive} = ${"${pkg}::CASE"};
198 }
199 }
200 else {
201 1 carp("Invalid transliteration map, using default");
202 }
203 }
204  
205 # By default use reversed maps for the opposite direction (DC DV DD DF)
206 16 for (qw( C V D F )) {
207 64 ▶ $self->{"D$_"} = do { my %m = reverse %{$self->{$_}}; \%m } if !defined $self->{"D$_"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self{"D$_"} 63 1 64
208 }
209  
210 # Make the inherent vowel translate to '' in the D map
211 16 $self->{D}->{$Inherent} = '';
212  
213 # Convert the 'allow' array to a hash for fast lookup
214 16 my %allow = map { $_ => 1 } @{ $self->{allow} };
215 16 $self->{allow} = \%allow;
216  
217 # Make consonants, vowels, and finals available as tokens
218 16 my %tokens = (%{ $self->{C} }, %{ $self->{V} }, %{ $self->{F} });
219 16 $self->{T} = \%tokens;
220  
221 16 return bless $self, $class;
222 }
223  
224 =item l_to_tokens()
225  
226 Converts a string of Latin characters into tokens and returns a reference to
227 an array of tokens. A "token" is either a character sequence which may
228 constitute a single Devanagari grapheme or a single non-Devanagari character.
229 In the first sense, a token is simply any key in the transliteration maps.
230  
231 my $t = $d->l_to_tokens("Bhārata\n");
232 # $t now refers to the array ['Bh','ā','r','a','t','a',"\n"]
233  
234 The input string is normalized with
235 L<Unicode::Normalize::NFD|Unicode::Normalize>. No chomping takes place.
236 Upper case and lower case distinctions are preserved.
237  
238 =cut
239  
240 20016 ▶sub l_to_tokens {
Subroutine l_to_tokens: called CC=4 SCAR=13.9
Pod documented
241 20016 my ($self, $text) = @_;
242 20016 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 20015 20016
243  
244 20015 my $nfdtext = NFD($text);
245  
246 20015 my $re = join '|', reverse sort { length $a <=> length $b } keys %{$self->{T}};
247 20015 ▶ my $reobject = $self->{casesensitive} ? qr/($re|.)/s : qr/($re|.)/is;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'casesensitive'} ? : 2 20013 20015
248  
249 20015 my @tokens;
250 20015 ▶ while ($nfdtext =~ /$reobject/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfdtext =~ /$reobject/cgu) 1244245 20015 1264260
251 1244245 push @tokens, $1;
252 }
253  
254 20015 return \@tokens;
255 }
256  
257 =item l_to_aksaras()
258  
259 Converts a Latin string (or a reference to an array of tokens) into
260 L<Aksaras|Lingua::Deva::Aksara> and returns a reference to an array of
261 Aksaras.
262  
263 my $a = $d->l_to_aksaras('hyaḥ');
264 is( ref($a->[0]), 'Lingua::Deva::Aksara', 'one aksara object' );
265 done_testing();
266  
267 Input tokens which can not be part of an Aksara pass through untouched. Thus,
268 the resulting array can contain both C<Lingua::Deva::Aksara> objects and
269 separate tokens.
270  
271 In I<strict> mode warnings for invalid tokens are output.
272  
273 =cut
274  
275 15014 ▶sub l_to_aksaras {
Subroutine l_to_aksaras: called CC=28 SCAR=34.2
Pod documented
276 15014 my ($self, $input) = @_;
277  
278 # Input can be either a string (scalar) or an array reference
279 15014 ▶ my $tokens = ref($input) eq '' ? $self->l_to_tokens($input) : $input;
Branch: 1 branch
BranchTrueFalseTotal
ref $input eq '' ? : 15012 2 15014
280  
281 15014 my @aksaras;
282 my $a;
283 15014 my $state = 0;
284 15014 my ($C, $V, $F) = ($self->{C}, $self->{V}, $self->{F});
285  
286 # Aksarization is implemented with a state machine.
287 # State 0: Not currently constructing an aksara, ready for any input
288 # State 1: Constructing consonantal onset
289 # State 2: Onset and vowel read, ready for final or end of aksara
290  
291 15014 for my $t (@$tokens) {
292 933222 ▶ my $lct = $self->{casesensitive} ? $t : lc $t;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'casesensitive'} ? : 10 933212 933222
293 933222 ▶ if ($state == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($state == 0) { } 189053 744169 933222
elsif ($state == 1) { } 414094 330075 744169
elsif ($state == 2) { } 330075 0 330075
294 189053 ▶ if (exists $C->{$lct}) { # consonant: new aksara
Branch: 2 branches
BranchTrueFalseTotal
if (exists $C->{$lct}) { } 114013 75040 189053
elsif (exists $V->{$lct}) { } 27013 48027 75040
295 114013 $a = Lingua::Deva::Aksara->new( onset => [ $lct ] );
296 114013 $state = 1;
297 }
298 elsif (exists $V->{$lct}) { # vowel: vowel-initial aksara
299 27013 $a = Lingua::Deva::Aksara->new( vowel => $lct );
300 27013 $state = 2;
301 }
302 else { # final/space/avagraha/other
303 48027 ▶ if ($t !~ /\p{Space}/ and $t ne $self->{avagraha}
Branch: 1 branch
BranchTrueFalseTotal
if (not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"} and not exists $self->{"allow"}{$t}) 1004 47023 48027
Condition100%9/9
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"} and not exists $self->{"allow"}{$t}
!ll&&!rl&&r
4402130021004
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"}
!ll&&!rl&&r
3601080114006
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"}
!ll&&!rl&&r
33010300012017
MC/DC100%4/4
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"} and not exists $self->{"allow"}{$t}
not $t =~ /\p{Space}/u $t ne $self->{"avagraha"} $self->{"strict"} not exists $self->{"allow"}{$t}
Truth table100%5/5
not(A) and B and C and not(D)
A= not $t =~ /\p{Space}/uB= $t ne $self->{"avagraha"}C= $self->{"strict"}D= not exists $self->{"allow"}{$t}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
304 and $self->{strict} and !exists $self->{allow}->{$t}) {
305 1004 carp("Invalid token $t read");
306 }
307 48027 push @aksaras, $t;
308 }
309 }
310 elsif ($state == 1) {
311 414094 ▶ if (exists $C->{$lct}) { # consonant: part of onset
Branch: 2 branches
BranchTrueFalseTotal
if (exists $C->{$lct}) { } 84028 330066 414094
elsif (exists $V->{$lct}) { } 309066 21000 330066
312 84028 push @{ $a->onset() }, $lct;
313 }
314 elsif (exists $V->{$lct}) { # vowel: vowel nucleus
315 309066 $a->vowel( $lct );
316 309066 $state = 2;
317 }
318 else { # final/space/avagraha/other
319 21000 ▶ if ($t !~ /\p{Space}/ and $t ne $self->{avagraha}
Branch: 1 branch
BranchTrueFalseTotal
if (not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"} and not exists $self->{"allow"}{$t}) 0 21000 21000
Condition33%3/9
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"} and not exists $self->{"allow"}{$t}
!ll&&!rl&&r
2100000
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"}
!ll&&!rl&&r
2100000
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"}
!ll&&!rl&&r
2100000
MC/DC0%0/4
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"} and not exists $self->{"allow"}{$t}
not $t =~ /\p{Space}/u $t ne $self->{"avagraha"} $self->{"strict"} not exists $self->{"allow"}{$t}
Truth table20%1/5
not(A) and B and C and not(D)
A= not $t =~ /\p{Space}/uB= $t ne $self->{"avagraha"}C= $self->{"strict"}D= not exists $self->{"allow"}{$t}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
320 and $self->{strict} and !exists $self->{allow}->{$t}) {
321 0 carp("Invalid token $t read");
322 }
323 21000 push @aksaras, $a, $t;
324 21000 $state = 0;
325 }
326 }
327 elsif ($state == 2) {
328 330075 ▶ if (exists $C->{$lct}) { # consonant: new aksara
Branch: 3 branches
BranchTrueFalseTotal
if (exists $C->{$lct}) { } 216054 114021 330075
elsif (exists $V->{$lct}) { } 0 114021 114021
elsif (exists $F->{$lct}) { } 33007 81014 114021
329 216054 push @aksaras, $a;
330 216054 $a = Lingua::Deva::Aksara->new( onset => [ $lct ] );
331 216054 $state = 1;
332 }
333 elsif (exists $V->{$lct}) { # vowel: new vowel-initial aksara
334 0 push @aksaras, $a;
335 0 $a = Lingua::Deva::Aksara->new( vowel => $lct );
336 0 $state = 2;
337 }
338 elsif (exists $F->{$lct}) { # final: end of aksara
339 33007 $a->final( $lct );
340 33007 push @aksaras, $a;
341 33007 $state = 0;
342 }
343 else { # space/avagraha/other
344 81014 ▶ if ($t !~ /\p{Space}/ and $t ne $self->{avagraha}
Branch: 1 branch
BranchTrueFalseTotal
if (not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"} and not exists $self->{"allow"}{$t}) 2000 79014 81014
Condition77%7/9
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"} and not exists $self->{"allow"}{$t}
!ll&&!rl&&r
7901402000
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"}
!ll&&!rl&&r
7501440002000
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"}
!ll&&!rl&&r
7501406000
MC/DC50%2/4
not $t =~ /\p{Space}/u and $t ne $self->{"avagraha"} and $self->{"strict"} and not exists $self->{"allow"}{$t}
not $t =~ /\p{Space}/u $t ne $self->{"avagraha"} $self->{"strict"} not exists $self->{"allow"}{$t}
Truth table60%3/5
not(A) and B and C and not(D)
A= not $t =~ /\p{Space}/uB= $t ne $self->{"avagraha"}C= $self->{"strict"}D= not exists $self->{"allow"}{$t}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
345 and $self->{strict} and !exists $self->{allow}->{$t}) {
346 2000 carp("Invalid token $t read");
347 }
348 81014 push @aksaras, $a, $t;
349 81014 $state = 0;
350 }
351 }
352 }
353  
354 # Finish aksara currently under construction
355 15014 ▶ push @aksaras, $a if $state == 1 or $state == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $state == 1 or $state == 2 6005 9009 15014
Condition100%3/3
$state == 1 or $state == 2
l!l&&r!l&&!r
160049009
MC/DC100%2/2
$state == 1 or $state == 2
$state == 1 $state == 2
Truth table100%3/3
A or B
A= $state == 1B= $state == 2
ABresult
1X1
011
000
356  
357 15014 return \@aksaras;
358 }
359  
360 6 *l_to_aksara = \&l_to_aksaras; # alias
361  
362 =item d_to_aksaras()
363  
364 Converts a Devanagari string into L<Aksaras|Lingua::Deva::Aksara> and returns
365 a reference to an array of Aksaras.
366  
367 my $aksaras = $d->d_to_aksaras('बुद्धः');
368 my $onset = $aksaras->[1]->onset();
369 is_deeply( $onset, ['d', 'dh'], 'onset of second aksara' );
370 done_testing();
371  
372 Input tokens which can not be part of an Aksara pass through untouched. Thus,
373 the resulting array can contain both C<Lingua::Deva::Aksara> objects and
374 separate tokens.
375  
376 In I<strict> mode warnings for invalid tokens are output.
377  
378 =cut
379  
380 5006 ▶sub d_to_aksaras {
Subroutine d_to_aksaras: called CC=34 SCAR=41.1
Pod documented
381 5006 my ($self, $input) = @_;
382  
383 5006 my @chars = split //, $input;
384 5006 my @aksaras;
385 my $a;
386 5006 my $state = 0;
387 my ($DC, $DV, $DD, $DF) = ( $self->{DC}, $self->{DV},
388 5006 $self->{DD}, $self->{DF} );
389  
390 # Aksarization is implemented with a state machine.
391 # State 0: Not currently constructing an aksara, ready for any input
392 # State 1: Consonant with inherent vowel, ready for vowel, Virama, final
393 # State 2: Virama read, ready for consonant or end of aksara
394 # State 3: Vowel read, ready for final or end of aksara
395 # The inherent vowel needs to be taken into account specially
396  
397 5006 for my $c (@chars) {
398 310113 ▶ if ($state == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($state == 0) { } 63028 247085 310113
elsif ($state == 1) { } 138050 109035 247085
elsif ($state == 2) { } 35013 74022 109035
elsif ($state == 3) { } 74022 0 74022
399 63028 ▶ if (exists $DC->{$c}) { # consonant: new aksara
Branch: 3 branches
BranchTrueFalseTotal
if (exists $DC->{$c}) { } 38006 25022 63028
elsif (exists $DV->{$c}) { } 9007 16015 25022
elsif ($c =~ /$Lingua::Deva::Avagraha/u) { } 1000 15015 16015
400 38006 $a = Lingua::Deva::Aksara->new( onset => [ $DC->{$c} ] );
401 38006 $state = 1;
402 }
403 elsif (exists $DV->{$c}) { # vowel: vowel-initial aksara
404 9007 $a = Lingua::Deva::Aksara->new( vowel => $DV->{$c} );
405 9007 $state = 3;
406 }
407 elsif ($c =~ /$Avagraha/) { # Avagraha
408 1000 push @aksaras, $self->{avagraha};
409 }
410 else { # final or other: invalid
411 15015 ▶ if ($c !~ /\p{Space}/ and $self->{strict} and !exists $self->{allow}->{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}) 1 15014 15015
Condition100%6/6
not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}
!ll&&!rl&&r
1501221
not $c =~ /\p{Space}/u and $self->{"strict"}
!ll&&!rl&&r
1100640063
MC/DC100%3/3
not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}
not $c =~ /\p{Space}/u $self->{"strict"} not exists $self->{"allow"}{$c}
Truth table100%4/4
not(A) and B and not(C)
A= not $c =~ /\p{Space}/uB= $self->{"strict"}C= not exists $self->{"allow"}{$c}
ABCresult
0XX0
10X0
1100
1111
412 1 carp("Invalid character $c read");
413 }
414 15015 push @aksaras, $c;
415 }
416 }
417 elsif ($state == 1) {
418 138050 ▶ if ($c =~ /$Virama/) { # Virama: consonant-final
Branch: 6 branches
BranchTrueFalseTotal
if ($c =~ /$Lingua::Deva::Virama/u) { } 35013 103037 138050
elsif (exists $DD->{$c}) { } 67016 36021 103037
elsif (exists $DV->{$c}) { } 0 36021 36021
elsif (exists $DC->{$c}) { } 20012 16009 36021
elsif (exists $DF->{$c}) { } 7004 9005 16009
elsif ($c =~ /$Lingua::Deva::Avagraha/u) { } 0 9005 9005
419 35013 $state = 2;
420 }
421 elsif (exists $DD->{$c}) { # diacritic: vowel nucleus
422 67016 $a->vowel( $DD->{$c} );
423 67016 $state = 3;
424 }
425 elsif (exists $DV->{$c}) { # vowel: new vowel-initial aksara
426 0 $a->vowel( $Inherent );
427 0 push @aksaras, $a;
428 0 $a = Lingua::Deva::Aksara->new( vowel => $DV->{$c} );
429 0 $state = 3;
430 }
431 elsif (exists $DC->{$c}) { # consonant: new aksara
432 20012 $a->vowel( $Inherent );
433 20012 push @aksaras, $a;
434 20012 $a = Lingua::Deva::Aksara->new( onset => [ $DC->{$c} ] );
435 }
436 elsif (exists $DF->{$c}) { # final: end of aksara
437 7004 $a->vowel( $Inherent );
438 7004 $a->final( $DF->{$c} );
439 7004 push @aksaras, $a;
440 7004 $state = 0;
441 }
442 elsif ($c =~ /$Avagraha/) { # Avagraha
443 0 $a->vowel( $Inherent );
444 0 push @aksaras, $a, $self->{avagraha};
445 0 $state = 0;
446 }
447 else { # other: invalid
448 9005 $a->vowel( $Inherent );
449 9005 ▶ if ($c !~ /\p{Space}/ and $self->{strict} and !exists $self->{allow}->{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}) 0 9005 9005
Condition50%3/6
not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}
!ll&&!rl&&r
900500
not $c =~ /\p{Space}/u and $self->{"strict"}
!ll&&!rl&&r
800510000
MC/DC0%0/3
not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}
not $c =~ /\p{Space}/u $self->{"strict"} not exists $self->{"allow"}{$c}
Truth table50%2/4
not(A) and B and not(C)
A= not $c =~ /\p{Space}/uB= $self->{"strict"}C= not exists $self->{"allow"}{$c}
ABCresult
0XX0
10X0
1100
1111
450 0 carp("Invalid character $c read");
451 }
452 9005 push @aksaras, $a, $c;
453 9005 $state = 0;
454 }
455 }
456 elsif ($state == 2) {
457 35013 ▶ if (exists $DC->{$c}) { # consonant: cluster
Branch: 3 branches
BranchTrueFalseTotal
if (exists $DC->{$c}) { } 28013 7000 35013
elsif (exists $DV->{$c}) { } 0 7000 7000
elsif ($c =~ /$Lingua::Deva::Avagraha/u) { } 0 7000 7000
458 28013 push @{ $a->onset() }, $DC->{$c};
459 28013 $state = 1;
460 }
461 elsif (exists $DV->{$c}) { # vowel: new vowel-initial aksara
462 0 push @aksaras, $a;
463 0 $a = Lingua::Deva::Aksara->new( vowel => $DV->{$c} );
464 0 $state = 3;
465 }
466 elsif ($c =~ /$Avagraha/) { # Avagraha
467 0 push @aksaras, $a, $self->{avagraha};
468 0 $state = 0;
469 }
470 else { # other: invalid
471 7000 ▶ if ($c !~ /\p{Space}/ and $self->{strict} and !exists $self->{allow}->{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}) 0 7000 7000
Condition33%2/6
not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}
!ll&&!rl&&r
700000
not $c =~ /\p{Space}/u and $self->{"strict"}
!ll&&!rl&&r
700000
MC/DC0%0/3
not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}
not $c =~ /\p{Space}/u $self->{"strict"} not exists $self->{"allow"}{$c}
Truth table25%1/4
not(A) and B and not(C)
A= not $c =~ /\p{Space}/uB= $self->{"strict"}C= not exists $self->{"allow"}{$c}
ABCresult
0XX0
10X0
1100
1111
472 0 carp("Invalid character $c read");
473 }
474 7000 push @aksaras, $a, $c;
475 7000 $state = 0;
476 }
477 }
478 elsif ($state == 3) { # final: end of aksara
479 74022 ▶ if (exists $DF->{$c}) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $DF->{$c}) { } 4000 70022 74022
elsif (exists $DC->{$c}) { } 52019 18003 70022
elsif (exists $DV->{$c}) { } 0 18003 18003
elsif ($c =~ /$Lingua::Deva::Avagraha/u) { } 0 18003 18003
480 4000 $a->final( $DF->{$c} );
481 4000 push @aksaras, $a;
482 4000 $state = 0;
483 }
484 elsif (exists $DC->{$c}) { # consonant: new aksara
485 52019 push @aksaras, $a;
486 52019 $a = Lingua::Deva::Aksara->new( onset => [ $DC->{$c} ] );
487 52019 $state = 1;
488 }
489 elsif (exists $DV->{$c}) { # vowel: new vowel-initial aksara
490 0 push @aksaras, $a;
491 0 $a = Lingua::Deva::Aksara->new( vowel => $DV->{$c} );
492 0 $state = 3;
493 }
494 elsif ($c =~ /$Avagraha/) { # Avagraha
495 0 push @aksaras, $a, $self->{avagraha};
496 0 $state = 0;
497 }
498 else { # other: invalid
499 18003 ▶ if ($c !~ /\p{Space}/ and $self->{strict} and !exists $self->{allow}->{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}) 0 18003 18003
Condition50%3/6
not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}
!ll&&!rl&&r
1800300
not $c =~ /\p{Space}/u and $self->{"strict"}
!ll&&!rl&&r
1700310000
MC/DC0%0/3
not $c =~ /\p{Space}/u and $self->{"strict"} and not exists $self->{"allow"}{$c}
not $c =~ /\p{Space}/u $self->{"strict"} not exists $self->{"allow"}{$c}
Truth table50%2/4
not(A) and B and not(C)
A= not $c =~ /\p{Space}/uB= $self->{"strict"}C= not exists $self->{"allow"}{$c}
ABCresult
0XX0
10X0
1100
1111
500 0 carp("Invalid character $c read");
501 }
502 18003 push @aksaras, $a, $c;
503 18003 $state = 0;
504 }
505 }
506 }
507  
508 # Finish aksara currently under construction
509 5006 given ($state) {
510 0 when (1) { $a->vowel( $Inherent ); continue }
511 5006 when ([1..3]) { push @aksaras, $a }
512 }
513  
514 5006 return \@aksaras;
515 }
516  
517 6 *d_to_aksara = \&d_to_aksaras; # alias
518  
519 =item to_deva()
520  
521 Converts a Latin string (or a reference to an array of
522 L<Aksaras|Lingua::Deva::Aksara>) into Devanagari and returns a Devanagari
523 string.
524  
525 say $d->to_deva('Kāmasūtra');
526  
527 # same as
528 my $a = $d->l_to_aksaras('Kāmasūtra');
529 say $d->to_deva($a);
530  
531 Aksaras are assumed to be well-formed.
532  
533 =cut
534  
535 6 ▶sub to_deva {
Subroutine to_deva: called CC=9 SCAR=22.1
Pod documented
536 6 my ($self, $input) = @_;
537  
538 # Input can be either a string (scalar) or an array reference
539 6 ▶ my $aksaras = ref($input) eq '' ? $self->l_to_aksaras($input) : $input;
Branch: 1 branch
BranchTrueFalseTotal
ref $input eq '' ? : 4 2 6
540  
541 6 my $s = '';
542 6 my ($C, $V, $D, $F) = ($self->{C}, $self->{V}, $self->{D}, $self->{F});
543  
544 6 for my $a (@$aksaras) {
545 48 ▶ if (ref($a) ne 'Lingua::Deva::Aksara') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a ne 'Lingua::Deva::Aksara') { } 13 35 48
546 13 ▶ $s .= $a eq $self->{avagraha} ? $Avagraha : $a;
Branch: 1 branch
BranchTrueFalseTotal
$a eq $self->{'avagraha'} ? : 0 13 13
547 }
548 else {
549 35 ▶ if (defined $a->{onset}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $a->{'onset'}) { } 30 5 35
elsif (defined $a->vowel) { } 5 0 5
550 30 $s .= join($Virama, map { $C->{$_} } @{ $a->onset() });
551 30 ▶ $s .= defined $a->vowel() ? $D->{$a->vowel()} : $Virama;
Branch: 1 branch
BranchTrueFalseTotal
defined $a->vowel ? : 30 0 30
552 }
553 elsif (defined $a->vowel()) {
554 5 $s .= $V->{$a->vowel()};
555 }
556 35 ▶ $s .= $F->{$a->final()} if defined $a->final();
Branch: 1 branch
BranchTrueFalseTotal
if defined $a->final 1 34 35
557 }
558 }
559  
560 6 return $s;
561 }
562  
563 =item to_latin()
564  
565 Converts a Devanagari string (or a reference to an array of
566 L<Aksaras|Lingua::Deva::Aksara>) into Latin transliteration and returns a
567 Latin string.
568  
569 Aksaras are assumed to be well-formed.
570  
571 =cut
572  
573 4 ▶sub to_latin {
Subroutine to_latin: called CC=7 SCAR=19.5
Pod documented
574 4 my ($self, $input) = @_;
575  
576 # Input can be either a string (scalar) or an array reference
577 4 ▶ my $aksaras = ref($input) eq '' ? $self->d_to_aksaras($input) : $input;
Branch: 1 branch
BranchTrueFalseTotal
ref $input eq '' ? : 3 1 4
578  
579 4 my $s = '';
580 4 for my $a (@$aksaras) {
581 33 ▶ if (ref($a) eq 'Lingua::Deva::Aksara') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq 'Lingua::Deva::Aksara') { } 24 9 33
582 24 ▶ $s .= join '', @{ $a->onset() } if defined $a->onset();
Branch: 1 branch
BranchTrueFalseTotal
if defined $a->onset 21 3 24
583 24 ▶ $s .= $a->vowel() if defined $a->vowel();
Branch: 1 branch
BranchTrueFalseTotal
if defined $a->vowel 24 0 24
584 24 ▶ $s .= $a->final() if defined $a->final();
Branch: 1 branch
BranchTrueFalseTotal
if defined $a->final 2 22 24
585 }
586 else {
587 9 $s .= $a;
588 }
589 }
590  
591 4 return $s;
592 }
593  
594 =back
595  
596 =cut
597  
598 6 1;
599 __END__
↑ Summary Deva/Aksara.pm »