blib/lib/Lingua/JA/Moji.pm

filter: Statementstmt 89.4% 687 / 768 Branchbran 76.7% 178 / 232 Conditioncond 70.8% 17 / 24 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 94.8% 92 / 97 Podpod 64.0% 57 / 89 totaltotal 84.7% 1036 / 1223 SCAR 52.1
CC
135
Cov
86%
split_match
72.0
kana2romaji
44.1
make_convertors
9.9
CRAP 183.6
↑ Summary
1 package Lingua::JA::Moji;
2  
3 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 require Exporter;
8 25 our @ISA = qw(Exporter);
9  
10 25 our $VERSION = '0.62';
11  
12 25 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use Convert::Moji qw/make_regex length_one unambiguous/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 25 ▶use JSON::Parse 'read_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 25 our @EXPORT_OK = qw/
17 InHankakuKatakana
18 InKana
19 InWideAscii
20 ascii2wide
21 bad_kanji
22 bracketed2kanji
23 braille2kana
24 circled2kana
25 circled2kanji
26 cleanup_kana
27 cyrillic2katakana
28 hangul2kana
29 hentai2kana
30 hentai2kanji
31 hira2kata
32 hw2katakana
33 is_hiragana
34 is_kana
35 is_katakana
36 is_romaji
37 is_romaji_semistrict
38 is_romaji_strict
39 is_small
40 is_voiced
41 join_sound_marks
42 kana2braille
43 kana2circled
44 kana2cyrillic
45 kana2hangul
46 kana2hentai
47 kana2hw
48 kana2katakana
49 kana2morse
50 kana2romaji
51 kana_consonant
52 kana_to_large
53 kanji2bracketed
54 kanji2circled
55 kanji2hentai
56 kata2hira
57 katakana2hw
58 katakana2square
59 katakana2syllable
60 morse2kana
61 new2old_kanji
62 nigori_first
63 normalize_romaji
64 old2new_kanji
65 romaji2hiragana
66 romaji2kana
67 romaji_styles
68 romaji_vowel_styles
69 smallize_kana
70 split_sound_marks
71 strip_sound_marks
72 square2katakana
73 wide2ascii
74 yurei_moji
75 /;
76  
77 25 our %EXPORT_TAGS = (
78 'all' => \@EXPORT_OK,
79 );
80  
81 # Load a specified convertor from the shared directory.
82  
83 sub load_convertor
84 {
85 21 ▶ my ($in, $out) = @_;
Subroutine load_convertor: called CC=3 SCAR=11.0
Pod undocumented
86 21 my $filename = $in."2".$out;
87 21 my $file = getdistfile ($filename);
88 21 ▶ if (! $file || ! -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file and -f $file) 1 20 21
Condition66%2/3
$file and -f $file
!ll&&!rl&&r
0120
MC/DC50%1/2
$file and -f $file
$file -f $file
Truth table66%2/3
A and B
A= $fileB= -f $file
ABresult
0X0
100
111
89 1 croak "Could not find distribution file '$filename'";
90 }
91 20 my $convertor = Convert::Moji::load_convertor ($file);
92 20 return $convertor;
93 }
94  
95 sub add_boilerplate
96 {
97 16 ▶ my ($code, $name) = @_;
Subroutine add_boilerplate: called CC=1 SCAR=0.0
Pod undocumented
98 16 $code =<<EOSUB;
99 sub convert_$name
100 {
101 my (\$conv,\$input,\$convert_type) = \@_;
102 $code
103 return \$input;
104 }
105 EOSUB
106 16 $code .= "\\\&".__PACKAGE__."::convert_$name;";
107 16 return $code;
108 }
109  
110 sub ambiguous_reverse
111 {
112 0 ▶ my ($table) = @_;
Subroutine ambiguous_reverse: not called CC=2 SCAR=17.9
Pod undocumented
113 0 my %inverted;
114 0 for (keys %$table) {
115 0 my $val = $table->{$_};
116 0 push @{$inverted{$val}}, $_;
117 }
118 0 return \%inverted;
119 }
120  
121 # Callback
122  
123 sub split_match
124 {
125 0 ▶ my ($conv, $input, $convert_type) = @_;
Subroutine split_match: not called CC=8 SCAR=42.8
Pod undocumented
126 0 ▶ if (!$convert_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($convert_type) 0 0 0
127 0 $convert_type = "all";
128 }
129 0 my @input = split '', $input;
130 0 my @output;
131 0 for (@input) {
132 0 my $in = $conv->{out2in}->{$_};
133 # No conversion defined.
134 0 ▶ if (! $in) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in) 0 0 0
135 0 push @output, $_;
136 0 next;
137 }
138 # Unambigous case
139 0 ▶ if (@{$in} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$in;} == 1) 0 0 0
140 0 push @output, $in->[0];
141 0 next;
142 }
143 0 ▶ if ($convert_type eq 'all') {
Branch: 3 branches
BranchTrueFalseTotal
if ($convert_type eq 'all') { } 0 0 0
elsif ($convert_type eq 'first') { } 0 0 0
elsif ($convert_type eq 'random') { } 0 0 0
144 0 push @output, $in;
145 }
146 elsif ($convert_type eq 'first') {
147 0 push @output, $in->[0];
148 }
149 elsif ($convert_type eq 'random') {
150 0 my $pos = int rand @$in;
151 0 push @output, $in->[$pos];
152 }
153 }
154 0 return \@output;
155 }
156  
157 sub make_convertors
158 {
159 8 ▶ my ($in, $out, $table) = @_;
Subroutine make_convertors: called CC=9 SCAR=22.9
Pod undocumented
160 8 my $conv = {};
161 8 ▶ if (!$table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 8 0 8
162 8 $table = load_convertor ($in, $out);
163 }
164 8 $conv->{in2out} = $table;
165 8 my @keys = keys %{$table};
166 8 my @values = values %{$table};
167 8 my $sub_in2out;
168 my $sub_out2in;
169 8 ▶ if (length_one (@keys)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length_one(@keys)) { } 8 0 8
170 8 my $lhs = join '', @keys;
171  
172 # Improvement: one way tr/// for the ambiguous case lhs/rhs only.
173  
174 8 ▶ if (length_one (@values) && unambiguous ($table)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length_one(@values) and unambiguous($table)) { } 2 6 8
Condition66%2/3
length_one(@values) and unambiguous($table)
!ll&&!rl&&r
602
MC/DC50%1/2
length_one(@values) and unambiguous($table)
length_one(@values) unambiguous($table)
Truth table66%2/3
A and B
A= length_one(@values)B= unambiguous($table)
ABresult
0X0
100
111
175 # can use tr///;
176 2 my $rhs = join '', @values;
177 2 $sub_in2out = "\$input =~ tr/$lhs/$rhs/;";
178 2 $sub_out2in = "\$input =~ tr/$rhs/$lhs/;";
179 }
180 else {
181 6 $sub_in2out = "\$input =~ s/([$lhs])/\$conv->{in2out}->{\$1}/eg;";
182 6 my $rhs = make_regex (@values);
183 6 ▶ if (unambiguous($conv->{in2out})) {
Branch: 1 branch
BranchTrueFalseTotal
if (unambiguous($conv->{'in2out'})) { } 6 0 6
184 6 my %out2in_table = reverse %{$conv->{in2out}};
185 6 $conv->{out2in} = \%out2in_table;
186 6 $sub_out2in = "\$input =~ s/($rhs)/\$conv->{out2in}->{\$1}/eg;";
187 }
188 else {
189 0 $conv->{out2in} = ambiguous_reverse ($conv->{in2out});
190 0 $sub_out2in = "\$input = \$conv->split_match (\$input, \$convert_type);";
191 }
192 }
193 }
194 else {
195 0 my $lhs = make_regex (@keys);
196 0 $sub_in2out = "\$input =~ s/($lhs)/\$conv->{in2out}->{\$1}/eg;";
197 0 my $rhs = make_regex (@values);
198 0 ▶ if (unambiguous($conv->{in2out})) {
Branch: 1 branch
BranchTrueFalseTotal
if (unambiguous($conv->{"in2out"})) 0 0 0
199 0 my %out2in_table = reverse %{$conv->{in2out}};
200 0 $conv->{out2in} = \%out2in_table;
201 0 $sub_out2in = " \$input =~ s/($rhs)/\$conv->{out2in}->{\$1}/eg;";
202 }
203 }
204 8 $sub_in2out = add_boilerplate ($sub_in2out, "${in}2$out");
205 8 ▶ my $sub1 = eval $sub_in2out;
Subroutine convert_katakana2braille: called CC=1 SCAR=0.0
Subroutine convert_katakana2circled: called CC=1 SCAR=0.0
Subroutine convert_katakana2hw_katakana: called CC=1 SCAR=0.0
Subroutine convert_katakana2morse: called CC=1 SCAR=0.0
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
206 8 $conv->{in2out_sub} = $sub1;
207 8 ▶ if ($sub_out2in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_out2in) 8 0 8
208 8 $sub_out2in = add_boilerplate ($sub_out2in, "${out}2$in");
209 8 ▶ my $sub2 = eval $sub_out2in;
Subroutine convert_braille2katakana: called CC=1 SCAR=0.0
Subroutine convert_circled2katakana: called CC=1 SCAR=0.0
Subroutine convert_hw_katakana2katakana: called CC=1 SCAR=0.0
Subroutine convert_morse2katakana: called CC=1 SCAR=0.0
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
210 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
211 0 print "Errors are ",$@,"\n";
212 0 print "\$sub2 = ",$sub2,"\n";
213 }
214 8 $conv->{out2in_sub} = $sub2;
215 }
216 8 bless $conv;
217 8 return $conv;
218 }
219  
220 sub convert
221 {
222 4 ▶ my ($conv, $input) = @_;
Subroutine convert: called CC=1 SCAR=0.0
Pod undocumented
223 4 return &{$conv->{in2out_sub}}($conv, $input);
224 }
225  
226 sub invert
227 {
228 17 ▶ my ($conv, $input, $convert_type) = @_;
Subroutine invert: called CC=1 SCAR=0.0
Pod undocumented
229 17 return &{$conv->{out2in_sub}}($conv, $input, $convert_type);
230 }
231  
232  
233 # Kana ordered by consonant. Adds two bogus gyous, a "q" gyou for
234 # small vowels and an "xy" gyou for youon (ya, yu, yo), to the usual
235 # ones.
236  
237 25 my @gyou = (
238 a => [qw/ア イ ウ エ オ/],
239 # Not a real gyou.
240 q => [qw/ã‚¡ ã‚£ ã‚¥ ã‚§ ã‚©/],
241 k => [qw/カ キ ク ケ コ/],
242 g => [qw/ガ ギ グ ゲ ゴ/],
243 s => [qw/サ シ ス セ ソ/],
244 z => [qw/ザ ジ ズ ゼ ゾ/],
245 t => [qw/タ チ ツ テ ト/],
246 d => [qw/ダ ヂ ヅ デ ド/],
247 n => [qw/ナ ニ ヌ ネ ノ/],
248 h => [qw/ハ ヒ フ ヘ ホ/],
249 b => [qw/バ ビ ブ ベ ボ/],
250 p => [qw/パ ピ プ ペ ポ/],
251 m => [qw/マ ミ ム メ モ/],
252 y => [qw/ヤ ユ ヨ/],
253 xy => [qw/ャ ュ ョ/],
254 r => [qw/ラ リ ル レ ロ/],
255 w => [qw/ワ ヰ ヱ ヲ/],
256 v => [qw/ヴ/],
257 );
258  
259 25 my %gyou = @gyou;
260  
261 # Kana => consonant mapping.
262  
263 25 my %siin;
264  
265 25 for my $consonant (keys %gyou) {
266 450 for my $kana (@{$gyou{$consonant}}) {
267 2025 ▶ if ($consonant eq 'a') {
Branch: 1 branch
BranchTrueFalseTotal
if ($consonant eq 'a') { } 125 1900 2025
268 125 $siin{$kana} = '';
269 }
270 else {
271 1900 $siin{$kana} = $consonant;
272 }
273 }
274 }
275  
276 # Vowel => kana mapping.
277  
278 25 my %dan = (a => [qw/ア カ ガ サ ザ タ ダ ナ ハ バ パ マ ヤ ラ ワ ャ ァ/],
279 i => [qw/イ キ ギ シ ジ チ ヂ ニ ヒ ビ ピ ミ リ ヰ ィ/],
280 u => [qw/ウ ク グ ス ズ ツ ヅ ヌ フ ブ プ ム ユ ル ュ ゥ ヴ/],
281 e => [qw/エ ケ ゲ セ ゼ テ デ ネ ヘ ベ ペ メ レ ヱ ェ/],
282 o => [qw/オ コ ゴ ソ ゾ ト ド ノ ホ ボ ポ モ ヨ ロ ヲ ョ ォ/]);
283  
284 # Kana => vowel mapping
285  
286 25 my %boin;
287  
288 # List of kana with a certain vowel.
289  
290 my %vowelclass;
291  
292 25 for my $vowel (keys %dan) {
293 125 my @kana_list = @{$dan{$vowel}};
294 125 for my $kana (@kana_list) {
295 2025 $boin{$kana} = $vowel;
296 }
297 125 $vowelclass{$vowel} = join '', @kana_list;
298 }
299  
300 # Kana gyou which can be preceded by a sokuon (small tsu).
301  
302 # Added d to the list for ウッド
303 # Added z for "badge" etc.
304 # Added g for ドッグ etc.
305  
306 #my @takes_sokuon_gyou = qw/s t k p d z g/;
307 #my @takes_sokuon = (map {@{$gyou{$_}}} @takes_sokuon_gyou);
308 #my $takes_sokuon = join '', @takes_sokuon;
309 #die @takes_sokuon;
310 25 my $takes_sokuon = 'サシスセソタチツテトカキクケコパピプペポダヂヅデドザジズゼゾガギグゲゴ';
311  
312 # Any kana except ã‚“
313  
314 #my@b4s;push@b4s,@{$gyou{$_}}for sort keys%gyou;@b4s=grep!/ã‚“/,@b4s;die join'',@b4s;
315  
316 25 my $before_sokuon = 'ヤユヨナニヌネノャュョガギグゲゴダヂヅデドカキクケコヴラリルレロワヰヱヲバビブベボタチツテトアイウエオパピプペポサシスセソァィゥェォマミムメモハヒフヘホザジズゼゾ';
317  
318 # N
319  
320 # Kana gyou which need an apostrophe when preceded by an "n" kana.
321  
322 25 my $need_apostrophe = join '', (map {@{$gyou{$_}}} qw/a y/);
323  
324 # Gyou which turn an "n" into an "m" in some kinds of romanization
325  
326 25 my $need_m = join '', (map {@{$gyou{$_}}} qw/p b m/);
327  
328 # YOUON
329  
330 # Small ya, yu, yo.
331  
332 25 my $youon = join '', (@{$gyou{xy}});
333 25 my %youon = qw/a ャ u ュ o ョ ou ョ/;
334  
335 # HEPBURN
336  
337 # Hepburn irregular romanization
338  
339 25 my %hepburn = qw/シ sh ツ ts チ ch ジ j ヅ z ヂ j フ f/;
340  
341 # Hepburn map from vowel to list of kana with that vowel.
342  
343 25 my %hep_vowel = (i => 'シチジヂ', u => 'ヅツフ');
344 25 my $hep_list = join '', keys %hepburn;
345  
346 # Hepburn irregular romanization of ッチ as "tch".
347  
348 25 my %hepburn_sokuon = qw/チ t/;
349 25 my $hep_sok_list = join '', keys %hepburn_sokuon;
350  
351 # Hepburn variants for the youon case.
352  
353 25 my %hepburn_youon = qw/シ sh チ ch ジ j ヂ j/;
354 25 my $is_hepburn_youon = join '', keys %hepburn_youon;
355  
356 # Kunrei variants for the youon case.
357  
358 25 my %kunrei_youon = qw/ヂ z/;
359 25 my $is_kunrei_youon = join '', keys %kunrei_youon;
360  
361 # Kunrei romanization
362  
363 25 my %kunrei = qw/ヅ z ヂ z/;
364  
365 25 my $kun_list = join '', keys %kunrei;
366  
367 # LONG VOWELS
368  
369 # Long vowels, another bugbear of Japanese romanization.
370  
371 25 my @aiueo = qw/a i u e o ou/;
372  
373 # Various ways to display the long vowels.
374  
375 25 my %chouonhyouki;
376 25 @{$chouonhyouki{circumflex}}{@aiueo} = qw/â î û ê ô ô/;
377 25 @{$chouonhyouki{macron}}{@aiueo} = qw/ā ii ū ē ō ō/;
378 25 @{$chouonhyouki{wapuro}}{@aiueo} = qw/aa ii uu ee oo ou/;
379 25 @{$chouonhyouki{passport}}{@aiueo} = qw/a i u e oh oh/;
380 25 @{$chouonhyouki{none}}{@aiueo} = qw/a ii u e o o/;
381  
382 25 my $vowel_re = qr/[aeiouâêîôûāēōū]/i;
383 25 my $no_u_vowel_re = qr/[aeioâêîôāēō]/i;
384 25 my $u_re = qr/[uūû]/i;
385  
386 sub kana2romaji
387 {
388 190 ▶ my ($input, $options) = @_;
Subroutine kana2romaji: called CC=43 SCAR=37.9
Pod documented
389 190 $input = kana2katakana ($input);
390 190 ▶ if (! $options) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options) 10 180 190
391 10 $options = {};
392 }
393 # Parse the options
394 190 my $kunrei;
395 my $hepburn;
396 190 my $passport;
397 190 my $common;
398 190 ▶ if ($options->{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"style"}) 29 161 190
399 29 my $style = $options->{style};
400 29 ▶ if ($style eq 'kunrei') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "kunrei") 2 27 29
401 2 $kunrei = 1;
402 }
403 29 ▶ if ($style eq 'passport') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "passport") 0 29 29
404 0 $passport = 1;
405 }
406 29 ▶ if ($style eq 'hepburn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "hepburn") 18 11 29
407 18 $hepburn = 1;
408 }
409 29 ▶ if ($style eq 'common') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "common") 8 21 29
410 8 $hepburn = 1;
411 8 $common = 1;
412 }
413 29 ▶ if (!$kunrei && !$passport && !$hepburn && $style ne "nihon" &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $kunrei || $passport || $hepburn and $style ne "nihon" and $style ne "nippon") 0 29 29
Condition66%8/12
not $kunrei || $passport || $hepburn and $style ne "nihon" and $style ne "nippon"
!ll&&!rl&&r
2900
not $kunrei || $passport || $hepburn and $style ne "nihon"
!ll&&!rl&&r
2810
$kunrei || $passport || $hepburn
l!l&&r!l&&!r
2261
$kunrei or $passport
l!l&&r!l&&!r
2027
MC/DC0%0/5
not $kunrei || $passport || $hepburn and $style ne "nihon" and $style ne "nippon"
$kunrei $passport $hepburn $style ne "nihon" $style ne "nippon"
Truth table50%3/6
not(A or B || C) and D and E
A= $kunreiB= $passportC= $hepburnD= $style ne "nihon"E= $style ne "nippon"
ABCDEresult
1XXXX0
01XXX0
001XX0
0000X0
000100
000111
414 $style ne 'nippon') {
415 0 croak "Unknown romanization style '$options->{style}'";
416 }
417 }
418 190 my $wapuro;
419 190 ▶ if ($options->{wapuro}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"wapuro"}) 158 32 190
420 158 $wapuro = 1;
421 }
422 190 my $wikipedia;
423 190 ▶ if ($options->{wikipedia}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"wikipedia"}) 1 189 190
424 1 $wikipedia = 1;
425 }
426 190 my $truck;
427 190 ▶ if ($options->{truck}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"truck"}) 1 189 190
428 1 $truck = 1;
429 }
430 190 my $use_m = 0;
431 190 ▶ if ($hepburn || $passport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hepburn or $passport) 26 164 190
Condition66%2/3
$hepburn or $passport
l!l&&r!l&&!r
260164
MC/DC50%1/2
$hepburn or $passport
$hepburn $passport
Truth table66%2/3
A or B
A= $hepburnB= $passport
ABresult
1X1
011
000
432 26 $use_m = 1;
433 }
434 190 ▶ if (defined $options->{use_m}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"use_m"}) 2 188 190
435 $use_m = $options->{use_m}
436 2 }
437 190 my $ve_type = 'circumflex'; # type of vowel extension to use.
438 190 ▶ if ($hepburn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hepburn) 26 164 190
439 26 $ve_type = 'macron';
440 }
441 190 ▶ if ($wapuro) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wapuro) 158 32 190
442 158 $ve_type = 'wapuro';
443 }
444 190 ▶ if ($passport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($passport) 0 190 190
445 0 $hepburn = 1;
446 0 $ve_type = 'passport';
447 0 $use_m = 1;
448 }
449 190 ▶ if ($options->{ve_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"ve_type"}) 10 180 190
450 10 $ve_type = $options->{ve_type};
451 }
452 190 ▶ if (! $chouonhyouki{$ve_type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chouonhyouki{$ve_type}) 0 190 190
453 0 print STDERR "Warning: unrecognized long vowel type '$ve_type'\n";
454 0 $ve_type = 'circumflex';
455 }
456 190 my $wo;
457 190 ▶ if ($options->{wo}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"wo"}) 1 189 190
458 1 $wo = 1;
459 }
460  
461 # Start of conversion
462  
463 # 撥音 (ん)
464 190 $input =~ s/ン(?=[$need_apostrophe])/n\'/g;
465 190 ▶ if ($use_m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_m) 24 166 190
466 24 $input =~ s/ン(?=[$need_m])/m/g;
467 }
468 190 $input =~ s/ン/n/g;
469 # 促音 (っ)
470 190 ▶ if ($hepburn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hepburn) 26 164 190
471 26 $input =~ s/ッ([$hep_sok_list])/$hepburn_sokuon{$1}$1/g;
472 }
473 190 $input =~ s/ッ([$takes_sokuon])/$siin{$1}$1/g;
474 190 ▶ if ($ve_type eq 'wapuro') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ve_type eq "wapuro") 167 23 190
475 167 $input =~ s/ー/-/g;
476 }
477 190 ▶ if ($ve_type eq 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ve_type eq "none") 1 189 190
478 1 $input =~ s/ー//g;
479 }
480 # Convert long vowels plus consonants into romaji
481 190 for my $vowel (@aiueo) {
482 1140 my $ve = $chouonhyouki{$ve_type}->{$vowel};
483 1140 my $vowelclass;
484 my $vowel_kana;
485 1140 ▶ if ($vowel eq 'ou') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vowel eq 'ou') { } 190 950 1140
486 190 $vowelclass = $vowelclass{o};
487 190 $vowel_kana = 'ウ';
488 }
489 else {
490 950 $vowelclass = $vowelclass{$vowel};
491 950 $vowel_kana = $dan{$vowel}->[0];
492 }
493 # 拗音 (きょ) + 長音 (ー)
494 1140 my $y = $youon{$vowel};
495 1140 ▶ if ($y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y) 760 380 1140
496 760 ▶ if ($hepburn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hepburn) 104 656 760
497 104 $input =~ s/([$is_hepburn_youon])${y}[ー$vowel_kana]/$hepburn_youon{$1}$ve/g;
498 }
499 760 ▶ if ($kunrei) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kunrei) 8 752 760
500 8 $input =~ s/([$is_kunrei_youon])${y}[ー$vowel_kana]/$kunrei_youon{$1}y$ve/g;
501 }
502 760 $input =~ s/([$vowelclass{i}])${y}[ー$vowel_kana]/$siin{$1}y$ve/g;
503 }
504 1140 ▶ if ($hepburn && $hep_vowel{$vowel}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hepburn and $hep_vowel{$vowel}) 52 1088 1140
Condition100%3/3
$hepburn and $hep_vowel{$vowel}
!ll&&!rl&&r
98410452
MC/DC100%2/2
$hepburn and $hep_vowel{$vowel}
$hepburn $hep_vowel{$vowel}
Truth table100%3/3
A and B
A= $hepburnB= $hep_vowel{$vowel}
ABresult
0X0
100
111
505 52 $input =~ s/([$hep_vowel{$vowel}])[ー$vowel_kana]/$hepburn{$1}$ve/g;
506 }
507 1140 $input =~ s/${vowel_kana}[ー$vowel_kana]/$ve/g;
508 1140 $input =~ s/([$vowelclass])[ー$vowel_kana]/$siin{$1}$ve/g;
509 }
510 # 拗音 (きょ)
511 190 ▶ if ($hepburn) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hepburn) { } 26 164 190
elsif ($kunrei) { } 2 162 164
512 26 $input =~ s/([$is_hepburn_youon])([$youon])/$hepburn_youon{$1}$boin{$2}/g;
513 }
514 elsif ($kunrei) {
515 2 $input =~ s/([$is_kunrei_youon])([$youon])/$kunrei_youon{$1}y$boin{$2}/g;
516 }
517 190 $input =~ s/([$vowelclass{i}])([$youon])/$siin{$1}y$boin{$2}/g;
518 # その他
519 190 ▶ if ($wo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wo) { } 1 189 190
520 1 $input =~ s/ヲ/wo/g;
521 1 $input =~ s/([アイウエオ])/$boin{$1}/g;
522 }
523 else {
524 189 $input =~ s/([アイウエオヲ])/$boin{$1}/g;
525 }
526 190 $input =~ s/([ァィゥェォ])/q$boin{$1}/g;
527 190 $input =~ s/ヮ/xwa/g;
528 190 ▶ if ($hepburn) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hepburn) { } 26 164 190
elsif ($kunrei) { } 2 162 164
529 26 $input =~ s/([$hep_list])/$hepburn{$1}$boin{$1}/g;
530 }
531 elsif ($kunrei) {
532 2 $input =~ s/([$kun_list])/$kunrei{$1}$boin{$1}/g;
533 }
534 190 $input =~ s/([カ-ヂツ-ヱヴ])/$siin{$1}$boin{$1}/g;
535 190 $input =~ s/q($vowel_re)/x$1/g;
536 190 ▶ if ($common) {
Branch: 1 branch
BranchTrueFalseTotal
if ($common) 8 182 190
537 # Convert kana + small vowel into thingumibob, if there is a
538 # consonant before.
539 8 $input =~ s/([^\Waiueo])$vowel_re[x]($vowel_re)/$1$2/;
540 # Convert u + small kana into w + vowel
541 8 $input =~ s/($vowel_re|\b)ux([iue])/$1w$2/i;
542 }
543 190 ▶ if ($wikipedia) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wikipedia) 1 189 190
544 1 $input =~ s/ii/Ä«/g;
545 }
546 190 ▶ if ($truck) {
Branch: 1 branch
BranchTrueFalseTotal
if ($truck) 1 189 190
547 1 $input =~ s/j($vowel_re)/jy$1/g;
548 }
549 190 return $input;
550 }
551  
552 sub romaji2hiragana
553 {
554 53 ▶ my ($input, $options) = @_;
Subroutine romaji2hiragana: called CC=2 SCAR=6.9
Pod documented
555 53 ▶ if (! $options) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options) 50 3 53
556 50 $options = {};
557 }
558 53 my $katakana = romaji2kana ($input, {wapuro => 1, %$options});
559 53 return kata2hira ($katakana);
560 }
561  
562 sub romaji_styles
563 {
564 1 ▶ my ($check) = @_;
Subroutine romaji_styles: called CC=4 SCAR=14.5
Pod documented
565 1 my @styles = (
566 {
567 abbrev => "hepburn",
568 full_name => "Hepburn",
569 }, {
570 abbrev => 'nihon',
571 full_name => 'Nihon-shiki',
572 }, {
573 abbrev => 'kunrei',
574 full_name => 'Kunrei-shiki',
575 }, {
576 abbrev => 'common',
577 full_name => 'common',
578 });
579 1 ▶ if (! defined ($check)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $check) { } 0 1 1
580 0 return (@styles);
581 }
582 else {
583 1 for my $style (@styles) {
584 2 ▶ if ($check eq $style->{abbrev}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check eq $style->{"abbrev"}) 1 1 2
585 1 return 1;
586 }
587 }
588 0 return;
589 }
590 }
591  
592 25 my %styles = (
593 macron => 1,
594 circumflex => 1,
595 wapuro => 1,
596 passport => 1,
597 none => 1,
598 );
599  
600 # Check whether this vowel style is allowed.
601  
602 sub romaji_vowel_styles
603 {
604 1 ▶ my ($check) = @_;
Subroutine romaji_vowel_styles: called CC=2 SCAR=7.6
Pod documented
605 1 ▶ if (! defined ($check)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $check) { } 1 0 1
606 1 return [keys %styles];
607 }
608 else {
609 0 return $styles{$check};
610 }
611 }
612  
613 25 my $romaji2katakana;
614 my $romaji_regex;
615  
616 25 my %longvowels;
617 25 @longvowels{qw/â î û ê ô/} = qw/aー iー uー eー oー/;
618 25 @longvowels{qw/ā ī ū ē ō/} = qw/aー iー uー eー oー/;
619 25 my $longvowels = join '|', sort {length($a)<=>length($b)} keys %longvowels;
620  
621 sub romaji2kana
622 {
623 363 ▶ my ($input, $options) = @_;
Subroutine romaji2kana: called CC=8 SCAR=20.8
Pod documented
624 363 ▶ if (! defined $romaji2katakana) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $romaji2katakana) 8 355 363
625 8 $romaji2katakana = load_convertor ('romaji', 'katakana');
626 8 $romaji_regex = make_regex (keys %$romaji2katakana);
627 }
628 # Set to true if we want long o to be オウ rather than オー
629 363 my $wapuro;
630 # Set to true if we want gumma to be ぐっま and onnna to be おんな.
631 my $ime;
632 363 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 269 94 363
633 269 $wapuro = $options->{wapuro};
634 269 $ime = $options->{ime};
635 }
636  
637 363 ▶ if (! defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input) 0 363 363
638 0 return;
639 }
640 363 $input = lc $input;
641 # Deal with long vowels
642 363 ▶ if ($wapuro) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wapuro) { } 269 94 363
643 269 $input =~ s/[âā]/aa/g;
644 269 $input =~ s/[îī]/ii/g;
645 269 $input =~ s/[êē]/ee/g;
646 269 $input =~ s/[ûū]/uu/g;
647 269 $input =~ s/[ôō]/ou/g;
648 }
649 else {
650 94 $input =~ s/($longvowels)/$longvowels{$1}/g;
651 # Doubled vowels to chouon
652 94 $input =~ s/([aiueo])\1/$1ー/g;
653 }
654 # Deal with double consonants
655 # danna -> だんな
656 363 ▶ if ($ime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ime) { } 3 360 363
657 # IME romaji rules:
658 # Allow double n for ã‚“:
659 # gunnma -> グンマ, dannna -> ダンナ
660 3 $input =~ s/n{1,2}(?=[nm][aiueo])/ン/g;
661 # Substitute sokuon for mm + vowel:
662 # gumma -> グッマ
663 3 $input =~ s/m(?=[nm][aiueo])/ッ/g;
664 }
665 else {
666 # Usual romaji rules: Don't allow double n for ã‚“, change
667 # gumma to グンマ.
668 360 $input =~ s/[nm](?=[nm][aiueo])/ン/g;
669 }
670 # shimbun -> しんぶん
671 363 $input =~ s/m(?=[pb]y?[aiueo])/ン/g;
672 # tcha -> っちゃ
673 363 $input =~ s/t(?=ch[aiueo])/ッ/g;
674 # ccha -> っちゃ
675 363 $input =~ s/c(?=ch[aiueo])/ッ/g;
676 # kkya -> っきゃ etc.
677 363 $input =~ s/([kstfhmrgzdbpjqvwy])(?=\1y?[aiueo])/ッ/g;
678 # kkya -> っきゃ etc.
679 363 $input =~ s/ttsu/ッツ/g;
680 # xtsu -> っ
681 363 $input =~ s/xtsu/ッ/g;
682 # ssha -> っしゃ
683 363 $input =~ s/([s])(?=\1h[aiueo])/ッ/g;
684 # Passport romaji,
685 # oh{consonant} -> oo
686 363 ▶ if (! $ime) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ime) 360 3 363
687 # IMEs do not recognize passport romaji.
688 360 ▶ if ($wapuro) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wapuro) { } 266 94 360
689 266 $input =~ s/oh(?=[ksthmrgzdbp])/オウ/g;
690 }
691 else {
692 94 $input =~ s/oh(?=[ksthmrgzdbp])/オー/g;
693 }
694 }
695 # All the special cases have been dealt with, now substitute all
696 # the kana.
697 363 $input =~ s/($romaji_regex)/$romaji2katakana->{$1}/g;
698 363 return $input;
699 }
700  
701 sub is_small
702 {
703 0 ▶ my ($input) = @_;
Subroutine is_small: not called CC=2 SCAR=17.9
Pod documented
704 0 ▶ if ($input =~/[ぁぃぅぇぉっゃゅょゕゖゎヮァィゥェォッャュョヵヶ]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /[\x{3041}\x{3043}\x{3045}\x{3047}\x{3049}\x{3063}\x{3083}\x{3085}\x{3087}\x{3095}\x{3096}\x{308e}\x{30ee}\x{30a1}\x{30a3}\x{30a5}\x{30a7}\x{30a9}\x{30c3}\x{30e3}\x{30e5}\x{30e7}\x{30f5}\x{30f6}]/) 0 0 0
705 0 return 1;
706 }
707 0 return undef;
708 }
709  
710  
711 sub is_voiced
712 {
713 2 ▶ my ($sound) = @_;
Subroutine is_voiced: called CC=4 SCAR=15.0
Pod documented
714 2 ▶ if (is_kana ($sound)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_kana($sound)) { } 2 0 2
elsif (my $romaji = is_romaji($sound)) { } 0 0 0
715 2 $sound = kana2romaji ($sound);
716 }
717 elsif (my $romaji = is_romaji ($sound)) {
718 # Normalize to nihon shiki so that we don't have to worry
719 # about ch, j, ts, etc. at the start of the sound.
720 0 $sound = $romaji;
721 }
722 2 ▶ if ($sound =~ /^[aiueogzbpmnry]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sound =~ /^[aiueogzbpmnry]/) { } 1 1 2
723 1 return 1;
724 }
725 else {
726 1 return undef;
727 }
728 }
729  
730 sub is_romaji
731 {
732 218 ▶ my ($romaji) = @_;
Subroutine is_romaji: called CC=4 SCAR=14.0
Pod documented
733 218 ▶ if (length ($romaji) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $romaji == 0) 0 218 218
734 0 return undef;
735 }
736 # Test that $romaji contains only characters which may be
737 # romanized Japanese.
738 218 ▶ if ($romaji =~ /[^\sa-zāīūēōâîûêô'-]|^-/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($romaji =~ /[^\sa-z\x{101}\x{12b}\x{16b}\x{113}\x{14d}\342\356\373\352\364'-]|^-/i) 4 214 218
739 4 return undef;
740 }
741 214 my $kana = romaji2kana ($romaji, {wapuro => 1});
742 214 ▶ if ($kana =~ /^[ã‚¢-ンッー\s]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kana =~ /^[\x{30a2}-\x{30f3}\x{30c3}\x{30fc}\s]+$/) 143 71 214
743 143 return kana2romaji ($kana, {wapuro => 1});
744 }
745 71 return undef;
746 }
747  
748  
749 sub is_romaji_semistrict
750 {
751 94 ▶ my ($romaji) = @_;
Subroutine is_romaji_semistrict: called CC=3 SCAR=11.0
Pod documented
752 94 my $canonical = is_romaji ($romaji);
753 94 ▶ if (! $canonical) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($canonical) 34 60 94
754 34 return undef;
755 }
756 60 ▶ if ($romaji =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($romaji =~ / # Don't allow small vowels, small tsu, or fya, # fye etc. (fy|l|x|v)y?($vowel_re|ts?u|wa|ka|ke) | # Don't allow hyi, hye, yi, ye. [zh]?y[ie\352\356\x{113}] | # Don't allow tye ty[e\352\x{113}] | # Don't allow wh-, kw-, gw-, dh-, etc. (wh|kw|gw|dh|thy)$vowel_re | # Don't allow "t'i" [dt]'(i|y?$u_re) | # Don't allow dwu, twu [dt](w$u_re) | hwy$u_re | # Don't allow "wi" or "we". w(i|e) | # Don't allow some non-Japanese double consonants. (?:rr|yy) | # Don't allow 'thi' thi /xi) 51 9 60
757 # Don't allow small vowels, small tsu, or fya,
758 # fye etc.
759 (fy|l|x|v)y?($vowel_re|ts?u|wa|ka|ke)
760 |
761 # Don't allow hyi, hye, yi, ye.
762 [zh]?y[ieêîē]
763 |
764 # Don't allow tye
765 ty[eêē]
766 |
767 # Don't allow wh-, kw-, gw-, dh-, etc.
768 (wh|kw|gw|dh|thy)$vowel_re
769 |
770 # Don't allow "t'i"
771 [dt]'(i|y?$u_re)
772 |
773 # Don't allow dwu, twu
774 [dt](w$u_re)
775 |
776 hwy$u_re
777 |
778 # Don't allow "wi" or "we".
779 w(i|e)
780 |
781 # Don't allow some non-Japanese double consonants.
782 (?:rr|yy)
783 |
784 # Don't allow 'thi'
785 thi
786 /ix) {
787 51 return undef;
788 }
789 9 return $canonical;
790 }
791  
792 sub is_romaji_strict
793 {
794 107 ▶ my ($romaji) = @_;
Subroutine is_romaji_strict: called CC=4 SCAR=13.9
Pod documented
795 107 my $canonical = is_romaji ($romaji);
796 107 ▶ if (! $canonical) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($canonical) 34 73 107
797 34 return undef;
798 }
799 73 my $kana = romaji2kana ($romaji);
800 73 ▶ if ($kana =~ m!
Branch: 1 branch
BranchTrueFalseTotal
if ($kana =~ / # Don't allow tanggono \x{30f3}\x{30c3} | # Don't allow "nmichi". ^\x{30f3} | # Don't allow ffun etc. ^\x{30c3} /x) 6 67 73
801 # Don't allow tanggono
802 ンッ
803 |
804 # Don't allow "nmichi".
805 ^ン
806 |
807 # Don't allow ffun etc.
808 ^ッ
809 !x) {
810 6 return undef;
811 }
812 67 ▶ if ($romaji =~ m!
Branch: 1 branch
BranchTrueFalseTotal
if ($romaji =~ m[ (fy|l|x|v)y?($vowel_re|ts?u|wa|ka|ke) | # Don't allow hyi, hye, yi, ye. [zh]?y[ie\352\356\x{113}] | # Don't allow tye ty[e\352\x{113}] | # Don't allow wh-, kw-, gw-, dh-, etc. (wh|kw|gw|dh|thy)$vowel_re | # Don't allow tsa, tsi, tse, tso, fa, fe, fi, fo. (ts|f)$no_u_vowel_re | # Don't allow "t'i" [dt]'(i|y?$u_re) | # Don't allow dwu, twu [dt](w$u_re) | hwy$u_re | # Don't allow "wi" or "we". w(i|e) | # Don't allow 'je', 'che', 'she' (?:[cs]h|j)e | # Don't allow some non-Japanese double consonants. (?:rr|yy) | # Don't allow 'thi'/'thu' th[iu] | # Don't allow 'johann' nn$ | # Don't allow 'ridzuan' etc. dz | # Qs are out. q | # Double ws, hs, etc. are out ww|hh|bb | # This is allowed by IMEs as "\343\201\241\343\202\203" etc. cy ]xi) 64 3 67
813 (fy|l|x|v)y?($vowel_re|ts?u|wa|ka|ke)
814 |
815 # Don't allow hyi, hye, yi, ye.
816 [zh]?y[ieêîē]
817 |
818 # Don't allow tye
819 ty[eêē]
820 |
821 # Don't allow wh-, kw-, gw-, dh-, etc.
822 (wh|kw|gw|dh|thy)$vowel_re
823 |
824 # Don't allow tsa, tsi, tse, tso, fa, fe, fi, fo.
825 (ts|f)$no_u_vowel_re
826 |
827 # Don't allow "t'i"
828 [dt]'(i|y?$u_re)
829 |
830 # Don't allow dwu, twu
831 [dt](w$u_re)
832 |
833 hwy$u_re
834 |
835 # Don't allow "wi" or "we".
836 w(i|e)
837 |
838 # Don't allow 'je', 'che', 'she'
839 (?:[cs]h|j)e
840 |
841 # Don't allow some non-Japanese double consonants.
842 (?:rr|yy)
843 |
844 # Don't allow 'thi'/'thu'
845 th[iu]
846 |
847 # Don't allow 'johann'
848 nn$
849 |
850 # Don't allow 'ridzuan' etc.
851 dz
852 |
853 # Qs are out.
854 q
855 |
856 # Double ws, hs, etc. are out
857 ww|hh|bb
858 |
859 # This is allowed by IMEs as "ちゃ" etc.
860 cy
861 !ix) {
862 64 return undef;
863 }
864 3 return $canonical;
865 }
866  
867 sub hira2kata
868 {
869 208 ▶ my (@input) = @_;
Subroutine hira2kata: called CC=5 SCAR=17.5
Pod documented
870 208 ▶ if (!@input) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@input) 0 208 208
871 0 return;
872 }
873 208 for (@input) {
874 208 ▶ if ($_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_) 208 0 208
875 208 tr/ぁ-んゔ/ァ-ンヴ/;
876 }
877 }
878 208 ▶ return wantarray ? @input : "@input";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 208 208
879 }
880  
881 sub kata2hira
882 {
883 57 ▶ my (@input) = @_;
Subroutine kata2hira: called CC=3 SCAR=11.1
Pod documented
884 57 for (@input) {tr/ァ-ンヴ/ぁ-んゔ/}
885 57 ▶ return wantarray ? @input : "@input";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 57 57
886 }
887  
888 # Make the list of dakuon stuff.
889  
890 sub make_dak_list
891 {
892 0 ▶ my @dak_list;
Subroutine make_dak_list: not called CC=2 SCAR=17.9
Pod undocumented
893 0 for (@_) {
894 0 push @dak_list, @{$gyou{$_}};
895 0 push @dak_list, hira2kata (@{$gyou{$_}});
896 }
897 0 return @dak_list;
898 }
899  
900 sub load_kana2hw2
901 {
902 3 ▶ my $conv = Convert::Moji->new (
Subroutine load_kana2hw2: called CC=1 SCAR=0.0
Pod undocumented
903 ["oneway", "tr", "あ-ん", "ア-ン"],
904 ["file",
905 getdistfile ("katakana2hw_katakana")]
906 );
907 3 return $conv;
908 }
909  
910 25 my $kata2hw;
911  
912 sub make_kata2hw
913 {
914 1 ▶ if (!$kata2hw) {
Subroutine make_kata2hw: called CC=2 SCAR=9.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (not $kata2hw) 0 1 1
915 0 $kata2hw = make_convertors ('katakana','hw_katakana');
916 }
917 }
918  
919 my $kana2hw;
920  
921 sub kana2hw
922 {
923 3 ▶ my ($input) = @_;
Subroutine kana2hw: called CC=2 SCAR=7.0
Pod documented
924 3 ▶ if (! $kana2hw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kana2hw) 3 0 3
925 3 $kana2hw = load_kana2hw2 ();
926 }
927 3 return $kana2hw->convert ($input);
928 }
929  
930 sub katakana2hw
931 {
932 1 ▶ my ($input) = @_;
Subroutine katakana2hw: called CC=1 SCAR=0.0
Pod documented
933 1 make_kata2hw ();
934 1 return $kata2hw->convert ($input);
935 }
936  
937 sub hw2katakana
938 {
939 8 ▶ my ($input) = @_;
Subroutine hw2katakana: called CC=2 SCAR=6.9
Pod documented
940 8 ▶ if (!$kata2hw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kata2hw) 5 3 8
941 5 $kata2hw = make_convertors ('katakana','hw_katakana');
942 }
943 8 return $kata2hw->invert ($input);
944 }
945  
946 sub InHankakuKatakana
947 {
948 39 ▶ return <<'END';
Subroutine InHankakuKatakana: called CC=1 SCAR=0.0
Pod documented
949 +utf8::Katakana
950 &utf8::InHalfwidthAndFullwidthForms
951 END
952 }
953  
954 # The two lists in wide2ascii and ascii2wide have exactly the same
955 # length.
956 #
957 # The warnings produced by Perl versions later than 22 are bugs in
958 # Perl:
959 #
960 # https://rt.perl.org/Public/Bug/Display.html?id=125493
961 #
962 # To save problems for users, switch off warnings in these routines.
963 #
964 # I have no idea what command to use to switch off just the
965 # "Replacement list is longer than search list" warning and leave the
966 # others intact.
967  
968 25 ▶no warnings 'misc';
Subroutine BEGIN: called CC=1 SCAR=0.0
969  
970 sub wide2ascii
971 {
972 4 ▶ my ($input) = @_;
Subroutine wide2ascii: called CC=1 SCAR=0.0
Pod documented
973 4 $input =~ tr/\x{3000}\x{FF01}-\x{FF5E}/ -~/;
974 4 return $input;
975 }
976  
977 sub ascii2wide
978 {
979 2 ▶ my ($input) = @_;
Subroutine ascii2wide: called CC=1 SCAR=0.0
Pod documented
980 2 $input =~ tr/ -~/\x{3000}\x{FF01}-\x{FF5E}/;
981 2 return $input;
982 }
983  
984 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
985  
986 sub InWideAscii
987 {
988 1 ▶ return <<'END';
Subroutine InWideAscii: called CC=1 SCAR=0.0
Pod documented
989 FF01 FF5E
990 3000
991 END
992 }
993  
994 25 my $kana2morse;
995  
996 sub load_kana2morse
997 {
998 2 ▶ if (!$kana2morse) {
Subroutine load_kana2morse: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (not $kana2morse) 1 1 2
999 1 $kana2morse = make_convertors ('katakana', 'morse');
1000 }
1001 }
1002  
1003 sub kana2morse
1004 {
1005 1 ▶ my ($input) = @_;
Subroutine kana2morse: called CC=1 SCAR=0.0
Pod documented
1006 1 load_kana2morse;
1007 1 $input = hira2kata ($input);
1008 1 $input =~ tr/ァィゥェォャュョッ/アイウエオヤユヨツ/;
1009 1 $input = split_sound_marks ($input);
1010 1 $input = join ' ', (split '', $input);
1011 1 $input = $kana2morse->convert ($input);
1012 1 return $input;
1013 }
1014  
1015  
1016 sub getdistfile
1017 {
1018 31 ▶ my ($filename) = @_;
Subroutine getdistfile: called CC=1 SCAR=0.0
Pod undocumented
1019 31 my $dir = __FILE__;
1020 31 $dir =~ s!\.pm$!/!;
1021 31 my $file = "$dir$filename.txt";
1022 31 return $file;
1023 }
1024  
1025 sub morse2kana
1026 {
1027 1 ▶ my ($input) = @_;
Subroutine morse2kana: called CC=2 SCAR=6.9
Pod documented
1028 1 load_kana2morse;
1029 1 my @input = split ' ',$input;
1030 1 for (@input) {
1031 7 $_ = $kana2morse->invert ($_);
1032 }
1033 1 $input = join '', @input;
1034 1 $input = join_sound_marks ($input);
1035 1 return $input;
1036 }
1037  
1038 25 my $kana2braille;
1039  
1040 sub load_kana2braille
1041 {
1042 2 ▶ if (!$kana2braille) {
Subroutine load_kana2braille: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (not $kana2braille) 1 1 2
1043 1 $kana2braille = make_convertors ('katakana', 'braille');
1044 }
1045 }
1046  
1047 25 my %nippon2kana;
1048  
1049 25 for my $k (keys %gyou) {
1050 450 for my $ar (@{$gyou{$k}}) {
1051 2025 my $vowel = $boin{$ar};
1052 2025 my $nippon = $k.$vowel;
1053 2025 $nippon2kana{$nippon} = $ar;
1054 }
1055 }
1056  
1057 sub is_kana
1058 {
1059 7 ▶ my ($may_be_kana) = @_;
Subroutine is_kana: called CC=2 SCAR=6.9
Pod documented
1060 7 ▶ if ($may_be_kana =~ /^[あ-ã‚“ã‚¢-ン]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($may_be_kana =~ /^[\x{3042}-\x{3093}\x{30a2}-\x{30f3}]+$/) 5 2 7
1061 5 return 1;
1062 }
1063 2 return;
1064 }
1065  
1066 sub is_katakana
1067 {
1068 0 ▶ my ($may_be_kana) = @_;
Subroutine is_katakana: not called CC=2 SCAR=17.9
Pod documented
1069 0 ▶ if ($may_be_kana =~ /^[ã‚¢-ン]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($may_be_kana =~ /^[\x{30a2}-\x{30f3}]+$/) 0 0 0
1070 0 return 1;
1071 }
1072 0 return;
1073 }
1074  
1075 sub is_hiragana
1076 {
1077 2 ▶ my ($may_be_kana) = @_;
Subroutine is_hiragana: called CC=2 SCAR=6.9
Pod documented
1078 2 ▶ if ($may_be_kana =~ /^[あ-ã‚“]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($may_be_kana =~ /^[\x{3042}-\x{3093}]+$/) 1 1 2
1079 1 return 1;
1080 }
1081 1 return;
1082 }
1083  
1084 25 my %daku2not = (qw/
1085 が か
1086 ぎ き
1087 ぐ く
1088 げ け
1089 ご こ
1090 だ た
1091 ぢ ち
1092 づ つ
1093 で て
1094 ど と
1095 ざ さ
1096 じ し
1097 ず す
1098 ぜ せ
1099 ぞ そ
1100 ば は
1101 び ひ
1102 ぶ ふ
1103 べ へ
1104 ぼ ほ
1105 ガ カ
1106 ã‚® ã‚­
1107 グ ク
1108 ゲ ケ
1109 ゴ コ
1110 ダ タ
1111 ヂ チ
1112 ヅ ツ
1113 デ テ
1114 ド ト
1115 ザ サ
1116 ジ シ
1117 ズ ス
1118 ゼ セ
1119 ゾ ソ
1120 バ ハ
1121 ビ ヒ
1122 ブ フ
1123 ベ ヘ
1124 ボ ホ
1125 /);
1126  
1127 25 my %not2daku = reverse %daku2not;
1128  
1129 25 my $daku = qr![がぎぐげごだぢづでどざじずぜぞばびぶべぼガギグゲゴダヂヅデドザジズゼゾバビブベボ]!;
1130  
1131 25 my $nodaku = qr![かきくけこたしつてとさしすせそはひふへほカキクケコタシツテトサシスセソハヒフヘホ]!;
1132  
1133 25 my %handaku2not = (qw!
1134 ぱ は
1135 ぴ ひ
1136 ぷ ふ
1137 ぺ へ
1138 ぽ ほ
1139 パ ハ
1140 ピ ヒ
1141 プ フ
1142 ペ ヘ
1143 ポ ホ
1144 !);
1145  
1146 25 my %not2handaku = reverse %handaku2not;
1147  
1148 25 my $handaku = qr![ぱぴぷぺぽパピプペポ]!;
1149  
1150 25 my $nohandaku = qr![はひふへほハヒフヘホ]!;
1151  
1152 sub join_sound_marks
1153 {
1154 210 ▶ my ($input) = @_;
Subroutine join_sound_marks: called CC=1 SCAR=0.0
Pod documented
1155 210 $input =~ s!($nohandaku)(゚|゜)!$not2handaku{$1}!g;
1156 210 $input =~ s!($nodaku)(ã‚™|ã‚›)!$not2daku{$1}!g;
1157 # Remove strays.
1158 210 $input =~ s![゙゛゚゜]!!g;
1159 210 return $input;
1160 }
1161  
1162 sub split_sound_marks
1163 {
1164 7 ▶ my ($input) = @_;
Subroutine split_sound_marks: called CC=1 SCAR=0.0
Pod documented
1165 7 $input =~ s!($handaku)!$handaku2not{$1}゜!g;
1166 7 $input =~ s!($daku)!$daku2not{$1}ã‚›!g;
1167 7 return $input;
1168 }
1169  
1170 sub strip_sound_marks
1171 {
1172 2 ▶ my ($input) = @_;
Subroutine strip_sound_marks: called CC=1 SCAR=0.0
Pod documented
1173 2 $input =~ s!($handaku)!$handaku2not{$1}!g;
1174 2 $input =~ s!($daku)!$daku2not{$1}!g;
1175 2 return $input;
1176 }
1177  
1178 sub kana2katakana
1179 {
1180 205 ▶ my ($input) = @_;
Subroutine kana2katakana: called CC=2 SCAR=6.9
Pod documented
1181 205 $input = join_sound_marks ($input);
1182 205 $input = hira2kata($input);
1183 205 ▶ if ($input =~ /\p{InHankakuKatakana}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /\p{InHankakuKatakana}/) 2 203 205
1184 2 $input = hw2katakana($input);
1185 }
1186 205 return $input;
1187 }
1188  
1189 sub kana2braille
1190 {
1191 1 ▶ my ($input) = @_;
Subroutine kana2braille: called CC=1 SCAR=0.0
Pod documented
1192 1 load_kana2braille;
1193 1 $input = kana2katakana ($input);
1194 1 $input = split_sound_marks ($input);
1195 1 $input =~ s/([キシチヒ])゛([ャュョ])/'⠘'.$nippon2kana{$siin{$1}.$boin{$2}}/eg;
1196 1 $input =~ s/(ヒ)゜([ャュョ])/'⠨'.$nippon2kana{$siin{$1}.$boin{$2}}/eg;
1197 1 $input =~ s/([キシチニヒミリ])([ャュョ])/'⠈'.$nippon2kana{$siin{$1}.$boin{$2}}/eg;
1198 1 $input =~ s/([$vowelclass{o}])ウ/$1ー/g;
1199 1 $input = $kana2braille->convert ($input);
1200 1 $input =~ s/(.)([⠐⠠])/$2$1/g;
1201 1 return $input;
1202 }
1203  
1204 sub braille2kana
1205 {
1206 1 ▶ my ($input) = @_;
Subroutine braille2kana: called CC=1 SCAR=0.0
Pod documented
1207 1 load_kana2braille;
1208 1 $input =~ s/([⠐⠠])(.)/$2$1/g;
1209 1 $input = $kana2braille->invert ($input);
1210 1 $input =~ s/â ˜(.)/$nippon2kana{$siin{$1}.'i'}.'ã‚›'.$youon{$boin{$1}}/eg;
1211 1 $input =~ s/⠨(.)/$nippon2kana{$siin{$1}.'i'}.'゜'.$youon{$boin{$1}}/eg;
1212 1 $input =~ s/â ˆ(.)/$nippon2kana{$siin{$1}.'i'}.$youon{$boin{$1}}/eg;
1213 1 $input = join_sound_marks ($input);
1214 1 return $input;
1215 }
1216  
1217 25 my $circled_conv;
1218  
1219 sub load_circled_conv
1220 {
1221 2 ▶ if (!$circled_conv) {
Subroutine load_circled_conv: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (not $circled_conv) 1 1 2
1222 1 $circled_conv = make_convertors ("katakana", "circled");
1223 }
1224 }
1225  
1226 sub kana2circled
1227 {
1228 1 ▶ my ($input) = @_;
Subroutine kana2circled: called CC=1 SCAR=0.0
Pod documented
1229 1 $input = kana2katakana($input);
1230 1 $input = split_sound_marks ($input);
1231 1 load_circled_conv;
1232 1 $input = $circled_conv->convert ($input);
1233 1 return $input;
1234 }
1235  
1236 sub circled2kana
1237 {
1238 1 ▶ my ($input) = @_;
Subroutine circled2kana: called CC=1 SCAR=0.0
Pod documented
1239 1 load_circled_conv;
1240 1 $input = $circled_conv->invert ($input);
1241 1 $input = join_sound_marks ($input);
1242 1 return $input;
1243 }
1244  
1245 sub normalize_romaji
1246 {
1247 2 ▶ my ($romaji) = @_;
Subroutine normalize_romaji: called CC=1 SCAR=0.0
Pod documented
1248 2 my $kana = romaji2kana ($romaji, {wapuro => 1});
1249 2 $kana =~ s/[っッ]/xtu/g;
1250 2 my $romaji_out = kana2romaji ($kana, {ve_type => 'wapuro'});
1251 2 return $romaji_out;
1252 }
1253  
1254 my $new2old_kanji;
1255  
1256 sub load_new2old_kanji
1257 {
1258 1 ▶ $new2old_kanji = Convert::Moji->new (
Subroutine load_new2old_kanji: called CC=1 SCAR=0.0
Pod undocumented
1259 ['file', getdistfile ('new_kanji2old_kanji')],
1260 );
1261 }
1262  
1263 sub new2old_kanji
1264 {
1265 1 ▶ my ($new_kanji) = @_;
Subroutine new2old_kanji: called CC=2 SCAR=7.4
Pod documented
1266 1 ▶ if (! $new2old_kanji) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new2old_kanji) 0 1 1
1267 0 load_new2old_kanji ();
1268 }
1269 1 my $old_kanji = $new2old_kanji->convert ($new_kanji);
1270 1 return $old_kanji;
1271 }
1272  
1273 sub old2new_kanji
1274 {
1275 1 ▶ my ($old_kanji) = @_;
Subroutine old2new_kanji: called CC=2 SCAR=7.0
Pod documented
1276 1 ▶ if (! $new2old_kanji) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new2old_kanji) 1 0 1
1277 1 load_new2old_kanji ();
1278 }
1279 1 my $new_kanji = $new2old_kanji->invert ($old_kanji);
1280 1 return $new_kanji;
1281 }
1282  
1283 25 my $katakana2cyrillic;
1284  
1285 sub load_katakana2cyrillic
1286 {
1287 1 ▶ $katakana2cyrillic = Convert::Moji->new (['file', getdistfile ('katakana2cyrillic')]);
Subroutine load_katakana2cyrillic: called CC=1 SCAR=0.0
Pod undocumented
1288 }
1289  
1290 sub kana2cyrillic
1291 {
1292 4 ▶ my ($kana) = @_;
Subroutine kana2cyrillic: called CC=2 SCAR=6.9
Pod documented
1293 4 my $katakana = kana2katakana ($kana);
1294 4 $katakana =~ s/ン([アイウエオヤユヨ])/ンъ$1/g;
1295 4 ▶ if (! $katakana2cyrillic) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($katakana2cyrillic) 1 3 4
1296 1 load_katakana2cyrillic ();
1297 }
1298 4 my $cyrillic = $katakana2cyrillic->convert ($katakana);
1299 4 $cyrillic =~ s/н([пбм])/м$1/g;
1300 4 return $cyrillic;
1301 }
1302  
1303 sub cyrillic2katakana
1304 {
1305 6 ▶ my ($cyrillic) = @_;
Subroutine cyrillic2katakana: called CC=2 SCAR=7.1
Pod documented
1306 # Convert the Cyrillic letters to lower case versions of the
1307 # letters. This table of conversions was made from the one in
1308 # Wikipedia at <http://en.wikipedia.org/wiki/Cyrillic_alphabets>
1309 # using Emacs, the revision being
1310 # <http://en.wikipedia.org/w/index.php?title=Cyrillic_alphabets&oldid=482154809>.
1311 # I do not know if it covers the alphabets perfectly.
1312 6 $cyrillic =~ tr/АБВГДЕЖЗИЙIКЛМНОПРСТУФХЦЧШЩЬЮЯ/абвгдежзийiклмнопрстуфхцчшщьюя/;
1313 6 ▶ if (! $katakana2cyrillic) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($katakana2cyrillic) 0 6 6
1314 0 load_katakana2cyrillic ();
1315 }
1316 6 my $katakana = $katakana2cyrillic->invert ($cyrillic);
1317 6 $katakana =~ s/м/ン/g;
1318 6 $katakana =~ s/ンъ([アイウエオヤユヨ])/ン$1/g;
1319 6 return $katakana;
1320 }
1321  
1322 25 my $first2hangul;
1323 25 my $rest2hangul;
1324  
1325 25 my $first2hangul_re;
1326 25 my $rest2hangul_re;
1327  
1328 sub load_kana2hangul
1329 {
1330 2 ▶ $first2hangul = load_convertor ('first', 'hangul');
Subroutine load_kana2hangul: called CC=1 SCAR=0.0
Pod undocumented
1331 2 $rest2hangul = load_convertor ('rest', 'hangul');
1332 2 $first2hangul_re = '\b' . make_regex (keys %$first2hangul);
1333 2 $rest2hangul_re = make_regex (keys %$rest2hangul);
1334 }
1335  
1336 sub kana2hangul
1337 {
1338 2 ▶ my ($kana) = @_;
Subroutine kana2hangul: called CC=2 SCAR=6.9
Pod documented
1339 2 my $katakana = kana2katakana ($kana);
1340 2 ▶ if (! $first2hangul) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first2hangul) 1 1 2
1341 1 load_kana2hangul ();
1342 }
1343 2 $katakana =~ s/($first2hangul_re)/$first2hangul->{$1}/g;
1344 2 $katakana =~ s/($rest2hangul_re)/$rest2hangul->{$1}/g;
1345 2 return $katakana;
1346 }
1347  
1348 25 my $firsth2k_re;
1349 25 my $resth2k_re;
1350 25 my $firsth2k;
1351 25 my $resth2k;
1352  
1353 sub load_hangul2kana
1354 {
1355 1 ▶ load_kana2hangul ();
Subroutine load_hangul2kana: called CC=1 SCAR=0.0
Pod undocumented
1356 1 $firsth2k = { reverse %$first2hangul };
1357 1 $resth2k = { reverse %$rest2hangul };
1358 1 $firsth2k_re = '\b' . make_regex (keys %$firsth2k);
1359 1 $resth2k_re = make_regex (keys %$resth2k);
1360 }
1361  
1362 sub hangul2kana
1363 {
1364 1 ▶ my ($hangul) = @_;
Subroutine hangul2kana: called CC=2 SCAR=7.0
Pod undocumented
1365 1 ▶ if (! $firsth2k) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($firsth2k) 1 0 1
1366 1 load_hangul2kana ();
1367 }
1368 1 $hangul =~ s/($firsth2k_re)/$firsth2k->{$1}/;
1369 1 $hangul =~ s/($resth2k_re)/$resth2k->{$1}/;
1370 1 return $hangul;
1371 }
1372  
1373 25 my %small2large = qw!
1374 ゃ や
1375 ゅ ゆ
1376 ょ よ
1377 ぁ あ
1378 ぃ い
1379 ぅ う
1380 ぇ え
1381 ぉ お
1382 っ つ
1383 ゎ わ
1384 !;
1385  
1386 sub kana_to_large
1387 {
1388 2 ▶ my ($kana) = @_;
Subroutine kana_to_large: called CC=1 SCAR=0.0
Pod documented
1389 2 $kana =~ tr/ゃゅょぁぃぅぇぉっゎ/やゆよあいうえおつわ/;
1390 2 $kana =~ tr/ャュョァィゥェォッヮ/ヤユヨアイウエオツワ/;
1391 # Katakana phonetic extensions.
1392 2 $kana =~ tr/ㇰㇱㇲㇳㇴㇵㇶㇷㇸㇹㇺㇻㇼㇽㇾㇿ/クシストヌハヒフヘホムラリルレロ/;
1393 2 return $kana;
1394 }
1395  
1396 25 my $circled2kanji;
1397  
1398 sub load_circled2kanji
1399 {
1400 4 ▶ if (! $circled2kanji) {
Subroutine load_circled2kanji: called CC=3 SCAR=11.4
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless ($circled2kanji) 1 3 4
1401 1 $circled2kanji =
1402 Convert::Moji->new (["file",
1403 getdistfile ('circled2kanji')]);
1404 }
1405 4 ▶ if (! $circled2kanji) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $circled2kanji) 0 4 4
1406 0 die "ERROR";
1407 }
1408 }
1409  
1410 sub circled2kanji
1411 {
1412 2 ▶ my ($input) = @_;
Subroutine circled2kanji: called CC=1 SCAR=0.0
Pod documented
1413 2 load_circled2kanji ();
1414 2 return $circled2kanji->convert ($input);
1415 }
1416  
1417 sub kanji2circled
1418 {
1419 2 ▶ my ($input) = @_;
Subroutine kanji2circled: called CC=1 SCAR=0.0
Pod documented
1420 2 load_circled2kanji ();
1421 2 return $circled2kanji->invert ($input);
1422 }
1423  
1424 my $bracketed2kanji;
1425  
1426 sub load_bracketed2kanji
1427 {
1428 2 ▶ if (! $bracketed2kanji) {
Subroutine load_bracketed2kanji: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (not $bracketed2kanji) 1 1 2
1429 1 $bracketed2kanji =
1430 Convert::Moji->new (["file",
1431 getdistfile ('bracketed2kanji')]);
1432 }
1433 }
1434  
1435 sub bracketed2kanji
1436 {
1437 1 ▶ my ($input) = @_;
Subroutine bracketed2kanji: called CC=1 SCAR=0.0
Pod documented
1438 1 load_bracketed2kanji ();
1439 1 return $bracketed2kanji->convert ($input);
1440 }
1441  
1442 sub kanji2bracketed
1443 {
1444 1 ▶ my ($input) = @_;
Subroutine kanji2bracketed: called CC=1 SCAR=0.0
Pod documented
1445 1 load_bracketed2kanji ();
1446 1 return $bracketed2kanji->invert ($input);
1447 }
1448  
1449 sub InKana
1450 {
1451 30 ▶ return <<"END";
Subroutine InKana: called CC=1 SCAR=0.0
Pod documented
1452 +utf8::Katakana
1453 +utf8::InKatakana
1454 +utf8::InHiragana
1455 FF9E\tFF9F
1456 FF70
1457 -utf8::IsCn
1458 -30FB
1459 END
1460 # Explanation of the above gibberish: The funny hex is for dakuten
1461 # and handakuten half width. The "Katakana" catches halfwidth
1462 # katakana, and the "InKatakana" catches the chouon mark. "IsCn"
1463 # means "other, not assigned", so we remove this to prevent
1464 # matching non-kana characters floating around near to real
1465 # ones. 30FB is "Katakana middle dot", which is not kana as far as
1466 # I know, so that's also removed.
1467 }
1468  
1469 # お
1470  
1471 # Match zero or one sokuons, one full-sized kana character, then zero
1472 # or one each of small kana, chouon, and syllabic n, in that order.
1473  
1474 25 my $kana2syllable_re = qr/ッ?[アイウエオ-モヤユヨ-ヴ](?:[ャュョァィゥェォ])?ー?ン?/;
1475  
1476 sub katakana2syllable
1477 {
1478 4 ▶ my ($kana) = @_;
Subroutine katakana2syllable: called CC=2 SCAR=6.9
Pod documented
1479 4 my @pieces;
1480 4 ▶ while ($kana =~ /($kana2syllable_re)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kana =~ /($kana2syllable_re)/g) 22 4 26
1481 22 push @pieces, $1;
1482 }
1483 4 return \@pieces;
1484 }
1485  
1486 25 my $square2katakana;
1487  
1488 sub load_square2katakana
1489 {
1490 2 ▶ if (! $square2katakana) {
Subroutine load_square2katakana: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (not $square2katakana) 1 1 2
1491 1 $square2katakana =
1492 Convert::Moji->new (["file",
1493 getdistfile ('square-katakana')]);
1494 }
1495 }
1496  
1497 sub square2katakana
1498 {
1499 1 ▶ load_square2katakana ();
Subroutine square2katakana: called CC=1 SCAR=0.0
Pod documented
1500 1 return $square2katakana->convert (@_);
1501 }
1502  
1503 sub katakana2square
1504 {
1505 1 ▶ load_square2katakana ();
Subroutine katakana2square: called CC=1 SCAR=0.0
Pod documented
1506 1 return $square2katakana->invert (@_);
1507 }
1508  
1509 sub nigori_first
1510 {
1511 1 ▶ my ($list) = @_;
Subroutine nigori_first: called CC=5 SCAR=16.1
Pod documented
1512 1 my @nigori;
1513 1 for my $kana (@$list) {
1514 4 my ($first, $remaining) = split //, $kana, 2;
1515 4 my $nf = $not2daku{$first};
1516 4 ▶ if ($nf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nf) 3 1 4
1517 3 push @nigori, $nf.$remaining;
1518 }
1519 4 my $hf = $not2handaku{$first};
1520 4 ▶ if ($hf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hf) 1 3 4
1521 1 push @nigori, $hf.$remaining;
1522 }
1523 }
1524 1 ▶ if (@nigori) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nigori) 1 0 1
1525 1 push @$list, @nigori;
1526 }
1527 }
1528  
1529 # Hentaigana (Unicode 10.0) related
1530  
1531 25 my $hentai_file = __FILE__;
1532 25 $hentai_file =~ s!\.pm$!/!;
1533 25 $hentai_file .= "hentaigana.json";
1534 # Hentai to hiragana (one to one)
1535 25 my %hen2hi;
1536 # Hiragana to hentai (one to many)
1537 my %hi2hen;
1538 # Hentaigana to kanji
1539 25 my %hen2k;
1540 # Kanji to hentaigana
1541 25 my %k2hen;
1542 25 my $k2hen_re;
1543 # Hentai to hiragana/kanji regex (recycled for the kanji case).
1544 25 my $hen_re;
1545 # Hiragana to hentai regex
1546 25 my $hi2hen_re;
1547 # Hentai data
1548 25 my $hendat;
1549  
1550 sub load_hentai
1551 {
1552 1 ▶ $hendat = read_json ($hentai_file);
Subroutine load_hentai: called CC=3 SCAR=11.0
Pod undocumented
1553 1 for my $h (@$hendat) {
1554 285 my $hi = $h->{hi};
1555 285 my $hen = chr ($h->{u});
1556 285 $hen2hi{$hen} = $hi;
1557 285 for my $hiragana (@$hi) {
1558 298 push @{$hi2hen{$hiragana}}, $hen;
1559 }
1560 285 $hen2k{$hen} = $h->{ka};
1561 285 push @{$k2hen{$h->{ka}}}, $hen;
1562 }
1563 1 $hen_re = make_regex (keys %hen2hi);
1564 1 $hi2hen_re = make_regex (keys %hi2hen);
1565 1 $k2hen_re = make_regex (keys %k2hen);
1566 }
1567  
1568 sub hentai2kana
1569 {
1570 1 ▶ my ($text) = @_;
Subroutine hentai2kana: called CC=2 SCAR=7.0
Pod documented
1571 1 ▶ if (! $hendat) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hendat) 1 0 1
1572 1 load_hentai ();
1573 }
1574 1 $text =~ s/$hen_re/join ('・', @{$hen2hi{$1}})/ge;
1575 1 return $text;
1576 }
1577  
1578 sub kana2hentai
1579 {
1580 2 ▶ my ($text) = @_;
Subroutine kana2hentai: called CC=2 SCAR=7.1
Pod documented
1581 2 ▶ if (! $hendat) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hendat) 0 2 2
1582 0 load_hentai ();
1583 }
1584 # Make it all-hiragana.
1585 2 $text = split_sound_marks ($text);
1586 2 $text = kata2hira ($text);
1587 2 $text =~ s/$hi2hen_re/join ('・', @{$hi2hen{$1}})/ge;
1588 2 return $text;
1589 # what to do?
1590 }
1591  
1592 sub hentai2kanji
1593 {
1594 1 ▶ my ($text) = @_;
Subroutine hentai2kanji: called CC=2 SCAR=7.4
Pod documented
1595 1 ▶ if (! $hendat) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hendat) 0 1 1
1596 0 load_hentai ();
1597 }
1598 # This uses the same regex as the kanji case.
1599 1 $text =~ s/$hen_re/$hen2k{$1}/g;
1600 1 return $text;
1601 }
1602  
1603 sub kanji2hentai
1604 {
1605 1 ▶ my ($text) = @_;
Subroutine kanji2hentai: called CC=2 SCAR=7.1
Pod documented
1606 1 ▶ if (! $hendat) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hendat) 0 1 1
1607 0 load_hentai ();
1608 }
1609 1 $text =~ s/$k2hen_re/join ('・', @{$k2hen{$1}})/ge;
1610 1 return $text;
1611 }
1612  
1613 25 my %yayuyo = (qw/
1614 ヤ ャ
1615 ユ ュ
1616 ヨ ョ
1617 /);
1618  
1619 25 my %l2s = qw!ア ァ イ ィ ウ ゥ エ ェ オ ォ!;
1620  
1621 sub smallize_kana
1622 {
1623 10 ▶ my ($kana) = @_;
Subroutine smallize_kana: called CC=2 SCAR=6.9
Pod documented
1624 10 my $orig = $kana;
1625 10 $kana =~ s/([キギシジチヂニヒビピミリ])([ヤユヨ])/$1$yayuyo{$2}/g;
1626 # Don't make "ツル" into "ッル".
1627 10 $kana =~ s/([$before_sokuon])ツ([$takes_sokuon])/$1ッ$2/g;
1628 10 $kana =~ s/フ([アイエオ])/フ$l2s{$1}/g;
1629 10 ▶ if ($kana ne $orig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kana ne $orig) 7 3 10
1630 7 return $kana;
1631 }
1632 3 return undef;
1633 }
1634  
1635 sub cleanup_kana
1636 {
1637 3 ▶ my ($kana) = @_;
Subroutine cleanup_kana: called CC=4 SCAR=14.4
Pod documented
1638 3 ▶ if (! $kana) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kana) 0 3 3
1639 0 return $kana;
1640 }
1641 3 ▶ if ($kana =~ /[\x{ff01}-\x{ff5e}]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kana =~ /[\x{ff01}-\x{ff5e}]/) { } 1 2 3
elsif ($kana =~ /[a-z\342\356\373\352\364\364\x{101}\x{16b}\x{113}\x{14d}]/i) { } 0 2 2
1642 1 $kana = wide2ascii ($kana);
1643 1 $kana = romaji2kana ($kana);
1644 }
1645 elsif ($kana =~ /[a-zâîûêôôāūēō]/i) {
1646 0 $kana = romaji2kana ($kana);
1647 }
1648 # This calls join_sound_marks, so that call is not necessary.
1649 3 $kana = kana2katakana ($kana);
1650 # Translate kanjis into katakana where a "naive user" has inserted
1651 # kanji not kana. Because the following expression is visually
1652 # confusing, note that the LHS are all kanji, and the RHS are all
1653 # kana/chouon
1654 3 $kana =~ tr/囗口八力二一/ロロハカニー/;
1655 # Turn silly small youon kana into big ones
1656 3 $kana =~ s/([^きぎしじちぢにひびぴみり]|^)([ゃゅょ])/$1$small2large{$2}/g;
1657 3 return $kana;
1658 }
1659  
1660 sub load_kanji
1661 {
1662 2 ▶ my ($file) = @_;
Subroutine load_kanji: called CC=5 SCAR=16.2
Pod undocumented
1663 2 my $bkfile = getdistfile ($file);
1664 2 ▶ open my $in, "<:encoding(utf8)", $bkfile
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<:encoding(utf8)", $bkfile or die "Error opening '${bkfile}': $!" 0 2 2
1665 or die "Error opening '$bkfile': $!";
1666 2 my @bk;
1667 2 ▶ while (<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 20 2 22
1668 20 ▶ while (/(\p{InCJKUnifiedIdeographs})/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/(\p{InCJKUnifiedIdeographs})/g) 268 20 288
1669 268 push @bk, $1;
1670 }
1671 }
1672 2 ▶ close $in or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $in or die $! 0 2 2
1673 2 return @bk;
1674 }
1675  
1676 sub yurei_moji
1677 {
1678 1 ▶ return load_kanji ('yurei-moji')
Subroutine yurei_moji: called CC=1 SCAR=0.0
Pod documented
1679 }
1680  
1681 sub bad_kanji
1682 {
1683 1 ▶ return load_kanji ('bad-kanji');
Subroutine bad_kanji: called CC=1 SCAR=0.0
Pod documented
1684 }
1685  
1686 sub kana_consonant
1687 {
1688 5 ▶ my ($kana) = @_;
Subroutine kana_consonant: called CC=4 SCAR=13.9
Pod documented
1689 5 ▶ if (length ($kana) < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $kana < 1) 1 4 5
1690 1 croak "Empty input to kana_consonant";
1691 }
1692 4 my $first = substr ($kana, 0, 1);
1693 4 ▶ if ($first !~ /\p{InKana}/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first =~ /\p{InKana}/) 1 3 4
1694 1 croak "First character '$first' of '$kana' is not kana";
1695 }
1696 3 $first = kana2katakana ($first);
1697 3 my $not = $daku2not{$first};
1698 3 ▶ if (defined $not) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $not) 1 2 3
1699 1 $first = $not;
1700 }
1701 3 my $con = $siin{$first};
1702 3 return $con;
1703 }
1704  
1705 25 1;
↑ Summary