| 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
| |||||||||||||||||||||||||||||
| 134 | 47 | $char = substr $string, $index, 2; | |||||||||||||||||||||||||||
| 135 | 47 | $hepburn = $Map{$char}; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 69 | ▶ | if (!$hepburn && $index < length $string) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $hepburn and $index < length $string
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 155 | 3 | ▶ | $string =~ /^\p{Hiragana}*$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 169 | 3 | my $next = _hepburn_for($string, $i + 1); | |||||||||||||||||||||||||||
| 170 | 3 | ▶ | $hr->{hepburn} = $next->{hepburn} && $next->{hepburn} =~ /^[BMP]/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$next->{"hepburn"} and $next->{"hepburn"} =~ /^[BMP]/
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]/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 180 | $hr->{hepburn} = $next->{hepburn} =~ /^CH/ | ||||||||||||||||||||||||||||
| 181 | 6 | ▶ | ? 'T' : substr($next->{hepburn}, 0, 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||
| 192 | 1 | croak "$hr->{char} is not allowed"; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 59 | ▶ | if (defined $hr->{hepburn}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 59 | ▶ | if ($last_hepburn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 34 | my $h_test = $last_hepburn . $hr->{hepburn}; | |||||||||||||||||||||||||||
| 198 | 34 | ▶ | if (length $h_test > 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 204 | 4 | $hr->{hepburn} = ''; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # 氏名に「オウ」又は「オオ」の長音が含まれる場合、 | ||||||||||||||||||||||||||||
| 208 | # 「 O 」 か 「 OH 」 のいずれかの表記を選択することができる | ||||||||||||||||||||||||||||
| 209 | 34 | ▶ | if (grep $h_test eq $_, qw( OO OU )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 6 | ▶ | $hr->{hepburn} = $self->{long_vowels_h} ? 'H' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 59 | $output .= $hr->{hepburn}; | |||||||||||||||||||||||||||
| 215 | } else { | ||||||||||||||||||||||||||||
| 216 | 0 | ▶ | if ($self->{strict}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||