lib/Lingua/Translit.pm

filter: Statementstmt 97.4% 76 / 78 Branchbran 78.2% 36 / 46 Conditioncond 66.6% 12 / 18 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 85.9% 147 / 171 SCAR 35.3
CC
32
Cov
87%
translit_reverse
13.6
translit
12.4
new
9.3
CRAP 34.1
↑ Summary lib/Lingua/Translit/Tables.pm »
1 package Lingua::Translit;
2  
3 #
4 # Copyright (C) 2007-2008 ...
5 # Alex Linke <alinke@lingua-systems.com>
6 # Rona Linke <rlinke@lingua-systems.com>
7 # Copyright (C) 2009-2016 Lingua-Systems Software GmbH
8 # Copyright (C) 2016-2017 Netzum Sorglos, Lingua-Systems Software GmbH
9 # Copyright (C) 2017 Netzum Sorglos Software GmbH
10 #
11  
12 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 29 require 5.008;
16  
17 29 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 29 ▶use Encode qw/encode decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 29 ▶use Lingua::Translit::Tables;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 29 our $VERSION = '0.28';
23  
24 =pod
25  
26 =encoding utf8
27  
28 =head1 NAME
29  
30 Lingua::Translit - transliterates text between writing systems
31  
32 =head1 SYNOPSIS
33  
34 use Lingua::Translit;
35  
36 my $tr = new Lingua::Translit("ISO 843");
37  
38 my $text_tr = $tr->translit("character oriented string");
39  
40 if ($tr->can_reverse()) {
41 $text_tr = $tr->translit_reverse("character oriented string");
42 }
43  
44 =head1 DESCRIPTION
45  
46 Lingua::Translit can be used to convert text from one writing system to
47 another, based on national or international transliteration tables.
48 Where possible a reverse transliteration is supported.
49  
50 The term C<transliteration> describes the conversion of text from one
51 writing system or alphabet to another one.
52 The conversion is ideally unique, mapping one character to exactly one
53 character, so the original spelling can be reconstructed.
54 Practically this is not always the case and one single letter of the
55 original alphabet can be transcribed as two, three or even more letters.
56  
57 Furthermore there is more than one transliteration scheme for one writing
58 system.
59 Therefore it is an important and necessary information, which scheme will be
60 or has been used to transliterate a text, to work integrative and be able to
61 reconstruct the original data.
62  
63 Reconstruction is a problem though for non-unique transliterations, if no
64 language specific knowledge is available as the resulting clusters of
65 letters may be ambiguous.
66 For example, the Greek character "PSI" maps to "ps", but "ps" could also
67 result from the sequence "PI", "SIGMA" since "PI" maps to "p" and "SIGMA"
68 maps to s.
69 If a transliteration table leads to ambiguous conversions, the provided
70 table cannot be used reverse.
71  
72 Otherwise the table can be used in both directions, if appreciated.
73 So if ISO 9 is originally created to convert Cyrillic letters to
74 the Latin alphabet, the reverse transliteration will transform Latin
75 letters to Cyrillic.
76  
77 =head1 METHODS
78  
79 =head2 new(I<"name of table">)
80  
81 Initializes an object with the specific transliteration table, e.g. "ISO 9".
82  
83 =cut
84  
85 sub new {
86 40 ▶ my $class = shift();
Subroutine new: called CC=9 SCAR=22.3
Pod documented
87 40 my $name = shift();
88  
89 40 my $self;
90  
91 # Assure that a table name was set
92 40 ▶ croak("No transliteration name given.") unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 39 40
93  
94 # Stay compatible with programs that use Lingua::Translit < 0.05
95 39 ▶ if ( $name =~ /^DIN 5008$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^DIN 5008$/i) 1 38 39
96 1 $name = "Common DEU";
97 }
98  
99 39 my $table = Lingua::Translit::Tables::_get_table_reference($name);
100  
101 # Check that a table reference was assigned to the object
102 39 ▶ croak("No table found for $name.") unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 39 39
103  
104 # Assure the table's data is complete
105 39 ▶ croak("$name table: missing 'name'") unless defined $table->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table->{"name"} 0 39 39
106 39 ▶ croak("$name table: missing 'desc'") unless defined $table->{desc};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table->{"desc"} 0 39 39
107 39 ▶ croak("$name table: missing 'reverse'") unless defined $table->{reverse};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table->{"reverse"} 0 39 39
108 39 ▶ croak("$name table: missing 'rules'") unless defined $table->{rules};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table->{"rules"} 0 39 39
109  
110 # Copy over the table's data
111 39 $self->{name} = $table->{name};
112 39 $self->{desc} = $table->{desc};
113 39 $self->{rules} = $table->{rules};
114  
115 # Set a truth value of the transliteration's reversibility according to
116 # the natural language string in the original transliteration table
117 39 ▶ $self->{reverse} = ( $table->{reverse} =~ /^true$/i ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$table->{'reverse'} =~ /^true$/i ? : 10 29 39
118  
119 39 undef($table);
120  
121 39 return bless $self, $class;
122 }
123  
124 =head2 translit(I<"character oriented string">)
125  
126 Transliterates the given text according to the object's transliteration
127 table.
128 Returns the transliterated text.
129  
130 =cut
131  
132 sub translit {
133 153 ▶ my $self = shift();
Subroutine translit: called CC=12 SCAR=25.2
Pod documented
134 153 my $text = shift();
135  
136 # Return if no input was given
137 153 ▶ return unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 153 153
138  
139 153 my $utf8_flag_on = Encode::is_utf8($text);
140  
141 153 ▶ unless ($utf8_flag_on) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($utf8_flag_on) 59 94 153
142 59 $text = decode( "UTF-8", $text );
143 }
144  
145 153 foreach my $rule ( @{ $self->{rules} } ) {
146 14308 ▶ if ( defined $rule->{context} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->{'context'}) { } 493 13815 14308
147 493 my $c = $rule->{context};
148  
149 # single context rules
150 493 ▶ if ( defined $c->{before} && !defined $c->{after} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $c->{'before'} and not defined $c->{'after'}) { } 107 386 493
elsif (defined $c->{'after'} and not defined $c->{'before'}) { } 223 163 386
elsif (defined $c->{'before'} and defined $c->{'after'}) { } 163 0 163
Condition100%3/3
defined $c->{"before"} and not defined $c->{"after"}
!ll&&!rl&&r
223163107
MC/DC100%2/2
defined $c->{"before"} and not defined $c->{"after"}
defined $c->{"before"} not defined $c->{"after"}
Truth table100%3/3
A and not(B)
A= defined $c->{"before"}B= not defined $c->{"after"}
ABresult
0X0
100
111
151 107 $text =~ s/$rule->{from}(?=$c->{before})/$rule->{to}/g;
152 }
153 ▶ elsif ( defined $c->{after} && !defined $c->{before} ) {
Condition66%2/3
defined $c->{"after"} and not defined $c->{"before"}
!ll&&!rl&&r
0163223
MC/DC50%1/2
defined $c->{"after"} and not defined $c->{"before"}
defined $c->{"after"} not defined $c->{"before"}
Truth table66%2/3
A and not(B)
A= defined $c->{"after"}B= not defined $c->{"before"}
ABresult
0X0
100
111
154 223 $text =~ s/(?<=$c->{after})$rule->{from}/$rule->{to}/g;
155 }
156  
157 # double context rules: logical "inbetween"
158 ▶ elsif ( defined $c->{before} && defined $c->{after} ) {
Condition33%1/3
defined $c->{"before"} and defined $c->{"after"}
!ll&&!rl&&r
00163
MC/DC0%0/2
defined $c->{"before"} and defined $c->{"after"}
defined $c->{"before"} defined $c->{"after"}
Truth table33%1/3
A and B
A= defined $c->{"before"}B= defined $c->{"after"}
ABresult
0X0
100
111
159 163 $text =~ s/
160 (?<=$c->{after})$rule->{from}(?=$c->{before})
161 /$rule->{to}/gx;
162 }
163  
164 else {
165 0 croak("incomplete rule context");
166 }
167 }
168 else {
169 13815 $text =~ s/$rule->{from}/$rule->{to}/g;
170 }
171 }
172  
173 153 ▶ unless ($utf8_flag_on) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8_flag_on) { } 94 59 153
174 59 return encode( "UTF-8", $text );
175 }
176 else {
177 94 return $text;
178 }
179 }
180  
181 =head2 translit_reverse(I<"character oriented string">)
182  
183 Transliterates the given text according to the object's transliteration
184 table, but uses it the other way round. For example table ISO 9 is a
185 transliteration scheme for the conversion of Cyrillic letters to the Latin
186 alphabet. So if used reverse, Latin letters will be mapped to Cyrillic ones.
187  
188 Returns the transliterated text.
189  
190 =cut
191  
192 sub translit_reverse {
193 18 ▶ my $self = shift();
Subroutine translit_reverse: called CC=13 SCAR=26.1
Pod documented
194 18 my $text = shift();
195  
196 # Return if no input was given
197 18 ▶ return unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 18 18
198  
199 # Is this transliteration reversible?
200 18 ▶ croak("$self->{name} cannot be reversed") unless $self->{reverse};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"reverse"} 0 18 18
201  
202 18 my $utf8_flag_on = Encode::is_utf8($text);
203  
204 18 ▶ unless ($utf8_flag_on) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($utf8_flag_on) 15 3 18
205 15 $text = decode( "UTF-8", $text );
206 }
207  
208 18 foreach my $rule ( @{ $self->{rules} } ) {
209 1503 ▶ if ( defined $rule->{context} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->{'context'}) { } 178 1325 1503
210 178 my $c = $rule->{context};
211  
212 # single context rules
213 178 ▶ if ( defined $c->{before} && !defined $c->{after} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $c->{'before'} and not defined $c->{'after'}) { } 54 124 178
elsif (defined $c->{'after'} and not defined $c->{'before'}) { } 19 105 124
elsif (defined $c->{'before'} and defined $c->{'after'}) { } 105 0 105
Condition100%3/3
defined $c->{"before"} and not defined $c->{"after"}
!ll&&!rl&&r
1910554
MC/DC100%2/2
defined $c->{"before"} and not defined $c->{"after"}
defined $c->{"before"} not defined $c->{"after"}
Truth table100%3/3
A and not(B)
A= defined $c->{"before"}B= not defined $c->{"after"}
ABresult
0X0
100
111
214 54 $text =~ s/$rule->{to}(?=$c->{before})/$rule->{from}/g;
215 }
216 ▶ elsif ( defined $c->{after} && !defined $c->{before} ) {
Condition66%2/3
defined $c->{"after"} and not defined $c->{"before"}
!ll&&!rl&&r
010519
MC/DC50%1/2
defined $c->{"after"} and not defined $c->{"before"}
defined $c->{"after"} not defined $c->{"before"}
Truth table66%2/3
A and not(B)
A= defined $c->{"after"}B= not defined $c->{"before"}
ABresult
0X0
100
111
217 19 $text =~ s/(?<=$c->{after})$rule->{to}/$rule->{from}/g;
218 }
219  
220 # double context rules: logical "inbetween"
221 ▶ elsif ( defined $c->{before} && defined $c->{after} ) {
Condition33%1/3
defined $c->{"before"} and defined $c->{"after"}
!ll&&!rl&&r
00105
MC/DC0%0/2
defined $c->{"before"} and defined $c->{"after"}
defined $c->{"before"} defined $c->{"after"}
Truth table33%1/3
A and B
A= defined $c->{"before"}B= defined $c->{"after"}
ABresult
0X0
100
111
222 105 $text =~ s/
223 (?<=$c->{after})$rule->{to}(?=$c->{before})
224 /$rule->{from}/gx;
225 }
226  
227 else {
228 0 croak("incomplete rule context");
229 }
230 }
231 else {
232 1325 $text =~ s/$rule->{to}/$rule->{from}/g;
233 }
234 }
235  
236 18 ▶ unless ($utf8_flag_on) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8_flag_on) { } 3 15 18
237 15 return encode( "UTF-8", $text );
238 }
239 else {
240 3 return $text;
241 }
242 }
243  
244 =head2 can_reverse()
245  
246 Returns true (1), iff reverse transliteration is possible.
247 False (0) otherwise.
248  
249 =cut
250  
251 sub can_reverse {
252 22 ▶ return $_[0]->{reverse};
Subroutine can_reverse: called CC=1 SCAR=0.0
Pod documented
253 }
254  
255 =head2 name()
256  
257 Returns the name of the chosen transliteration table, e.g. "ISO 9".
258  
259 =cut
260  
261 sub name {
262 12 ▶ return $_[0]->{name};
Subroutine name: called CC=1 SCAR=0.0
Pod documented
263 }
264  
265 =head2 desc()
266  
267 Returns a description for the transliteration,
268 e.g. "ISO 9:1995, Cyrillic to Latin".
269  
270 =cut
271  
272 sub desc {
273 12 ▶ return $_[0]->{desc};
Subroutine desc: called CC=1 SCAR=0.0
Pod documented
274 }
275  
276 =head1 SUPPORTED TRANSLITERATIONS
277  
278 =over 4
279  
280 =item Cyrillic
281  
282 I<ALA-LC RUS>, not reversible, ALA-LC:1997, Cyrillic to Latin, Russian
283  
284 I<ISO 9>, reversible, ISO 9:1995, Cyrillic to Latin
285  
286 I<ISO/R 9>, reversible, ISO 9:1954, Cyrillic to Latin
287  
288 I<DIN 1460 RUS>, reversible, DIN 1460:1982, Cyrillic to Latin, Russian
289  
290 I<DIN 1460 UKR>, reversible, DIN 1460:1982, Cyrillic to Latin, Ukrainian
291  
292 I<DIN 1460 BUL>, reversible, DIN 1460:1982, Cyrillic to Latin, Bulgarian
293  
294 I<Streamlined System BUL>, not reversible, The Streamlined System: 2006,
295 Cyrillic to Latin, Bulgarian
296  
297 I<GOST 7.79 RUS>, reversible, GOST 7.79:2000 (table B), Cyrillic to Latin,
298 Russian
299  
300 I<GOST 7.79 RUS OLD>, not reversible, GOST 7.79:2000 (table B), Cyrillic to
301 Latin with support for Old Russian (pre 1918), Russian
302  
303 I<GOST 7.79 UKR>, reversible, GOST 7.79:2000 (table B), Cyrillic to Latin,
304 Ukrainian
305  
306 I<BGN/PCGN RUS Standard>, not reversible, BGN/PCGN:1947 (Standard Variant),
307 Cyrillic to Latin, Russian
308  
309 I<BGN/PCGN RUS Strict>, not reversible, BGN/PCGN:1947 (Strict Variant),
310 Cyrillic to Latin, Russian
311  
312 =item Greek
313  
314 I<ISO 843>, not reversible, ISO 843:1997, Greek to Latin
315  
316 I<DIN 31634>, not reversible, DIN 31634:1982, Greek to Latin
317  
318 I<Greeklish>, not reversible, Greeklish (Phonetic), Greek to Latin
319  
320 =item Latin
321  
322 I<Common CES>, not reversible, Czech without diacritics
323  
324 I<Common DEU>, not reversible, German without umlauts
325  
326 I<Common POL>, not reversible, Unaccented Polish
327  
328 I<Common RON>, not reversible, Romanian without diacritics as commonly used
329  
330 I<Common SLK>, not reversible, Slovak without diacritics
331  
332 I<Common SLV>, not reversible, Slovenian without diacritics
333  
334 I<ISO 8859-16 RON>, reversible, Romanian with appropriate diacritics
335  
336 =item Arabic
337  
338 I<Common ARA>, not reversible, Common Romanization of Arabic
339  
340 =item Sanskrit
341  
342 I<IAST Devanagari>, not reversible, IAST Romanization to Devanāgarī
343  
344 I<Devanagari IAST>, not reversible, Devanāgarī to IAST Romanization
345  
346 =back
347  
348 =head1 ADDING NEW TRANSLITERATIONS
349  
350 In case you want to add your own transliteration tables to
351 L<Lingua::Translit>, have a look at the developer documentation at
352 L<https://www.netzum-sorglos.de/software/lingua-translit/developer-documentation.html>.
353  
354 A template of a transliteration table is provided as well
355 (F<xml/template.xml>) so you can easily start developing.
356  
357  
358 =head1 RESTRICTIONS
359  
360 L<Lingua::Translit> is suited to handle B<Unicode> and utilizes comparisons
361 and regular expressions that rely on B<code points>.
362 Therefore, any input is supposed to be B<character oriented>
363 (C<use utf8;>, ...) instead of byte oriented.
364  
365 However, if your data is byte oriented, be sure to pass it
366 B<UTF-8 encoded> to translit() and/or translit_reverse() - it will be
367 converted internally.
368  
369 =head1 BUGS
370  
371 None known.
372  
373 Please report bugs using CPAN's request tracker at
374 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Lingua-Translit>.
375  
376 =head1 SEE ALSO
377  
378 L<Lingua::Translit::Tables>, L<Encode>, L<perlunicode>
379  
380 C<translit>'s manpage
381  
382 L<http://www.netzum-sorglos.de/software/lingua-translit/>
383  
384 =head1 CREDITS
385  
386 Thanks to Dr. Daniel Eiwen, Romanisches Seminar, Universitaet Koeln for his
387 help on Romanian transliteration.
388  
389 Thanks to Dmitry Smal and Rusar Publishing for contributing the "ALA-LC RUS"
390 transliteration table.
391  
392 Thanks to Ahmed Elsheshtawy for his help implementing the "Common ARA" Arabic
393 transliteration.
394  
395 Thanks to Dusan Vuckovic for contributing the "ISO/R 9" transliteration table.
396  
397 Thanks to Ștefan Suciu for contributing the "ISO 8859-16 RON" transliteration
398 table.
399  
400 Thanks to Philip Kime for contributing the "IAST Devanagari" and "Devanagari
401 IAST" transliteration tables.
402  
403 Thanks to Nikola Lečić for contributing the "BGN/PCGN RUS Standard" and
404 "BGN/PCGN RUS Strict" transliteration tables.
405  
406 =head1 AUTHORS
407  
408 Alex Linke <alinke@netzum-sorglos.de>
409  
410 Rona Linke <rlinke@netzum-sorglos.de>
411  
412 =head1 LICENSE AND COPYRIGHT
413  
414 Copyright (C) 2007-2008 Alex Linke and Rona Linke
415  
416 Copyright (C) 2009-2016 Lingua-Systems Software GmbH
417  
418 Copyright (C) 2016-2017 Netzum Sorglos, Lingua-Systems Software GmbH
419  
420 Copyright (C) 2017 Netzum Sorglos Software GmbH
421  
422 This module is free software; you can redistribute it and/or modify it under
423 the same terms as Perl itself.
424  
425 =cut
426  
427 29 1;
428  
429 # vim: set ft=perl sts=4 sw=4 ts=4 ai et:
↑ Summary lib/Lingua/Translit/Tables.pm »