ファイル名に漢字が含まれていると都合が悪い場面に遭遇し漢字をローマ字に変換する方法を模索してみました。
スクリプト
<#
.SYNOPSIS
漢字をローマ字に変換
#>
function Kanji2Romaji {
param($kanji)
$atbl = @{
"gya" = "ぎゃ";"gyu" = "ぎゅ";"gyo" = "ぎょ";
"kya" = "きゃ";"kyu" = "きゅ";"kyo" = "きょ"
"ja" = "じゃ";"jyu" = "じゅ";"jyo" = "じょ"
"sha" = "しゃ";"syu" = "しゅ";"syo" = "しょ"
"dya" = "ぢゃ";"dyu" = "ぢゅ";"dyo" = "ぢょ"
"tya" = "ちゃ";"tyu" = "ちゅ";"tyo" = "ちょ"
"pya" = "びゃ";"pyu" = "びゅ";"pyo" = "びょ"
"hya" = "ひゃ";"hyu" = "ひゅ";"hyo" = "ひょ"
"mya" = "みゃ";"myu" = "みゅ";"myo" = "みょ"
"rya" = "りゃ";"ryu" = "りゅ";"ryo" = "りょ"
}
$btbl = @{
"a"="あ";"i"="い";"u"="う";"e"="え";"o"="お";
"ka"= "か";"ki"= "き";"ku"= "く";"ke"= "け";"ko"= "こ";
"sa"= "さ";"shi"= "し";"su"= "す";"se"= "せ";"so"= "そ";
"ta"= "た";"chi"= "ち";"tsu"= "つ";"te"= "て";"to"= "と";
"na"= "な";"ni"= "に";"nu"= "ぬ";"ne"= "ね";"no"= "の";
"ha"= "は";"hi"= "ひ";"fu"= "ふ";"he"= "へ";"ho"= "ほ";
"ma"= "ま";"mi"= "み";"mu"= "む";"me"= "め";"mo"= "も";
"ra"= "ら";"ri"= "り";"ru"= "る";"re"= "れ";"ro"= "ろ";
"ya"= "や";"yu"= "ゆ";"yo"= "よ";"wa"= "わ";"wo"= "を";
"n"= "ん";
"ga"= "が";"gi"= "ぎ";"gu"= "ぐ";"ge"= "げ";"go"= "ご";
"za"= "ざ";"ji"= "じ";"zu"= "ず";"ze"= "ぜ";"zo"= "ぞ";
"da"= "だ";"di"= "ぢ";"du"= "づ";"de"= "で";"do"= "ど";
"ba"= "ば";"bi"= "び";"bu"= "ぶ";"be"= "べ";"bo"= "ぼ";
"pa"= "ぱ";"pi"= "ぴ";"pu"= "ぷ";"pe"= "ぺ";"po"= "ぽ";
}
$romaji = ""
$t = [Windows.Globalization.JapanesePhoneticAnalyzer, Windows.Globalization, ContentType=WindowsRuntime]::GetWords($kanji, $true)
$t | % { $romaji += $_.YomiText }
# 先に置き換えた方が良い文字
foreach($akey in $atbl.Keys) {
$romaji = $romaji -replace $atbl[$akey], $akey
}
# メインの置き換え
foreach($bkey in $btbl.Keys) {
$romaji = $romaji -replace $btbl[$bkey], $bkey
}
# 空白を置き換え
$romaji = $romaji -replace " ", " "
$romaji
}
Write-Host (Kanji2Romaji "漢字 犬")
実行結果
kanji inu
コメント