File Coverage

blib/lib/Lingua/Deva/Aksara.pm
Criterion Covered Total %
statement 49 49 100.0
branch 28 32 87.5
condition 2 2 100.0
subroutine 11 11 100.0
pod 6 6 100.0
total 96 100 96.0


line stmt bran cond sub pod time code
1             package Lingua::Deva::Aksara;
2              
3 6     6   80 use v5.12.1;
  6         22  
  6         441  
4 6     6   41 use strict;
  6         13  
  6         225  
5 6     6   33 use warnings;
  6         10  
  6         190  
6 6     6   39 use utf8;
  6         9  
  6         53  
7              
8 6     6   4440 use Lingua::Deva::Maps qw( %Vowels %Consonants %Finals );
  6         21  
  6         5379  
9              
10             =encoding UTF-8
11              
12             =head1 NAME
13              
14             Lingua::Deva::Aksara - Object representation of a Devanagari "syllable"
15              
16             =head1 SYNOPSIS
17              
18             use v5.12.1;
19             use strict;
20             use charnames ':full';
21             use Lingua::Deva::Aksara;
22              
23             my $a = Lingua::Deva::Aksara->new(
24             onset => ['dh', 'r'],
25             vowel => 'au',
26             final => "h\N{COMBINING DOT BELOW}",
27             );
28             $a->vowel( 'ai' );
29             say 'valid' if $a->is_valid();
30             say @{ $a->get_rhyme() }; # prints 'aiḥ'
31              
32             =head1 DESCRIPTION
33              
34             I is the Sanskrit term for the basic unit above the character level in
35             the Devanagari script. A C object is a Perl
36             representation of such a unit.
37              
38             C objects serve as an intermediate format for the
39             conversion facilities in L. Onset, vowel, and final tokens are
40             stored in separate fields. I are in Latin script; if the Aksara in
41             question was created through the L
42             or L method of a C
43             object, then the tokens are in the transliteration format associated with that
44             object.
45              
46             =head2 Methods
47              
48             =over 4
49              
50             =item new()
51              
52             Constructor. Can take optional initial data as its argument.
53              
54             use Lingua::Deva::Aksara;
55             Lingua::Deva::Aksara->new( onset => ['gh', 'r'] );
56              
57             =cut
58              
59             sub new {
60 476125     476125 1 627471 my $class = shift;
61 476125         5899445 my $self = { @_ };
62 476125         1704064 return bless $self, $class;
63             }
64              
65             =item onset()
66              
67             Accessor method for the array of onset tokens of this Aksara.
68              
69             my $a = Lingua::Deva::Aksara->new();
70             $a->onset( ['d', 'r'] ); # sets onset tokens to ['d', 'r']
71             $a->onset(); # returns a reference to ['d', 'r']
72              
73             Returns undefined when there is no onset.
74              
75             =cut
76              
77             sub onset {
78 341117     341117 1 939590 my $self = shift;
79 341117 50       715883 $self->{onset} = shift if @_;
80 341117         5741132 return $self->{onset};
81             }
82              
83             =item vowel()
84              
85             Accessor method for the vowel token of this Aksara. Returns undefined when
86             there is no vowel.
87              
88             =cut
89              
90             sub vowel {
91 412222     412222 1 550936 my $self = shift;
92 412222 100       1324372 $self->{vowel} = shift if @_;
93 412222         954759 return $self->{vowel};
94             }
95              
96             =item final()
97              
98             Accessor method for the final token of this Aksara. Returns undefined when
99             there is no final.
100              
101             =cut
102              
103             sub final {
104 44075     44075 1 57196 my $self = shift;
105 44075 100       162845 $self->{final} = shift if @_;
106 44075         98134 return $self->{final};
107             }
108              
109             =item get_rhyme()
110              
111             Returns the rhyme of this Aksara. This is a reference to an array consisting
112             of vowel and final. Undefined if there is no rhyme.
113              
114             The Aksara is assumed to be well-formed.
115              
116             =cut
117              
118             sub get_rhyme {
119 231002     231002 1 802429 my $self = shift;
120 231002 100       475501 if ($self->{final}) { return [ $self->{vowel}, $self->{final} ] }
  22001         77050  
121 209001 100       407108 if ($self->{vowel}) { return [ $self->{vowel} ] }
  202000         639829  
122 7001         15024 return;
123             }
124              
125             =item is_valid()
126              
127             Checks the formal validity of this Aksara. This method first checks if the
128             Aksara conforms to the structure C<(C+(VF?)?)|(VF?)>, where the letters
129             represent onset consonants, vowel, and final. Then it checks whether the
130             onset, vowel, and final fields contain only appropriate tokens.
131              
132             In order to do validation against a different transliteration scheme than the
133             default one, a reference to a customized C instance can be
134             passed along.
135              
136             $d; # Lingua::Deva object with custom transliteration
137             say $a->is_valid($d);
138              
139             An Aksara constructed through L's public interface is already
140             well-formed (ie. in accordance with the particular transliteration used) and
141             no validity check is necessary.
142              
143             =cut
144              
145             sub is_valid {
146 119003     119003 1 2661209 my ($self, $deva) = @_;
147              
148 119003 100       33859099 my ($C, $V, $F) = ref($deva) eq 'Lingua::Deva'
149             ? ($deva->{C}, $deva->{V}, $deva->{F})
150             : (\%Consonants, \%Vowels, \%Finals);
151              
152             # Check Aksara structure
153 119003 100 100     176457 my $s = @{ $self->{onset} // [] } ? 'C' : '';
  119003         519702  
154 119003 100       275728 $s .= $self->{vowel} ? 'V' : '';
155 119003 100       248189 $s .= $self->{final} ? 'F' : '';
156 119003 50       335142 return 0 if $s =~ m/^(C?F|)$/;
157              
158             # After this point empty strings and arrays have been rejected
159              
160             # Check Aksara tokens
161 119003 100       267868 if (defined $self->{onset}) {
162 110003         109054 for my $o (@{ $self->{onset} }) {
  110003         230727  
163 138004 100       425228 return 0 if not defined $C->{$o};
164             }
165             }
166 119002 100       275198 if (defined $self->{vowel}) {
167 112002 50       306502 return 0 if not defined $V->{ $self->{vowel} };
168             }
169 119002 100       235756 if (defined $self->{final}) {
170 11002 50       24384 return 0 if not defined $F->{ $self->{final} };
171             }
172              
173 119002         301437 return 1;
174             }
175              
176             =back
177              
178             =cut
179              
180             1;