blib/lib/Lingua/JA/Hepburn/Passport.pm

filter: Statementstmt 93.2% 55 / 59 Branchbran 85.0% 34 / 40 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 86.8% 106 / 122 CC 24 SCAR 28.4
CC
24
Cov
88%
romanize
22.6
_hepburn_for
4.0
BEGIN
1.0
CRAP 17.1
↑ Summary
1 package Lingua::JA::Hepburn::Passport;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 our $VERSION = '0.02';
5  
6 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our %Map = (
10 "あ", "A",
11 "い", "I",
12 "う", "U",
13 "え", "E",
14 "お", "O",
15 "か", "KA",
16 "き", "KI",
17 "く", "KU",
18 "け", "KE",
19 "こ", "KO",
20 "さ", "SA",
21 "し", "SHI",
22 "す", "SU",
23 "せ", "SE",
24 "そ", "SO",
25 "た", "TA",
26 "ち", "CHI",
27 "つ", "TSU",
28 "て", "TE",
29 "と", "TO",
30 "な", "NA",
31 "に", "NI",
32 "ぬ", "NU",
33 "ね", "NE",
34 "の", "NO",
35 "は", "HA",
36 "ひ", "HI",
37 "ふ", "FU",
38 "へ", "HE",
39 "ほ", "HO",
40 "ま", "MA",
41 "み", "MI",
42 "む", "MU",
43 "め", "ME",
44 "も", "MO",
45 "や", "YA",
46 "ゆ", "YU",
47 "よ", "YO",
48 "ら", "RA",
49 "り", "RI",
50 "る", "RU",
51 "れ", "RE",
52 "ろ", "RO",
53 "わ", "WA",
54 "ゐ", "I",
55 "ゑ", "E",
56 "を", "O",
57 "ん", "N",
58 "ぁ", "A",
59 "ぃ", "I",
60 "ぅ", "U",
61 "ぇ", "E",
62 "ぉ", "O",
63 "が", "GA",
64 "ぎ", "GI",
65 "ぐ", "GU",
66 "げ", "GE",
67 "ご", "GO",
68 "ざ", "ZA",
69 "じ", "JI",
70 "ず", "ZU",
71 "ぜ", "ZE",
72 "ぞ", "ZO",
73 "だ", "DA",
74 "ぢ", "JI",
75 "づ", "ZU",
76 "で", "DE",
77 "ど", "DO",
78 "ば", "BA",
79 "び", "BI",
80 "ぶ", "BU",
81 "べ", "BE",
82 "ぼ", "BO",
83 "ぱ", "PA",
84 "ぴ", "PI",
85 "ぷ", "PU",
86 "ぺ", "PE",
87 "ぽ", "PO",
88 "きゃ", "KYA",
89 "きゅ", "KYU",
90 "きょ", "KYO",
91 "しゃ", "SHA",
92 "しゅ", "SHU",
93 "しょ", "SHO",
94 "ちゃ", "CHA",
95 "ちゅ", "CHU",
96 "ちょ", "CHO",
97 "ちぇ", "CHE",
98 "にゃ", "NYA",
99 "にゅ", "NYU",
100 "にょ", "NYO",
101 "ひゃ", "HYA",
102 "ひゅ", "HYU",
103 "ひょ", "HYO",
104 "みゃ", "MYA",
105 "みゅ", "MYU",
106 "みょ", "MYO",
107 "りゃ", "RYA",
108 "りゅ", "RYU",
109 "りょ", "RYO",
110 "ぎゃ", "GYA",
111 "ぎゅ", "GYU",
112 "ぎょ", "GYO",
113 "じゃ", "JA",
114 "じゅ", "JU",
115 "じょ", "JO",
116 "びゃ", "BYA",
117 "びゅ", "BYU",
118 "びょ", "BYO",
119 "ぴゃ", "PYA",
120 "ぴゅ", "PYU",
121 "ぴょ", "PYO",
122 );
123  
124 21 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
125 21 my($class, %opt) = @_;
126 21 bless { %opt }, $class;
127 }
128  
129 69 ▶sub _hepburn_for {
Subroutine _hepburn_for: called CC=4 SCAR=13.9
130 69 my($string, $index) = @_;
131  
132 69 my($hepburn, $char);
133 69 ▶ if ($index + 1 < length $string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index + 1 < length $string) 47 22 69
134 47 $char = substr $string, $index, 2;
135 47 $hepburn = $Map{$char};
136 }
137 69 ▶ if (!$hepburn && $index < length $string) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hepburn and $index < length $string) 61 8 69
Condition66%2/3
not $hepburn and $index < length $string
!ll&&!rl&&r
8061
MC/DC50%1/2
not $hepburn and $index < length $string
not $hepburn $index < length $string
Truth table66%2/3
not(A) and B
A= not $hepburnB= $index < length $string
ABresult
0X0
100
111
138 61 $char = substr $string, $index, 1;
139 61 $hepburn = $Map{$char};
140 }
141  
142 69 return { char => $char, hepburn => $hepburn };
143 }
144  
145 21 ▶sub romanize {
Subroutine romanize: called CC=21 SCAR=31.2
Pod documented
146 21 my($self, $string) = @_;
147  
148 21 ▶ unless (utf8::is_utf8($string)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::is_utf8($string)) 0 21 21
149 0 croak "romanize(string): should be UTF-8 flagged string";
150 }
151  
152 21 $string =~ tr/ア-ン/あ-ん/;
153  
154 21 ▶ if ($self->{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict"}) 3 18 21
155 3 ▶ $string =~ /^\p{Hiragana}*$/
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /^\p{Hiragana}*$/ or croak("romanize(string): should be all Hiragana/Katakana") 1 2 3
156 or croak "romanize(string): should be all Hiragana/Katakana";
157 }
158  
159 19 my $output;
160 my $last_hepburn;
161 19 my $last_char;
162 19 my $i = 0;
163  
164 19 ▶ while ($i < length $string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $string) 60 18 78
165 60 my $hr = _hepburn_for($string, $i);
166  
167 # 1.撥音 ヘボン式ではB ・M ・P の前に N の代わりに M をおく
168 60 ▶ if ($hr->{char} eq 'ん') {
Branch: 4 branches
BranchTrueFalseTotal
if ($hr->{'char'} eq "\x{3093}") { } 3 57 60
elsif ($hr->{'char'} eq "\x{3063}") { } 6 51 57
elsif ($hr->{'char'} eq "\x{30fc}") { } 1 50 51
elsif ($hr->{'char'} =~ /[\x{3041}-\x{3049}]/ and $self->{'strict'}) { } 1 89 90
169 3 my $next = _hepburn_for($string, $i + 1);
170 3 ▶ $hr->{hepburn} = $next->{hepburn} && $next->{hepburn} =~ /^[BMP]/
Branch: 1 branch
BranchTrueFalseTotal
$next->{'hepburn'} && $next->{'hepburn'} =~ /^[BMP]/ ? : 3 0 3
Condition33%1/3
$next->{"hepburn"} and $next->{"hepburn"} =~ /^[BMP]/
!ll&&!rl&&r
003
MC/DC0%0/2
$next->{"hepburn"} and $next->{"hepburn"} =~ /^[BMP]/
$next->{"hepburn"} $next->{"hepburn"} =~ /^[BMP]/
Truth table33%1/3
A and B
A= $next->{"hepburn"}B= $next->{"hepburn"} =~ /^[BMP]/
ABresult
0X0
100
111
171 ? 'M' : 'N';
172 }
173  
174 # 2.促音 子音を重ねて示す
175 elsif ($hr->{char} eq 'っ') {
176 6 my $next = _hepburn_for($string, $i + 1);
177  
178 # チ(CH I)、チャ(CHA)、チュ(CHU)、チョ(CHO)音に限り、その前に T を加える。
179 6 ▶ if ($next->{hepburn}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->{"hepburn"}) 6 0 6
180 $hr->{hepburn} = $next->{hepburn} =~ /^CH/
181 6 ▶ ? 'T' : substr($next->{hepburn}, 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
$next->{'hepburn'} =~ /^CH/ ? : 4 2 6
182 }
183 }
184  
185 # 3.長音 ヘボン式では長音を表記しない
186 elsif ($hr->{char} eq "ー") {
187 1 $hr->{hepburn} = "";
188 }
189  
190 # Japanese Passport table doesn't have entries for ぁ-ぉ
191 ▶ elsif ($hr->{char} =~ /[ぁ-ぉ]/ && $self->{strict}) {
Condition100%3/3
$hr->{"char"} =~ /[\x{3041}-\x{3049}]/ and $self->{"strict"}
!ll&&!rl&&r
4091
MC/DC100%2/2
$hr->{"char"} =~ /[\x{3041}-\x{3049}]/ and $self->{"strict"}
$hr->{"char"} =~ /[\x{3041}-\x{3049}]/ $self->{"strict"}
Truth table100%3/3
A and B
A= $hr->{"char"} =~ /[\x{3041}-\x{3049}]/B= $self->{"strict"}
ABresult
0X0
100
111
192 1 croak "$hr->{char} is not allowed";
193 }
194  
195 59 ▶ if (defined $hr->{hepburn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hr->{'hepburn'}) { } 59 0 59
196 59 ▶ if ($last_hepburn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_hepburn) 34 25 59
197 34 my $h_test = $last_hepburn . $hr->{hepburn};
198 34 ▶ if (length $h_test > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $h_test > 2) 31 3 34
199 31 $h_test = substr $h_test, -2;
200 }
201  
202 # 3.長音 ヘボン式では長音を表記しない
203 34 ▶ if (grep $h_test eq $_, qw( AA II UU EE )) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($h_test eq $_), ("AA", "II", "UU", "EE"))) 4 30 34
204 4 $hr->{hepburn} = '';
205 }
206  
207 # 氏名に「オウ」又は「オオ」の長音が含まれる場合、
208 # 「 O 」 か 「 OH 」 のいずれかの表記を選択することができる
209 34 ▶ if (grep $h_test eq $_, qw( OO OU )) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($h_test eq $_), ("OO", "OU"))) 6 28 34
210 6 ▶ $hr->{hepburn} = $self->{long_vowels_h} ? 'H' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'long_vowels_h'} ? : 3 3 6
211 }
212 }
213  
214 59 $output .= $hr->{hepburn};
215 } else {
216 0 ▶ if ($self->{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict"}) 0 0 0
217 0 croak "Can't find hepburn replacement for $hr->{char}";
218 }
219 0 $output .= $hr->{char};
220 }
221  
222 59 $last_hepburn = $hr->{hepburn};
223 59 $last_char = $hr->{char};
224 59 $i += length $hr->{char};
225 }
226  
227 18 return $output;
228 }
229  
230 4 1;
231 __END__
↑ Summary