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