File Coverage

blib/lib/Lingua/LO/Romanize/Syllable.pm
Criterion Covered Total %
statement 15 69 21.7
branch 0 66 0.0
condition 0 15 0.0
subroutine 5 6 83.3
pod 1 1 100.0
total 21 157 13.3


line stmt bran cond sub pod time code
1             package Lingua::LO::Romanize::Syllable;
2              
3 1     1   8 use strict;
  1         1  
  1         38  
4 1     1   7 use utf8;
  1         2  
  1         6  
5              
6 1     1   23 use Moose;
  1         2  
  1         6  
7              
8             use constant{
9 1         1284 BGN_PCGN => {
10             #consonants
11             'ກ' => 'k',
12             'ຂ' => 'kh',
13             'ຄ' => 'kh',
14             'ງ' => 'ng',
15             'ຈ' => 'ch',
16             'ສ' => 's',
17             'ຊ' => 'x',
18             'ຕ' => 't',
19             'ຖ' => 'th',
20             'ທ' => 'th',
21             'ນ' => 'n',
22             'ໜ' => 'n',
23             'ປ' => 'p',
24             'ຜ' => 'ph',
25             'ຝ' => 'f',
26             'ພ' => 'ph',
27             'ຟ' => 'f',
28             'ມ' => 'm',
29             'ໝ' => 'm',
30             'ຢ' => 'y',
31             'ຣ' => 'r',
32             'ຣ໌' => 'r',
33             'ລ' => 'l',
34             'ຼ' => 'l',
35             'ຫ' => 'h',
36             'ຮ' => 'h',
37             #ຍ, ຽ, ດ, ບ, ວ, ອ special cases
38             #vowels
39             'ະ' => 'a',
40             'ັ' => 'a',
41             'າ' => 'a',
42             'ິ' => 'i',
43             'ີ' => 'i',
44             'ຶ' => 'u',
45             'ື' => 'u',
46             'ຸ' => 'ou',
47             'ູ' => 'ou',
48             'ເະ' => 'é',
49             'ເັ' => 'é',
50             'ເ' => 'é',
51             'ແະ' => 'è',
52             'ແັ' => 'è',
53             'ແ' => 'è',
54             'ໂະ' => 'ô',
55             'ົ' => 'ô',
56             'ໂ' => 'ô',
57             'ເາະ' => 'o',
58             'ັອ' => 'o',
59             'ໍ' => 'o',
60             'ອ' => 'o',
61             'ເັຽະ' => 'ia',
62             'ັຽ' => 'ia',
63             'ເັຽ' => 'ia',
64             'ຽ' => 'ia',
65             'ເັຍະ' => 'ia', #?
66             'ັຍ' => 'ia', #?
67             'ເັຍ' => 'ia', #?
68             'ຍ' => 'ia', #?
69             'ເຍ' => 'ia',
70             'ເຶອະ' => 'ua',
71             'ເຶອ' => 'ua',
72             'ເືອ' => 'ua',
73             'ເິະ' => 'eu',
74             'ເິ' => 'eu',
75             'ເີ' => 'eu',
76             'ເື' => 'eu',
77             'ໄ' => 'ai',
78             'ໃ' => 'ai',
79             'ເົາ' => 'ao',
80             'ຳ' => 'am', #not needed?
81             'ໍາ' => 'am', #not needed?
82             'ິວ' => 'iou',
83             'ີວ' => 'iou',
84             # special case: ົວະ, ັວ, ົວ, ວ
85             },
86 1     1   8246 };
  1         2  
