Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions lib/romanize.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,135 @@
module Romanize

DICT = {
"あ" => "a",
"い"=> "i",
"う"=> "u",
"え"=> "e",
"お"=> "o",
"か"=> "ka",
"き"=> "ki",
"く"=> "ku",
"け"=> "ke",
"こ"=> "ko",
"きゃ"=> "kya",
"きゅ"=> "kyu",
"きょ"=> "kyo",
"さ"=> "sa",
"し"=> "si",
"す"=> "su",
"せ"=> "se",
"そ"=> "so",
"しゃ"=> "sya",
"しゅ"=> "syu",
"しょ"=> "syo",
"た"=> "ta",
"ち"=> "ti",
"つ"=> "tu",
"て"=> "te",
"と"=> "to",
"ちゃ"=> "tya",
"ちゅ"=> "tyu",
"ちょ"=> "tyo",
"な"=> "na",
"に"=> "ni",
"ぬ"=> "nu",
"ね"=> "ne",
"の"=> "no",
"にゃ"=> "nya",
"にゅ"=> "nyu",
"にょ"=> "nyo",
"は"=> "ha",
"ひ"=> "hi",
"ふ"=> "hu",
"へ"=> "he",
"ほ"=> "ho",
"ひゃ"=> "hya",
"ひゅ"=> "hyu",
"ひょ"=> "hyo",
"ま"=> "ma",
"み"=> "mi",
"む"=> "mu",
"め"=> "me",
"も"=> "mo",
"みゃ"=> "mya",
"みゅ"=> "myu",
"みょ"=> "myo",
"や"=> "ya",
"ゆ"=> "yu",
"よ"=> "yo",
"ら"=> "ra",
"り"=> "ri",
"る"=> "ru",
"れ"=> "re",
"ろ"=> "ro",
"りゃ"=> "rya",
"りゅ"=> "ryu",
"りょ"=> "ryo",
"わ"=> "wa",
"ゐ"=> "wi",
"ゑ"=> "we",
"を"=> "wo",
"ん"=> "n",
"が"=> "ga",
"ぎ"=> "gi",
"ぐ"=> "gu",
"げ"=> "ge",
"ご"=> "go",
"ぎゃ"=> "gya",
"ぎゅ"=> "gyu",
"ぎょ"=> "gyo",
"ざ"=> "za",
"じ"=> "zi",
"ず"=> "zu",
"ぜ"=> "ze",
"ぞ"=> "zo",
"じゃ"=> "zya",
"じゅ"=> "zyu",
"じょ"=> "zyo",
"だ"=> "da",
"ぢ"=> "di",
"づ"=> "du",
"で"=> "de",
"ど"=> "do",
"ぢゃ"=> "dya",
"ぢゅ"=> "dyu",
"ぢょ"=> "dyo",
"ば"=> "ba",
"び"=> "bi",
"ぶ"=> "bu",
"べ"=> "be",
"ぼ"=> "bo",
"びゃ"=> "bya",
"びゅ"=> "byu",
"びょ"=> "byo",
"ぱ"=> "pa",
"ぴ"=> "pi",
"ぷ"=> "pu",
"ぺ"=> "pe",
"ぽ"=> "po",
"ぴゃ"=> "pya",
"ぴゅ"=> "pyu",
"ぴょ"=> "pyo",
"くゎ"=> "kwa",
"ぐゎ"=> "gwa"
}

def romanize(s)
result = []
sokuon_found = false
s.each_char do |c|
if c == "っ"
sokuon_found = true
next
end
if sokuon_found
tmp = DICT[c]
result << (tmp[0] + tmp)
sokuon_found = false
else
result << DICT[c]
end
end
return result.join
end
end
11 changes: 10 additions & 1 deletion test/test_romanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ def test_romanize_shu
assert_equal "sinzyuku", romanize("しんじゅく")
end

def test_romanize_sokuon
def test_romanize_sokuon1
assert_equal "nattou", romanize("なっとう")
end

def test_romanize_sokuon2
assert_equal "gakkou", romanize("がっこう")
end

def test_romanize_sokuon3
assert_equal "kekka", romanize("けっか")
end

end