読者です 読者をやめる 読者になる 読者になる

No Programming, No Life

新しいNPNLです。http://d.hatena.ne.jp/fumokmm/ から引っ越してきました。

Re:ひらがな->カタカナ変換Groovyスクリプト

id:nemo_kazさんのところ(⇒こちら)で変換スクリプトが書かれていたので、私も書いてみた。
単純に変換テーブルを宣言しただけだが。

ソースコード

class Converter {
	def maps = [
		[from:'あ',   to:'ア'],
		[from:'い',   to:'イ'],
		[from:'う',   to:'ウ'],
		[from:'え',   to:'エ'],
		[from:'お',   to:'オ'],
		[from:'ぁ',   to:'ァ'],
		[from:'ぃ',   to:'ィ'],
		[from:'ぅ',   to:'ゥ'],
		[from:'ぇ',   to:'ェ'],
		[from:'ぉ',   to:'ォ'],
		[from:'か',   to:'カ'],
		[from:'き',   to:'キ'],
		[from:'く',   to:'ク'],
		[from:'け',   to:'ケ'],
		[from:'こ',   to:'コ'],
		[from:'さ',   to:'サ'],
		[from:'し',   to:'シ'],
		[from:'す',   to:'ス'],
		[from:'せ',   to:'セ'],
		[from:'そ',   to:'ソ'],
		[from:'た',   to:'タ'],
		[from:'ち',   to:'チ'],
		[from:'つ',   to:'ツ'],
		[from:'て',   to:'テ'],
		[from:'と',   to:'ト'],
		[from:'な',   to:'ナ'],
		[from:'に',   to:'ニ'],
		[from:'ぬ',   to:'ヌ'],
		[from:'ね',   to:'ネ'],
		[from:'の',   to:'ノ'],
		[from:'は',   to:'ハ'],
		[from:'ひ',   to:'ヒ'],
		[from:'ふ',   to:'フ'],
		[from:'へ',   to:'ヘ'],
		[from:'ほ',   to:'ホ'],
		[from:'ま',   to:'マ'],
		[from:'み',   to:'ミ'],
		[from:'む',   to:'ム'],
		[from:'め',   to:'メ'],
		[from:'も',   to:'モ'],
		[from:'や',   to:'ヤ'],
		[from:'ゆ',   to:'ユ'],
		[from:'よ',   to:'ヨ'],
		[from:'ら',   to:'ラ'],
		[from:'り',   to:'リ'],
		[from:'ゃ',   to:'ャ'],
		[from:'ゅ',   to:'ュ'],
		[from:'ょ',   to:'ョ'],
		[from:'る',   to:'ル'],
		[from:'れ',   to:'レ'],
		[from:'ろ',   to:'ロ'],
		[from:'わ',   to:'ワ'],
		[from:'を',   to:'ヲ'],
		[from:'ん',   to:'ン'],
		[from:'が',   to:'ガ'],
		[from:'ぎ',   to:'ギ'],
		[from:'ぐ',   to:'グ'],
		[from:'げ',   to:'ゲ'],
		[from:'ご',   to:'ゴ'],
		[from:'ざ',   to:'ザ'],
		[from:'じ',   to:'ジ'],
		[from:'ず',   to:'ズ'],
		[from:'ぜ',   to:'ゼ'],
		[from:'ぞ',   to:'ゾ'],
		[from:'だ',   to:'ダ'],
		[from:'ぢ',   to:'ヂ'],
		[from:'づ',   to:'ヅ'],
		[from:'で',   to:'デ'],
		[from:'ど',   to:'ド'],
		[from:'ば',   to:'バ'],
		[from:'び',   to:'ビ'],
		[from:'ぶ',   to:'ブ'],
		[from:'べ',   to:'ベ'],
		[from:'ぼ',   to:'ボ'],
		[from:'ぱ',   to:'パ'],
		[from:'ぴ',   to:'ピ'],
		[from:'ぷ',   to:'ブ'],
		[from:'ぺ',   to:'ペ'],
		[from:'ぽ',   to:'ポ'],
		[from:'う゛',   to:'ヴ'],
	]
	
	/**
	 * 変換テーブルを探し
	 * 見つかった場合変換後の内容を返却
	 * 見つからなかった場合そのまま返却
	 */
	def hira2kata(String a) {
		return a.collect{ c ->
			def map = this.maps.find{ it.from == c }
			map ? map.to : c
		}.join()
	}
}

assert new Converter().hira2kata("あぜるばいじゃん") == "アゼルバイジャン"