87              
88             =encoding utf-8
89              
90             =head1 NAME
91              
92             Lingua::LO::Romanize::Syllable - Class for syllables, used by Lingua::LO::Romanize::Word.
93              
94             =head1 VERSION
95              
96             Version 0.10
97              
98             =cut
99              
100             our $VERSION = '0.10';
101              
102             has 'syllable_str' => (
103             is => 'ro',
104             isa => 'Str',
105             required => 1,
106             );
107              
108             =head1 SYNOPSIS
109              
110             L<Lingua::LO::Romanize::Syllable> is used by L<Lingua::LO::Romanize::Word> and L<Lingua::LO::Romanize> to syllables of words. It is recommended to use L<Lingua::LO::Romanize> instead of this class directly (even if it is possible).
111              
112             use Lingua::LO::Romanize::Syllable;
113              
114             my $foo = Lingua::LO::Romanize::Syllable->new(syllable_str => 'ລາວ');
115              
116             my $bar = $foo->romanize; # $bar will hold the string 'lao'
117             $bar = $foo->romanize; # $bar will hold the string 'lao'
118             $bar = $foo->syllable_str; # $bar will hold the string 'ລາວ'
119            
120             For more information, please see L<Lingua::LO::Romanize>
121              
122             =head1 METHODS
123              
124             =head2 new
125              
126             Creates a new object, syllable_str is required.
127              
128             =head2 romanize
129              
130             Romanize a syllable accourding to the BGN/PCGN standard.
131              
132             Please see L<Lingua::LO::Romanize> for more information.
133              
134             =head2 syllable_str
135              
136             Returns the original syllable in Lao characters.
137              
138             =head2 BGN_PCGN
139              
140             A constant hash reference for romanization mapping accourding to BGN/PCGN.
141              
142             =cut
143              
144             sub romanize {
145 0     0 1   my $self = shift;
146 0           my $syllable = $self->syllable_str;
147 0           my $romanized_str;
148            
149 0           $syllable =~ s/[່-໋]//;
150            
151 0 0         return '...' if $syllable =~ /^ຯ$/;
152            
153 0 0         if ($syllable =~ /^[໐-à»™]+$/) {
154 0           foreach (split //, $syllable) {
155 0 0         $romanized_str .= (ord($_) - 3792) if /^[໐-à»™]$/;
156             }
157 0           return $romanized_str;
158             }
159            
160 0           $syllable =~ s/^ຫ([ເ-ໄ]?[ນມ])/$1/;
161            
162 0 0         return $syllable
163             unless $syllable =~ /^[ເ-ໄ]?([ກຂຄງຈສຊຍຽດຕຖທນບປຜຝພຟມຢຣລຼວຫອຮໜໝ])/;
164            
165 0           my $consonant = $1;
166            
167             #ຍ, ຽ, ດ, ບ, ວ, ອ
168 0 0         if ($consonant =~ /^[ຍຽ]$/) {
    0          
    0          
    0          
    0          
    0          
169 0           $romanized_str = 'gn';
170             } elsif ($consonant =~ /^ດ$/) {
171 0           $romanized_str = 'd';
172             } elsif ($consonant =~ /^ບ$/) {
173 0           $romanized_str = 'b';
174             } elsif ($consonant =~ /^ວ$/) {
175 0           $romanized_str = 'v';
176             } elsif ($consonant =~ /^ອ$/) {
177 0           $romanized_str = '-';
178             } elsif (defined (BGN_PCGN->{$consonant})) {
179 0           $romanized_str = BGN_PCGN->{$consonant};
180             }
181            
182 0 0 0       if ($consonant =~ /^ຫ$/ && $syllable =~ /^[ເ-ໄ]?ຫ([ຍຣລຼວ])/) {
    0          
    0          
183 0           my $sec_consonant = $1;
184 0           $consonant .= $1;
185 0 0         if ($sec_consonant =~ /ຍ/) {
    0          
    0          
186 0           $romanized_str = 'gn';
187             } elsif ($sec_consonant =~ /ວ/) {
188 0           $romanized_str = 'v';
189             } elsif (defined (BGN_PCGN->{$sec_consonant})) {
190 0           $romanized_str = BGN_PCGN->{$sec_consonant};
191             }
192             } elsif ($syllable =~ /^[ເ-ໄ]?$consonant(ວ)./) {
193 0           $consonant .= $1;
194 0           $romanized_str .= 'o';
195             }
196             elsif ($syllable =~ /^[ເ-ໄ]?$consonant([ຣລຼ])/) { # ວ, ຣ, or ລ (also ຼ) can be used in combination with another consonant
197 0           my $sec_consonant = $1;
198 0           $consonant .= $sec_consonant;
199 0 0         if (defined (BGN_PCGN->{$sec_consonant})) {
200 0           $romanized_str .= BGN_PCGN->{$sec_consonant};
201             }
202             }
203            
204             #vowel
205 0           my $vowel = '';
206 0           my $final_consonant;
207 0 0         if ($syllable =~ /^([ເ-ໄ]?)$consonant/) {
208 0 0         $vowel .= $1 if $1;
209             }
210 0 0         if ($syllable =~ /^[ເ-ໄ]?$consonant([ະັາິີຶືຸູະັົອໍວຽຍຳ]*)/) {
211 0 0         $vowel .= $1 if $1;
212             }
213 0 0         if ($syllable =~ /^[ເ-ໄ]?$consonant(?:[ະັາິີຶືຸູະັົອໍວຽຍຳ]*)([ກງຍຽດນບມຣວ]|ຣ໌)?$/) {
214 0 0         $final_consonant = $1 if $1;
215             }
216            
217 0 0         return $romanized_str . 'am' if ($vowel =~ /^(?:ໍາ|ຳ)/);
218            
219 0 0 0       if (defined (BGN_PCGN->{$vowel})) {
    0 0        
    0 0        
      0        
220 0           $romanized_str .= BGN_PCGN->{$vowel};
221             } elsif ($vowel =~ /^ົວະ$/ || $vowel =~ /^ັວ$/ || $vowel =~ /^ົວ$/ || $vowel =~ /^ວ$/) {
222 0           $romanized_str .= 'oua';
223             } elsif ($vowel =~ s/([ອວຽຍ])$// && defined (BGN_PCGN->{$vowel})) {
224 0           $final_consonant = $1;
225 0           $romanized_str .= BGN_PCGN->{$vowel};
226             }
227            
228             # last character
229 0 0         if ($final_consonant) {
230 0 0         if ($final_consonant =~ /ວ/) {
    0          
    0          
    0          
    0          
231 0           $romanized_str .= 'o';
232             } elsif ($final_consonant =~ /ດ/) {
233 0           $romanized_str .= 't';
234             } elsif ($final_consonant =~ /ບ/) {
235 0           $romanized_str .= 'p';
236             } elsif ($final_consonant =~ /[ຍຽ]/) {
237 0           $romanized_str .= 'y';
238             } elsif (defined (BGN_PCGN->{$final_consonant})) {
239 0           $romanized_str .= BGN_PCGN->{$final_consonant};
240             }
241             }
242            
243 0           return $romanized_str;
244             }
245              
246             =head1 AUTHOR
247              
248             Joakim Lagerqvist, C<< <jokke at cpan.org> >>
249              
250             =head1 BUGS
251              
252             Please report any bugs or feature requests to C<bug-lingua-lo-romanize at rt.cpan.org>, or through
253             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Lingua-LO-Romanize>. I will be notified, and then you'll
254             automatically be notified of progress on your bug as I make changes.
255              
256             =head1 SEE ALSO
257              
258             L<Lingua::LO::Romanize>
259              
260             =head1 COPYRIGHT & LICENSE
261              
262             Copyright 2009 Joakim Lagerqvist, all rights reserved.
263              
264             This program is free software; you can redistribute it and/or modify it
265             under the same terms as Perl itself.
266              
267              
268             =cut
269              
270 1     1   7 no Moose;
  1         1  
  1         7  
271             __PACKAGE__->meta->make_immutable;
272              
273             1; # End of Lingua::LO::Romanize::Syllable