blib/lib/Search/Tools/Transliterate.pm

filter: Statementstmt 93.5% 73 / 78 Branchbran 69.0% 29 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod 100% 3 / 3 totaltotal 85.9% 116 / 135 CC 24 SCAR 18.8
CC
24
Cov
85%
convert
11.3
BUILD
7.8
_init_map
5.2
CRAP 6.6
« blib/lib/Search/Tools/Tokenizer.pm ↑ Summary blib/lib/Search/Tools/ArgNormalizer.pm »
1 package Search::Tools::Transliterate;
2 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 extends 'Search::Tools::Object';
4 2 ▶use Search::Tools::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Encoding::FixLatin qw( fix_latin );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶has 'ebit' => ( is => 'rw', default => sub {1} );
Subroutine __ANON__: not called CC=1 SCAR=1.2
11 2 has 'map' => ( is => 'ro' );
12  
13 2 our $VERSION = '1.007';
14  
15 =pod
16  
17 =head1 NAME
18  
19 Search::Tools::Transliterate - transliterations of UTF-8 chars
20  
21 =head1 SYNOPSIS
22  
23 my $tr = Search::Tools::Transliterate->new();
24 print $tr->convert( 'some string of utf8 chars' );
25
26 =head1 DESCRIPTION
27  
28 Search::Tools::Transliterate transliterates UTF-8 characters
29 to single-byte equivalents. It is based on the transmap project
30 by Markus Kuhn http://www.cl.cam.ac.uk/~mgk25/.
31  
32 B<NOTE:> All the I<is_*> encoding check methods that existed in this class prior
33 to version 0.05 were moved to Search::Tools::UTF8 and refactored as functions,
34 many using XS for speed improvements.
35  
36  
37 =head1 METHODS
38  
39 =head2 new
40  
41 Create new instance. Takes the following optional parameters:
42  
43 =over
44  
45 =item map
46  
47 Customize the character mapping. Should be a hashref. See map() method.
48  
49 =item ebit
50  
51 Allow convert() to use full native 8bit characters for transliterating,
52 rather than only 7bit ASCII. The default is true (1). Set to 0 to disable.
53 B<NOTE:> This must be set in new(). Changing via the accessor
54 after new() will have no effect on map().
55  
56 =back
57  
58 =head2 BUILD
59  
60 Called internally by new().
61  
62 =head2 map
63  
64 Access the transliteration character map. Example:
65  
66 use Search::Tools::Transliterate;
67 my $tr = Search::Tools::Transliterate->new;
68 $tr->map->{mychar} = 'my transliteration';
69 print $tr->convert('mychar'); # prints 'my transliteration'
70  
71 NOTE: The map() method is an accessor only. You can not pass in a new map.
72  
73  
74 =head2 convert( I<text> )
75  
76 Returns UTF-8 I<text> converted with all single bytes, transliterated according
77 to %Map. Will croak if I<text> is not valid UTF-8, so if in doubt, check first with
78 is_valid_utf8() in Search::Tools::UTF8.
79  
80 =head2 convert1252( I<text> )
81  
82 Returns UTF-8 I<text> converted to all single byte characters,
83 transliterated with convert() and the Windows 1252 characters in the range
84 B<0x80> and B<0x9f> inclusive.
85  
86 The 1252 codepoints are converted first to
87 their UTF-8 counterparts per
88 http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
89 using Encoding::FixLatin::fix_latin() and then
90 I<text> is run through convert().
91  
92 Note that I<text> is checked with the looks_like_cp1252() function from
93 Search::Tools::UTF8 before calling fix_latin().
94  
95 =head1 BUGS
96  
97 You might consider the whole attempt as a bug. It's really an attempt to
98 accomodate applications that don't support Unicode. Perhaps we shouldn't even
99 try. But for things like curly quotes and other 'smart' punctuation, it's often
100 helpful to render the UTF-8 character as B<something> rather than just letting
101 a character without a direct translation slip into the ether.
102  
103 That said, if a character has no mapping (and there are plenty that do not)
104 a single space will be used.
105  
106 =head1 AUTHOR
107  
108 Peter Karman C<< <karman@cpan.org> >>
109  
110 Originally based on the HTML::HiLiter regular expression building code,
111 by the same author, copyright 2004 by Cray Inc.
112  
113 Thanks to Atomic Learning C<www.atomiclearning.com>
114 for sponsoring the development of some of these modules.
115  
116 =head1 BUGS
117  
118 Please report any bugs or feature requests to C<bug-search-tools at rt.cpan.org>, or through
119 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Search-Tools>.
120 I will be notified, and then you'll
121 automatically be notified of progress on your bug as I make changes.
122  
123 =head1 SUPPORT
124  
125 You can find documentation for this module with the perldoc command.
126  
127 perldoc Search::Tools
128  
129  
130 You can also look for information at:
131  
132 =over 4
133  
134 =item * RT: CPAN's request tracker
135  
136 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Search-Tools>
137  
138 =item * AnnoCPAN: Annotated CPAN documentation
139  
140 L<http://annocpan.org/dist/Search-Tools>
141  
142 =item * CPAN Ratings
143  
144 L<http://cpanratings.perl.org/d/Search-Tools>
145  
146 =item * Search CPAN
147  
148 L<http://search.cpan.org/dist/Search-Tools/>
149  
150 =back
151  
152 =head1 COPYRIGHT
153  
154 Copyright 2006-2010 by Peter Karman.
155  
156 This package is free software; you can redistribute it and/or modify it under the
157 same terms as Perl itself.
158  
159 =head1 SEE ALSO
160  
161 Search::Tools::UTF8, Unicode::Map, Encode, Test::utf8, Encoding::FixLatin
162  
163 =cut
164  
165 # must memoize <DATA> the first time since if we call new()
166 # more than once, <DATA> has already been iterated over
167 # and _init_map() will end up returning empty hash.
168 2 my %MAP;
169  
170 4 ▶sub _init_map {
Subroutine _init_map: called CC=5 SCAR=16.5
171 4 my $self = shift;
172  
173 4 ▶ return {%MAP} if %MAP;
Branch: 1 branch
BranchTrueFalseTotal
if %MAP 2 2 4
174  
175 2 ▶ while (<DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 1680 2 1682
176 1680 chomp;
177 1680 ▶ next unless m/^<U/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^<U/ 0 1680 1680
178 1680 my ( $from, $to ) = (m/^(<U.+?>)\ (.+)$/);
179 1680 ▶ if ( !defined $to ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to) 0 1680 1680
180 0 warn "Undefined mapping for $_\n";
181 0 next;
182 }
183 1680 my @o = split( /;/, $to );
184 1680 $MAP{ _Utag_to_chr($from) } = _Utag_to_chr( $o[0] );
185 }
186  
187 2 return {%MAP};
188 }
189  
190 3360 ▶sub _Utag_to_chr {
Subroutine _Utag_to_chr: called CC=1 SCAR=0.0
191 3360 my $t = shift;
192  
193 # cruft
194 3360 $t =~ s/[<>"]+//g;
195  
196 3360 $t =~ s,U([0-9A-F]+),chr( hex($1) ),oge;
197 3360 return $t;
198 }
199  
200 4 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=20.5
Pod documented
201 4 my $self = shift;
202  
203 4 my $map = $self->_init_map;
204  
205 # add/override 8bit chars
206 4 ▶ if ( $self->ebit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ebit) 1 3 4
207 1 ▶ $self->debug and warn "ebit on\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "ebit on\n" 0 1 1
208 1 for ( 128 .. 255 ) {
209 128 my $c = chr($_);
210 128 ▶ $self->debug and warn "chr $_ -> $c\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "chr $_ -> $c\n" 0 128 128
211 128 $map->{$c} = $c;
212 }
213 }
214  
215 4 ▶ if ( $self->{map} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"map"}) 0 4 4
216 0 $map->{$_} = $self->{map}->{$_} for keys %{ $self->{map} };
217 }
218 4 $self->{map} = $map;
219 }
220  
221 # benchmark shows this is 244% faster than previous version.
222 13 ▶sub convert {
Subroutine convert: called CC=11 SCAR=24.2
Pod documented
223 13 my ( $self, $buf ) = @_;
224 13 my $newbuf = '';
225  
226 # don't bother unless we have non-ascii bytes
227 13 ▶ return $buf if is_ascii($buf);
Branch: 1 branch
BranchTrueFalseTotal
if is_ascii($buf) 1 12 13
228  
229 # make sure we've got valid UTF-8 to start with
230 12 ▶ unless ( is_valid_utf8($buf) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_valid_utf8($buf)) 1 11 12
231 1 my $badbyte = find_bad_utf8($buf);
232 1 croak "bad UTF-8 byte(s) at $badbyte [ " . dump($buf) . " ]";
233 }
234  
235 # an alternate algorithm. no idea if it is faster.
236 # it depends on Perl's utf8 char matching (.)
237 # which should work if locale is correct, afaik.
238 11 my $map = $self->map;
239  
240 11 ▶ $self->debug and warn "converting $buf\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "converting $buf\n" 0 11 11
241 11 ▶ while ( $buf =~ m/(.)/gso ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /(.)/sog) 351 11 362
242 351 my $char = $1;
243 351 ▶ $self->debug and warn "$char\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "$char\n" 0 351 351
244 351 ▶ if ( is_ascii($char) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_ascii($char)) { } 156 195 351
elsif (not exists $map->{$char}) { } 57 138 195
245 156 ▶ $self->debug and warn "$char is_ascii\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "$char is_ascii\n" 0 156 156
246 156 $newbuf .= $char;
247 }
248 elsif ( !exists $map->{$char} ) {
249 57 ▶ $self->debug and warn "$char not in map\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "$char not in map\n" 0 57 57
250 57 $newbuf .= ' ';
251 }
252 else {
253 138 ▶ $self->debug and warn "transliterate $char => $map->{$char}\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "transliterate $char => $map->{$char}\n" 0 138 138
254 138 $newbuf .= $map->{$char};
255 }
256  
257 }
258  
259 11 return $newbuf;
260 }
261  
262 2 ▶sub convert1252 {
Subroutine convert1252: called CC=4 SCAR=14.6
Pod documented
263 2 my ( $self, $buf ) = @_;
264  
265 # don't bother unless we have non-ascii bytes
266 2 ▶ return $buf if is_ascii($buf);
Branch: 1 branch
BranchTrueFalseTotal
if is_ascii($buf) 0 2 2
267  
268 2 ▶ $self->debug and warn "converting $buf\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "converting $buf\n" 0 2 2
269 2 ▶ my $newbuf = looks_like_cp1252($buf) ? fix_latin($buf) : $buf;
Branch: 1 branch
BranchTrueFalseTotal
looks_like_cp1252($buf) ? : 2 0 2
270 2 return $self->convert($newbuf);
271 }
272  
273 2 1;
274  
275 # map taken directly from
276 # http://www.cl.cam.ac.uk/~mgk25/download/transtab.tar.gz
277 # by Markus Kuhn
278  
279 __DATA__
« blib/lib/Search/Tools/Tokenizer.pm ↑ Summary blib/lib/Search/Tools/ArgNormalizer.pm »