blib/lib/Lingua/JA/Fold.pm

filter: Statementstmt 96.8% 152 / 157 Branchbran 80.7% 42 / 52 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 18 / 18 Podpod 45.4% 5 / 11 totaltotal 88.3% 219 / 248 CC 32 SCAR 16.3
CC
32
Cov
91%
fold
9.6
fold_3
7.1
fold_1
5.0
CRAP 5.1
↑ Summary
1 package Lingua::JA::Fold;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.08'; # 2008-03-19 (since 2003-03-26)
9  
10 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @ISA = qw(Exporter);
12 1 our @EXPORT = qw(
13 fold
14 );
15 1 our @EXPORT_OK = qw(
16 length_full length_half
17 tab2space kana_half2full
18 );
19  
20 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 NAME
24  
25 Lingua::JA::Fold - to fold a Japanese text.
26  
27 =head1 SYNOPSIS
28  
29 use utf8;
30 use Lingua::JA::Fold qw(fold tab2space kana_half2full);
31
32 my $text = 'アイウエオ 漢字';
33
34 # replace a [TAB] with four [SPACE]s.
35 $text = tab2space('text' => $text, 'space' => 4);
36 # convert a half-width 'Kana' character to a full-width one.
37 $text = kana_half2full($text);
38
39 # fold a text under full-width two characters par a line.
40 $text = fold('text' => $text, 'length' => 2);
41
42 # output the result
43 utf8::encode($text);
44 print $text;
45  
46 =head1 DESCRIPTION
47  
48 This module is used to fold a Japanese text and so on.
49  
50 The Japanese (the Chinese and the Korean would be the same) text has traditionally unique manner in representing. Basically those characters are used to be represented in two kind of size which is 'full-width' or 'half-width'. Width and Height of full-width characters are the same size (regular square). At the point, it is different from the alphabet characters which have normally variable (slim) width in representing. Roughly say, we call width of alphabet characters and Arabic numbers as a half, and do the width of other characters as a full. In a Japanese text which is mixed with alphabet and Arabic numbers, a character has a width, it would be full or half.
51  
52 For such reasons, to fold a Japanese text is rather complicate thing.
53  
54 =head1 FUNCTIONS
55  
56 =over
57  
58 =item fold('text' => $text, 'length' => $i [, 'mode' => $mode])
59  
60 Function. To fold a string within specified length of $i.
61  
62 The way in which to calculate length is differs by a mode.
63  
64 'full-width' : culculated for a full-width character.
65 'traditional': culculated for a full-width character; reflects traditional manner of composition.
66 (not given) : igore size difference between a full and a half.
67  
68 =cut
69  
70 8 ▶sub fold {
Subroutine fold: called CC=9 SCAR=22.6
Pod documented
71 8 my %param = @_;
72
73 # check parameters
74 8 ▶ unless ($param{'text'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($param{"text"}) 0 8 8
75 0 return undef;
76 }
77 8 ▶ if (not $param{'length'} or $param{'length'} =~ m/\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $param{"length"} or $param{"length"} =~ /\D/) 0 8 8
Condition33%1/3
not $param{"length"} or $param{"length"} =~ /\D/
l!l&&r!l&&!r
008
MC/DC0%0/2
not $param{"length"} or $param{"length"} =~ /\D/
not $param{"length"} $param{"length"} =~ /\D/
Truth table33%1/3
not(A) or B
A= not $param{"length"}B= $param{"length"} =~ /\D/
ABresult
1X1
011
000
78 0 croak "length must be given as an integer value of more than 1";
79 }
80
81 # UTF-8 flag on
82 8 utf8::decode( $param{'text'} );
83
84 # newline character unification
85 8 $param{'text'} =~ s/\x0D\x0A|\x0D|\x0A/\n/g;
86
87 # split a text to lines
88 8 my @line;
89 8 ▶ while ($param{'text'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"text"}) 70 5 75
90 70 ▶ if ($param{'text'} !~ m/\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $param{'text'} =~ /\n/) { } 3 67 70
91 # single line; end without newline
92 3 push @line, $param{'text'};
93 3 last;
94 }
95 else {
96 # single line; end with newline
97 # multi line; end with/without newline
98 67 $param{'text'} =~ s/^[^\n]*?\n//s;
99 67 push @line, $&;
100 }
101 }
102
103 # folding mode junction
104 8 ▶ if (not $param{'mode'}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $param{'mode'}) { } 1 7 8
elsif ($param{'mode'} eq 'full-width') { } 2 5 7
elsif ($param{'mode'} eq 'traditional') { } 5 0 5
105 1 &fold_1($param{'length'}, \@line);
106 }
107 elsif ($param{'mode'} eq 'full-width') {
108 2 &fold_2($param{'length'}, \@line);
109 }
110 elsif ($param{'mode'} eq 'traditional') {
111 5 &fold_3($param{'length'}, \@line);
112 }
113
114 8 return join '', @line;
115 }
116  
117 1 ▶sub fold_1 {
Subroutine fold_1: called CC=5 SCAR=16.1
Pod undocumented
118 1 my($length, $ref) = @_;
119
120 # fold each lines
121 1 foreach my $line ( @{$ref} ) {
122 12 my @folded;
123 12 ▶ while ($line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 47 0 47
124 47 ▶ if (length($line) > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line > $length) { } 35 12 47
125 35 my $newfold;
126 35 ($newfold, $line) = cutter_1($length, $line);
127 35 push(@folded, $newfold);
128 }
129 else {
130 12 last;
131 }
132 }
133 12 my $folded = join("\n", @folded);
134 12 ▶ if ($folded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($folded) 5 7 12
135 5 $line = "$folded\n$line";
136 }
137 }
138
139 1 return 1;
140 }
141 35 ▶sub cutter_1 {
Subroutine cutter_1: called CC=1 SCAR=0.0
Pod undocumented
142 35 my($length, $string) = @_;
143 35 my $folded = substr($string, 0, $length);
144 35 my $unfold = substr($string, $length);
145 35 return $folded, $unfold;
146 }
147  
148 2 ▶sub fold_2 {
Subroutine fold_2: called CC=5 SCAR=16.1
Pod undocumented
149 2 my($length, $ref) = @_;
150
151 # fold each lines
152 2 foreach my $line ( @{$ref} ) {
153 13 my @folded;
154 13 ▶ while ($line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 52 0 52
155 52 ▶ if (length_full($line) > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (&length_full($line) > $length) { } 39 13 52
156 39 my $newfold;
157 39 ($newfold, $line) = cutter_2($length, $line);
158 39 push(@folded, $newfold);
159 }
160 else {
161 13 last;
162 }
163 }
164 13 my $folded = join("\n", @folded);
165 13 ▶ if ($folded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($folded) 6 7 13
166 6 $line = "$folded\n$line";
167 }
168 }
169
170 2 return 1;
171 }
172 39 ▶sub cutter_2 {
Subroutine cutter_2: called CC=3 SCAR=11.0
Pod undocumented
173 39 my($length, $string) = @_;
174 39 my $chars = $length;
175 39 my $folded = substr($string, 0, $chars);
176 39 my $shortage = $length - length_full($folded);
177 39 ▶ while ($shortage != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shortage != 0) 35 28 63
178 35 ▶ if ($shortage < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shortage < 0) { } 11 24 35
179 11 $chars -= 1;
180 11 $folded = substr($string, 0, $chars);
181 11 last;
182 }
183 else {
184 24 $chars += int($shortage + 0.5);
185 24 $folded = substr($string, 0, $chars);
186 24 $shortage = $length - length_full($folded);
187 24 next;
188 }
189 }
190 39 my $unfold = substr($string, $chars);
191 39 return $folded, $unfold;
192 }
193  
194 =item The Japanese Traditional Manner of Composition
195  
196 This formal manner is another saying as the forbidden rule. The rule is: 1) a termination mark like Ten "," and Maru ".", 2) a closing mark -- brace or parenthesis or bracket -- like ")", "}", "]", ">" and etc., 3) a repeat mark, those should not be placed at the top of a line. If it would be occured, such mark should be moved to the next to the bottom of the previous line.
197  
198 Actually by this module what is detect as a forbidden mark are listed next:
199  
200 ’”、。〃々〉》」』】〕〟ゝゞヽヾ),.]}
201  
202 Note that all of these marks are full-width Japanese characters.
203  
204 =cut
205  
206 1 my $Forbidden = '’”、。〃々〉》」』】〕〟ゝゞヽヾ),.]}';
207 # my $Forbidden = '\x{2019}\x{201D}\x{3001}-\x{3003}\x{3005}\x{3009}\x{300B}\x{300D}\x{300F}\x{3011}\x{3015}\x{301F}\x{309D}\x{309E}\x{30FD}\x{30FE}\x{FF09}\x{FF0C}\x{FF0E}\x{FF3D}\x{FF5D}';
208  
209 5 ▶sub fold_3 {
Subroutine fold_3: called CC=7 SCAR=19.5
Pod undocumented
210 5 my($length, $ref) = @_;
211
212 # fold each lines
213 5 foreach my $line ( @{$ref} ) {
214 45 my @folded;
215 45 ▶ while ($line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 123 0 123
216 123 ▶ if (length_full($line) > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (&length_full($line) > $length) { } 78 45 123
217 78 my $newfold;
218 78 ($newfold, $line) = cutter_3($length, $line);
219 78 push(@folded, $newfold);
220 }
221 else {
222 45 last;
223 }
224 }
225 45 my $folded = join "\n", @folded;
226 45 ▶ if ($folded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($folded) 35 10 45
227 35 ▶ if ( length($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line) { } 35 0 35
228 35 ▶ if ($line eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "\n") { } 26 9 35
229 26 $line = "$folded$line";
230 }
231 else {
232 9 $line = "$folded\n$line";
233 }
234 }
235 else {
236 0 $line = $folded;
237 }
238 }
239 }
240
241 5 return 1;
242 }
243 78 ▶sub cutter_3 {
Subroutine cutter_3: called CC=5 SCAR=16.1
Pod undocumented
244 78 my($length, $string) = @_;
245
246 78 my $chars = $length;
247 78 my $folded = substr($string, 0, $chars);
248 78 my $shortage = $length - length_full($folded);
249 78 ▶ while ($shortage != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shortage != 0) 8 76 84
250 8 ▶ if ($shortage < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shortage < 0) { } 2 6 8
251 2 $chars -= 1;
252 2 $folded = substr($string, 0, $chars);
253 2 last;
254 }
255 else {
256 6 $chars += int($shortage + 0.5);
257 6 $folded = substr($string, 0, $chars);
258 6 $shortage = $length - length_full($folded);
259 6 next;
260 }
261 }
262 78 my $unfold = substr($string, $chars);
263
264 78 ▶ while ($unfold) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unfold) 134 0 134
265 134 my $char_top = substr($unfold, 0, 1);
266 134 ▶ if ($char_top =~ /[$Forbidden]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char_top =~ /[$Forbidden]/) { } 56 78 134
267 56 $folded .= $char_top;
268 56 $unfold = substr($unfold, 1);
269 56 next;
270 }
271 else {
272 78 last;
273 }
274 }
275
276 78 return $folded, $unfold;
277 }
278  
279 =item length_half($text)
280  
281 Function. Exportable. This is for counting length of a text for a half-width character.
282  
283 =cut
284  
285 1 ▶sub length_half ($) {
Subroutine length_half: called CC=1 SCAR=0.0
Pod documented
286 1 my $string = shift;
287
288 # remove all ASCII controls except for [SPACE]
289 1 $string =~ tr/\x00-\x1F\x7F//d;
290
291 # ascii: arabic numbers, alphabets, marks
292 1 my $ascii = $string =~ tr/\x20-\x7E//d;
293 # half-width characters in the Unicode compatibility area
294 1 my $halfwidth = $string =~ tr/\x{FF61}-\x{FF9F}\x{FFE0}-\x{FFE5}//d;
295 # the rest: full-width characters
296 1 my $rest = length($string);
297
298 1 return $ascii + $halfwidth + $rest * 2;
299 }
300  
301 =item length_full($text)
302  
303 Function. Exportable. This is for counting length of a text for a full-width character.
304  
305 =cut
306  
307 323 ▶sub length_full ($) {
Subroutine length_full: called CC=1 SCAR=0.0
Pod documented
308 323 my $string = shift;
309
310 # remove all ASCII controls except for [SPACE]
311 323 $string =~ tr/\x00-\x1F\x7F//d;
312
313 # ascii: arabic numbers, alphabets, marks
314 323 my $ascii = $string =~ tr/\x20-\x7E//d;
315 # half-width characters in the Unicode compatibility area
316 323 my $halfwidth = $string =~ tr/\x{FF61}-\x{FF9F}\x{FFE0}-\x{FFE5}//d;
317 # the rest: full-width characters
318 323 my $rest = length($string);
319
320 323 return ($ascii + $halfwidth) * 0.5 + $rest;
321 }
322  
323 # sub _length_full_fixed {}
324  
325 =item tab2space('text' => $text, 'space' => $i)
326  
327 Function. Exportable. To replace a [TAB] character in a text with given number of [SPACE]s.
328  
329 =cut
330  
331 1 ▶sub tab2space {
Subroutine tab2space: called CC=4 SCAR=15.8
Pod documented
332 1 my %param = @_;
333
334 # check parameters
335 1 ▶ unless ($param{'text'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($param{"text"}) 0 1 1
336 0 return undef;
337 }
338 1 ▶ if (not $param{'space'} or $param{'space'} =~ m/\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $param{"space"} or $param{"space"} =~ /\D/) 0 1 1
Condition33%1/3
not $param{"space"} or $param{"space"} =~ /\D/
l!l&&r!l&&!r
001
MC/DC0%0/2
not $param{"space"} or $param{"space"} =~ /\D/
not $param{"space"} $param{"space"} =~ /\D/
Truth table33%1/3
not(A) or B
A= not $param{"space"}B= $param{"space"} =~ /\D/
ABresult
1X1
011
000
339 0 croak "space must be given as an integer value of more than 1";
340 }
341
342 1 my $spaces = ' ';
343 1 $spaces x= $param{'space'};
344
345 # replacement
346 1 $param{'text'} =~ s/\t/$spaces/g;
347
348 1 return $param{'text'};
349 }
350  
351 =item kana_half2full($text)
352  
353 Function. Exportable. To convert a character in a text from half-width 'Kana' to full-width one.
354  
355 =cut
356  
357 1 ▶sub kana_half2full {
Subroutine kana_half2full: called CC=1 SCAR=0.0
Pod documented
358 1 my $text = shift;
359
360 1 $text = encode('iso-2022-jp', $text);
361 1 $text = decode('iso-2022-jp', $text);
362
363 1 return $text;
364 }
365  
366 ########################################################################
367 1 1;
368 __END__
↑ Summary