Hyphen.pm

filter: Statementstmt 93.7% 151 / 161 Branchbran 76.1% 64 / 84 Conditioncond 94.1% 16 / 17 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 5 totaltotal 86.2% 250 / 290 SCAR 39.7
CC
49
Cov
88%
hyphenate
28.5
new
22.3
make_result_list
3.0
CRAP 53.0
« Hyphen/german.pm ↑ Summary Hyphen/utf8.pm »
1  
2 package TeX::Hyphen;
3  
4 =head1 NAME
5  
6 TeX::Hyphen -- hyphenate words using TeX's patterns
7  
8 =head1 SYNOPSIS
9  
10 use TeX::Hyphen;
11 my $hyp = new TeX::Hyphen 'file' => 'hyphen.tex',
12 'style' => 'czech', leftmin => 2,
13 rightmin => 2;
14  
15 my $hyp = new TeX::Hyphen 'file' => 'hyphen.tex',
16 'style' => 'utf8';
17  
18 # my $hyp = new TeX::Hyphen "hyphen.tex";
19  
20 my $word = "representation";
21 my @points = $hyp->hyphenate($word);
22 print $hyp->visualize($word), "\n";
23  
24 =head1 DESCRIPTION
25  
26 Constructor new() creates a new Hyphen object and loads the file with
27 patterns into memory. Then you can ask it for hyphenation of a word by
28 calling a method of this object. If no file is specified, the default
29 Donald E. Knuth's F<hyphen.tex>, that is included in this module, is
30 used instead.
31  
32 =head2 Arguments to constructor
33  
34 You can pass arguments to the new() call as hash, possible options are
35  
36 =over 4
37  
38 =item file
39  
40 Name of the file with the patters. It will be loaded and the resulting
41 object will be able to hyphenate according to patterns in that file.
42  
43 For convenience and backward compatibility, the file name can also be
44 specified as the first (odd) parameter to new().
45  
46 =item style
47  
48 Various languages use special shortcuts to specify the patterns.
49 Instead of doing the full TeX expansion, we use Perl code to parse the
50 patterns. The style option loads TeX::Hyphen::name_of_the_style module
51 and uses the parsing functions found in it.
52  
53 Currently, the default czech (which also works for English alright),
54 german, and utf8 are available. See the TeX::Hyphen::czech man page
55 for more information, especially if you want to support other
56 languages/styles.
57  
58 =item leftmin
59  
60 The minimum starting substring which will not be hyphenated. This
61 overrides the default specified in the style file.
62  
63 =item rightmin
64  
65 The minimum ending substring which will not be hyphenated. This
66 overrides the default specified in the style file.
67  
68 =back
69  
70 =head2 Methods that are supported
71  
72 Method hyphenate() returns list of places where the word can be
73 divided, so
74  
75 $hyp->visualize('representation')
76  
77 returns list (3, 5, 8, 10).
78  
79 Method visualize() can be used to show these points, so
80  
81 $hyp->visualize('representation')
82
83 should return C<rep-re-sen-ta-tion>, at least for English patterns.
84  
85 Variables I<$TeX::Hyphen::LEFTMIN> and I<$TeX::Hyphen::RIGHTMIN> can
86 be used to restrict minimal starting and ending substring where it is
87 not possible to hyphenate. They both default to 2 but should be
88 changed to match the paratemers used to generate the patterns.
89  
90 Variable I<$TeX::Hyphen::DEBUG> can be set to see some statistics and
91 processing.
92  
93 The file with hyphenation patterns may contain C<\'> and C<\v> accents,
94 used in the Czech (and other) languages.
95  
96 =cut
97  
98 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 2 ▶use vars qw( $VERSION $DEBUG $LEFTMIN $RIGHTMIN $errstr );
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 2 $VERSION = '1.16';
102 0 ▶sub Version () { $VERSION; }
Subroutine Version: not called CC=1 SCAR=6.9
Pod undocumented
103  
104 2 ▶$DEBUG ||= 0;
Condition50%1/2
$TeX::Hyphen::DEBUG ||= 0
l!l
02
MC/DC0%0/1
$TeX::Hyphen::DEBUG ||= 0
$TeX::Hyphen::DEBUG
Truth table50%1/2
A
A= $TeX::Hyphen::DEBUG
Aresult
11
00
105  
106 # To protect beginning and end of the word from hyphenation
107 2 $LEFTMIN = 2;
108 2 $RIGHTMIN = 2;
109  
110 2 my (@DATA, $DATA_LOADED);
111  
112 # #############################################################
113 # Constructor. Parameter specifies file with patterns.
114 # File is searched for \patterns{ ... } and \hyphenation{ ... }
115 # sections and these are used.
116 #
117 sub new {
118 8 ▶ my $class = shift;
Subroutine new: called CC=22 SCAR=31.0
Pod undocumented
119 8 my ($file, %opts);
120 8 ▶ if (scalar(@_) % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@_) % 2) { } 3 5 8
121 3 $file = shift;
122 3 %opts = @_;
123 } else {
124 5 %opts = @_;
125 5 $file = $opts{'file'};
126 }
127 8 local *FILE;
128 8 ▶ if (not defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $file) { } 5 3 8
129 5 ▶ if (not defined $DATA_LOADED) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $DATA_LOADED) 2 3 5
130 2 @DATA = <DATA>;
131 2 $DATA_LOADED = 1;
132 }
133 } else {
134 3 ▶ open FILE, $file or do {
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or do { $TeX::Hyphen::errstr = "Error opening file `${file}': $!"; return } 0 3 3
135 0 $errstr = "Error opening file `$file': $!";
136 0 return;
137 };
138 }
139 8 my $self = {};
140 8 bless $self, $class;
141  
142 8 local ($/) = "\n";
143 8 my ($tag, $value);
144 8 my $hyphen = {};
145 8 my $beginhyphen = {};
146 8 my $endhyphen = {};
147 8 my $bothhyphen = {};
148 8 my $exception = {};
149  
150 8 my ($process_patterns, $process_hyphenation);
151 8 my ($leftmin, $rightmin) = ($LEFTMIN, $RIGHTMIN);
152 8 ▶ if (not defined $opts{'style'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"style"}) 7 1 8
153 7 $opts{'style'} = 'czech'; # for backward compatibility
154 }
155 8 ▶ if (defined $opts{'style'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"style"}) 8 0 8
156 8 ▶ if ($opts{'style'} eq 'utf8') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"style"} eq "utf8") 1 7 8
157 1 binmode(FILE,':utf8');
158 }
159 2 ▶ eval qq!use ${class}::$opts{'style'}!;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
160 8 ▶ if (not $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 8 0 8
161 8 eval "\$process_patterns = \\&${class}::$opts{'style'}::process_patterns";
162 8 eval "\$process_hyphenation = \\&${class}::$opts{'style'}::process_hyphenation";
163 8 eval "\$leftmin = \$${class}::$opts{'style'}::LEFTMIN";
164 8 eval "\$rightmin = \$${class}::$opts{'style'}::RIGHTMIN";
165 } else {
166 0 $errstr = "Error loading style module $class::$opts{'style'}: $@";
167 0 return;
168 }
169 }
170 8 ▶ $leftmin = $opts{leftmin} if exists $opts{leftmin};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"leftmin"} 0 8 8
171 8 ▶ $rightmin = $opts{rightmin} if exists $opts{rightmin};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"rightmin"} 1 7 8
172  
173 8 my ($in_patterns, $in_hyphenation) = (0, 0);
174 8 my $i = 0;
175 8 ▶ while ((defined $file and defined($_ = <FILE>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and defined($_ = readline FILE) or not defined $file and defined($_ = $DATA[$i++])) 22347 8 22355
Condition100%9/9
defined $file and defined($_ = readline FILE) or not defined $file and defined($_ = $DATA[$i++])
l!l&&r!l&&!r
17223317
defined $file and defined($_ = readline FILE)
!ll&&!rl&&r
22335317
not defined $file and defined($_ = $DATA[$i++])
!ll&&!rl&&r
3522330
MC/DC50%2/4
defined $file and defined($_ = readline FILE) or not defined $file and defined($_ = $DATA[$i++])
defined $file defined($_ = readline FILE) not defined $file defined($_ = $DATA[$i++])
Truth table42%3/7
A and B or not(C) and D
A= defined $fileB= defined($_ = readline FILE)C= not defined $fileD= defined($_ = $DATA[$i++])
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
176 or (not defined $file and defined($_ = $DATA[$i++]))) {
177 22347 s/\%.*$//; # comment out
178 22347 chomp;
179 22347 ▶ if ($in_patterns) {
Branch: 4 branches
BranchTrueFalseTotal
if ($in_patterns) { } 22254 93 22347
elsif ($in_hyphenation) { } 75 18 93
elsif (/\\patterns{/) { } 8 10 18
elsif (/\\hyphenation{/) { } 5 5 10
180 22254 $in_patterns = $process_patterns->($_,
181 $bothhyphen, $beginhyphen,
182 $endhyphen, $hyphen);
183 } elsif ($in_hyphenation) {
184 75 $in_hyphenation = $process_hyphenation->($_, $exception);
185 } elsif (/\\patterns{/) { # find the \patterns section
186 8 $in_patterns = 1;
187 } elsif (/\\hyphenation{/) {
188 5 $in_hyphenation = 1;
189 }
190 }
191 8 ▶ close FILE if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 3 5 8
192 8 $self->{hyphen} = $hyphen;
193 8 $self->{begin} = $beginhyphen;
194 8 $self->{end} = $endhyphen;
195 8 $self->{both} = $bothhyphen;
196 8 $self->{exception} = $exception;
197 8 ▶ print STDERR 'Statistics for ', (defined $file ? $file : 'hyphen.tex'),
Branch: 2 branches
BranchTrueFalseTotal
if $TeX::Hyphen::DEBUG 0 8 8
defined $file ? : 0 0 0
198 ': all ' , scalar %$hyphen,
199 ' (', scalar keys %$hyphen,
200 '), exception ', scalar %$exception,
201 ' (', scalar keys %$exception,
202 "),\n\tbegin ", scalar %$beginhyphen,
203 ' (', scalar keys %$beginhyphen,
204 '), end ', scalar %$endhyphen,
205 ' (', scalar keys %$endhyphen,
206 '), both ', scalar %$bothhyphen,
207 ' (', scalar keys %$bothhyphen, ")\n" if $DEBUG;
208
209 8 $self->{exact} = { %$exception };
210 8 $self->{leftmin} = $leftmin;
211 8 $self->{rightmin} = $rightmin;
212 8 $self;
213 }
214  
215 # ############################################
216 # For given word finds places for hyphenation.
217 # Returns an array specifying the places.
218 #
219 sub hyphenate {
220 1390 ▶ my ($self, $word) = (shift, shift);
Subroutine hyphenate: called CC=25 SCAR=33.5
Pod undocumented
221  
222 1390 ▶ print STDERR "Hyphenate `$word'\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $TeX::Hyphen::DEBUG 0 1390 1390
223
224 1390 my $exact = $self->{exact};
225 1390 ▶ if (defined(my $res = $exact->{$word})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $res = $exact->{$word})) 3 1387 1390
226 3 ▶ print STDERR "Exact match $res\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $TeX::Hyphen::DEBUG 0 3 3
227 3 return $self->make_result_list($res);
228 }
229  
230 1387 my $hyphen = $self->{hyphen};
231 1387 my $beginhyphen = $self->{begin};
232 1387 my $endhyphen = $self->{end};
233 1387 my $bothhyphen = $self->{both};
234  
235 1387 my $totallength = length $word;
236 1387 my @result = (0) x ($totallength + 1);
237  
238 # walk the word
239 1387 my $rightstop = $totallength - $self->{rightmin};
240 1387 my $pos;
241 1387 ▶ for ($pos = 0; $pos <= $rightstop; $pos++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos <= $rightstop) 4471 1387 5858
242 # length of the rest of the word
243 4471 my $restlength = $totallength - $pos;
244 # length of a substring
245 4471 my $length;
246 4471 ▶ for ($length = 1; $length <= $restlength; $length++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= $restlength) 20257 4471 24728
247 20257 my $substr = substr $word, $pos, $length;
248 20257 my $value;
249 my $j;
250 20257 my $letter;
251 20257 ▶ if (defined($value = $hyphen->{$substr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($value = $hyphen->{$substr})) 2430 17827 20257
252 2430 $j = $pos;
253 2430 ▶ print STDERR "$j: $substr: $value\n" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $TeX::Hyphen::DEBUG > 2 0 2430 2430
254 2430 ▶ while ($value =~ /(.)/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /(.)/gs) 7052 2430 9482
255 7052 ▶ $result[$j] = $1 if ($1 > $result[$j]);
Branch: 1 branch
BranchTrueFalseTotal
if $1 > $result[$j] 2495 4557 7052
256 7052 $j++;
257 }
258 }
259 20257 ▶ if (($pos == 0) and
Branch: 1 branch
BranchTrueFalseTotal
if ($pos == 0 and defined($value = $beginhyphen->{$substr})) 236 20021 20257
Condition100%3/3
$pos == 0 and defined($value = $beginhyphen->{$substr})
!ll&&!rl&&r
147125309236
MC/DC100%2/2
$pos == 0 and defined($value = $beginhyphen->{$substr})
$pos == 0 defined($value = $beginhyphen->{$substr})
Truth table100%3/3
A and B
A= $pos == 0B= defined($value = $beginhyphen->{$substr})
ABresult
0X0
100
111
260 defined($value = $beginhyphen->{$substr})) {
261 236 $j = 0;
262 236 ▶ print STDERR "$j: .$substr: $value\n" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $TeX::Hyphen::DEBUG > 2 0 236 236
263 236 ▶ while ($value =~ /(.)/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /(.)/gs) 815 236 1051
264 815 ▶ $result[$j] = $1 if ($1 > $result[$j]);
Branch: 1 branch
BranchTrueFalseTotal
if $1 > $result[$j] 236 579 815
265 815 $j++;
266 }
267 }
268 20257 ▶ if (($restlength == $length) and
Branch: 1 branch
BranchTrueFalseTotal
if ($restlength == $length and defined($value = $endhyphen->{$substr})) 222 20035 20257
Condition100%3/3
$restlength == $length and defined($value = $endhyphen->{$substr})
!ll&&!rl&&r
157864249222
MC/DC100%2/2
$restlength == $length and defined($value = $endhyphen->{$substr})
$restlength == $length defined($value = $endhyphen->{$substr})
Truth table100%3/3
A and B
A= $restlength == $lengthB= defined($value = $endhyphen->{$substr})
ABresult
0X0
100
111
269 defined($value = $endhyphen->{$substr})) {
270 222 $j = $pos;
271 222 ▶ print STDERR "$j: $substr.: $value\n" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $TeX::Hyphen::DEBUG > 2 0 222 222
272 222 ▶ while ($value =~ /(.)/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /(.)/gs) 477 222 699
273 477 ▶ $result[$j] = $1 if ($1 > $result[$j]);
Branch: 1 branch
BranchTrueFalseTotal
if $1 > $result[$j] 177 300 477
274 477 $j++;
275 }
276 }
277 }
278 }
279 1387 my $value;
280 my $letter;
281 1387 ▶ if (defined($value = $bothhyphen->{$word})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($value = $bothhyphen->{$word})) 0 1387 1387
282 0 my $j = 0;
283 0 ▶ print STDERR "$j: .$word.: $value\n" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $TeX::Hyphen::DEBUG > 2 0 0 0
284 0 ▶ while ($value =~ /(.)/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /(.)/gs) 0 0 0
285 0 ▶ $result[$j] = $1 if ($1 > $result[$j]);
Branch: 1 branch
BranchTrueFalseTotal
if $1 > $result[$j] 0 0 0
286 0 $j++;
287 }
288 }
289  
290 1387 my $result = join '', @result;
291 ### substr($result, 0, $self->{leftmin} + 1) = '0' x ($self->{leftmin} + 1);
292 1387 substr($result, 0, $self->{leftmin}) = '0' x $self->{leftmin};
293 1387 substr($result, -$self->{rightmin}) = '0' x $self->{rightmin};
294  
295 1387 ▶ print STDERR "Result: $result\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $TeX::Hyphen::DEBUG 0 1387 1387
296 1387 return $self->make_result_list($result);
297 }
298  
299 # ####################
300 #
301 #
302 sub make_result_list {
303 1390 ▶ my ($self, $result) = @_;
Subroutine make_result_list: called CC=3 SCAR=11.0
Pod undocumented
304 1390 my @result = ();
305 1390 my $i = 0;
306 1390 ▶ while ($result =~ /(.)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result =~ /(.)/g) 7280 1390 8670
307 7280 ▶ push @result, $i if (int($1) % 2);
Branch: 1 branch
BranchTrueFalseTotal
if int($1) % 2 535 6745 7280
308 7280 $i++;
309 }
310 1390 @result;
311 }
312  
313 # #########################################
314 # For a word show the result of hyphenation
315 #
316 sub visualize {
317 17 ▶ my ($self, $word) = (shift, shift);
Subroutine visualize: called CC=2 SCAR=6.9
Pod undocumented
318 17 my $number = 0;
319 17 my $pos;
320 17 for $pos ($self->hyphenate($word)) {
321 29 substr($word, $pos + $number, 0) = "-";
322 29 $number++;
323 }
324 17 $word;
325 }
326  
327 =head1 VERSION
328  
329 1.16
330  
331 =head1 AVAILABLE FROM
332  
333 http://www.adelton.com/perl/TeX-Hyphen/
334  
335 =head1 AUTHOR
336  
337 (c) 1997--2015 Jan Pazdziora.
338  
339 All rights reserved. This package is free software; you can
340 redistribute it and/or modify it under the same terms as Perl itself.
341  
342 Contact the author at jpx dash perl at adelton dot com.
343  
344 =head1 SEE ALSO
345  
346 perl(1), TeX::Hyphen::czech.
347  
348 =cut
349  
350 2 1;
351  
352 __DATA__
« Hyphen/german.pm ↑ Summary Hyphen/utf8.pm »