Module:Kana[edit | edit source]


-- This module converts between Japanese hiragana and katakana

local kanaData = mw.loadData('Module:Kana/data')
local p = {}

local function makeSingleCharConversionTable(inputKey, outputKey)
	local ret = {}
	for i, singleCharData in ipairs(kanaData.singleChars) do
		ret[singleCharData[inputKey]] = singleCharData[outputKey]
	end
	return ret
end

local function convertString(s, inputKey, outputKey)
	-- Convert double chars
	for i, doubleCharData in ipairs(kanaData.doubleChars) do
		s = mw.ustring.gsub(s, doubleCharData[inputKey], doubleCharData[outputKey])
	end

	-- Convert single chars
	s = mw.ustring.gsub(s, '.', makeSingleCharConversionTable(inputKey, outputKey))

	return s
end

local function cleanUpRomaji(s)
	-- Format long vowels the Hepburn way
	s = s:gsub('aa', 'ā')
	s = s:gsub('o[ou]', 'ō')
	s = s:gsub('uu', 'ū')
	s = s:gsub('ii', 'ī')

	-- Fix small tsu before consonants that aren't "t"
	s = s:gsub('t([kgsjdfhbpmnry])', '%1%1')
	s = s:gsub('ssu', 'tsu') -- Special case for "tsu" which became "ssu" on the line above

	-- Convert full-width spaces to half-width
	s = s:gsub(' ', ' ')

	return s
end

function p._htok(s)
	return convertString(s, 'hiragana', 'katakana')
end

function p._ktoh(s)
	return convertString(s, 'katakana', 'hiragana')
end

function p._htor(s)
	s = convertString(s, 'hiragana', 'romaji')
	return cleanUpRomaji(s)
end

function p._ktor(s)
	s = convertString(s, 'katakana', 'romaji')
	return cleanUpRomaji(s)
end

return setmetatable(p, {
	__index = function (t, key)
		return function (frame)
			local args = require('Module:Arguments').getArgs(frame, {
				wrappers = 'Template:Kana',
			})
			return p['_' .. key](args[1])
		end
	end
})

Module:Kana/data[edit | edit source]


return {
	singleChars = {
		{
			hiragana = 'あ',
			katakana = 'ア',
			romaji   = 'a',
		},
		{
			hiragana = 'ぃ',
			katakana = 'ィ',
			romaji   = 'i',
		},
		{
			hiragana = 'い',
			katakana = 'イ',
			romaji   = 'i',
		},
		{
			hiragana = 'ぅ',
			katakana = 'ゥ',
			romaji   = 'u',
		},
		{
			hiragana = 'う',
			katakana = 'ウ',
			romaji   = 'u',
		},
		{
			hiragana = 'ぇ',
			katakana = 'ェ',
			romaji   = 'e',
		},
		{
			hiragana = 'え',
			katakana = 'エ',
			romaji   = 'e',
		},
		{
			hiragana = 'ぉ',
			katakana = 'ォ',
			romaji   = 'o',
		},
		{
			hiragana = 'お',
			katakana = 'オ',
			romaji   = 'o',
		},
		{
			hiragana = 'か',
			katakana = 'カ',
			romaji   = 'ka',
		},
		{
			hiragana = 'が',
			katakana = 'ガ',
			romaji   = 'ga',
		},
		{
			hiragana = 'き',
			katakana = 'キ',
			romaji   = 'ki',
		},
		{
			hiragana = 'ぎ',
			katakana = 'ギ',
			romaji   = 'gi',
		},
		{
			hiragana = 'く',
			katakana = 'ク',
			romaji   = 'ku',
		},
		{
			hiragana = 'ぐ',
			katakana = 'グ',
			romaji   = 'gu',
		},
		{
			hiragana = 'け',
			katakana = 'ケ',
			romaji   = 'ke',
		},
		{
			hiragana = 'げ',
			katakana = 'ゲ',
			romaji   = 'ge',
		},
		{
			hiragana = 'こ',
			katakana = 'コ',
			romaji   = 'ko',
		},
		{
			hiragana = 'ご',
			katakana = 'ゴ',
			romaji   = 'go',
		},
		{
			hiragana = 'さ',
			katakana = 'サ',
			romaji   = 'sa',
		},
		{
			hiragana = 'ざ',
			katakana = 'ザ',
			romaji   = 'za',
		},
		{
			hiragana = 'し',
			katakana = 'シ',
			romaji   = 'shi',
		},
		{
			hiragana = 'じ',
			katakana = 'ジ',
			romaji   = 'ji',
		},
		{
			hiragana = 'す',
			katakana = 'ス',
			romaji   = 'su',
		},
		{
			hiragana = 'ず',
			katakana = 'ズ',
			romaji   = 'zu',
		},
		{
			hiragana = 'せ',
			katakana = 'セ',
			romaji   = 'se',
		},
		{
			hiragana = 'ぜ',
			katakana = 'ゼ',
			romaji   = 'ze',
		},
		{
			hiragana = 'そ',
			katakana = 'ソ',
			romaji   = 'so',
		},
		{
			hiragana = 'ぞ',
			katakana = 'ゾ',
			romaji   = 'zo',
		},
		{
			hiragana = 'た',
			katakana = 'タ',
			romaji   = 'ta',
		},
		{
			hiragana = 'だ',
			katakana = 'ダ',
			romaji   = 'da',
		},
		{
			hiragana = 'ち',
			katakana = 'チ',
			romaji   = 'chi',
		},
		{
			hiragana = 'ぢ',
			katakana = 'ヂ',
			romaji   = 'ji',
		},
		{
			hiragana = 'っ',
			katakana = 'ッ',
			romaji   = 't',
		},
		{
			hiragana = 'つ',
			katakana = 'ツ',
			romaji   = 'tsu',
		},
		{
			hiragana = 'づ',
			katakana = 'ヅ',
			romaji   = 'zu',
		},
		{
			hiragana = 'て',
			katakana = 'テ',
			romaji   = 'te',
		},
		{
			hiragana = 'で',
			katakana = 'デ',
			romaji   = 'de',
		},
		{
			hiragana = 'と',
			katakana = 'ト',
			romaji   = 'to',
		},
		{
			hiragana = 'ど',
			katakana = 'ド',
			romaji   = 'do',
		},
		{
			hiragana = 'な',
			katakana = 'ナ',
			romaji   = 'na',
		},
		{
			hiragana = 'に',
			katakana = 'ニ',
			romaji   = 'ni',
		},
		{
			hiragana = 'ぬ',
			katakana = 'ヌ',
			romaji   = 'nu',
		},
		{
			hiragana = 'ね',
			katakana = 'ネ',
			romaji   = 'ne',
		},
		{
			hiragana = 'の',
			katakana = 'ノ',
			romaji   = 'no',
		},
		{
			hiragana = 'は',
			katakana = 'ハ',
			romaji   = 'ha',
		},
		{
			hiragana = 'ば',
			katakana = 'バ',
			romaji   = 'ba',
		},
		{
			hiragana = 'ぱ',
			katakana = 'パ',
			romaji   = 'pa',
		},
		{
			hiragana = 'ひ',
			katakana = 'ヒ',
			romaji   = 'hi',
		},
		{
			hiragana = 'び',
			katakana = 'ビ',
			romaji   = 'bi',
		},
		{
			hiragana = 'ぴ',
			katakana = 'ピ',
			romaji   = 'pi',
		},
		{
			hiragana = 'ふ',
			katakana = 'フ',
			romaji   = 'fu',
		},
		{
			hiragana = 'ぶ',
			katakana = 'ブ',
			romaji   = 'bu',
		},
		{
			hiragana = 'ぷ',
			katakana = 'プ',
			romaji   = 'pu',
		},
		{
			hiragana = 'へ',
			katakana = 'ヘ',
			romaji   = 'he',
		},
		{
			hiragana = 'べ',
			katakana = 'ベ',
			romaji   = 'be',
		},
		{
			hiragana = 'ぺ',
			katakana = 'ペ',
			romaji   = 'pe',
		},
		{
			hiragana = 'ほ',
			katakana = 'ホ',
			romaji   = 'ho',
		},
		{
			hiragana = 'ぼ',
			katakana = 'ボ',
			romaji   = 'bo',
		},
		{
			hiragana = 'ぽ',
			katakana = 'ポ',
			romaji   = 'po',
		},
		{
			hiragana = 'ま',
			katakana = 'マ',
			romaji   = 'ma',
		},
		{
			hiragana = 'み',
			katakana = 'ミ',
			romaji   = 'mi',
		},
		{
			hiragana = 'む',
			katakana = 'ム',
			romaji   = 'mu',
		},
		{
			hiragana = 'め',
			katakana = 'メ',
			romaji   = 'me',
		},
		{
			hiragana = 'も',
			katakana = 'モ',
			romaji   = 'mo',
		},
		{
			hiragana = 'ゃ',
			katakana = 'ャ',
			romaji   = 'ya',
		},
		{
			hiragana = 'や',
			katakana = 'ヤ',
			romaji   = 'ya',
		},
		{
			hiragana = 'ゅ',
			katakana = 'ュ',
			romaji   = 'yu',
		},
		{
			hiragana = 'ゆ',
			katakana = 'ユ',
			romaji   = 'yu',
		},
		{
			hiragana = 'ょ',
			katakana = 'ョ',
			romaji   = 'yo',
		},
		{
			hiragana = 'よ',
			katakana = 'ヨ',
			romaji   = 'yo',
		},
		{
			hiragana = 'ら',
			katakana = 'ラ',
			romaji   = 'ra',
		},
		{
			hiragana = 'り',
			katakana = 'リ',
			romaji   = 'ri',
		},
		{
			hiragana = 'る',
			katakana = 'ル',
			romaji   = 'ru',
		},
		{
			hiragana = 'れ',
			katakana = 'レ',
			romaji   = 're',
		},
		{
			hiragana = 'ろ',
			katakana = 'ロ',
			romaji   = 'ro',
		},
		{
			hiragana = 'ゎ',
			katakana = 'ヮ',
			romaji   = 'wa',
		},
		{
			hiragana = 'わ',
			katakana = 'ワ',
			romaji   = 'wa',
		},
		{
			hiragana = 'ゐ',
			katakana = 'ヰ',
			romaji   = 'wi',
		},
		{
			hiragana = 'ゑ',
			katakana = 'ヱ',
			romaji   = 'we',
		},
		{
			hiragana = 'を',
			katakana = 'ヲ',
			romaji   = 'wo',
		},
		{
			hiragana = 'ん',
			katakana = 'ン',
			romaji   = 'n',
		},
		{
			hiragana = 'ゔ',
			katakana = 'ヴ',
			romaji   = 'vu',
		},
		{
			hiragana = 'ゕ',
			katakana = 'ヵ',
			romaji   = 'ka',
		},
		{
			hiragana = 'ゖ',
			katakana = 'ヶ',
			romaji   = 'ke',
		},
	},
	doubleChars = {
		{
			hiragana = 'ふぁ',
			katakana = 'ファ',
			romaji   = 'fa',
		},
		{
			hiragana = 'ヴぁ',
			katakana = 'ヴァ',
			romaji   = 'va',
		},
		{
			hiragana = 'てぃ',
			katakana = 'ティ',
			romaji   = 'ti',
		},
		{
			hiragana = 'でぃ',
			katakana = 'ディ',
			romaji   = 'di',
		},
		{
			hiragana = 'ふぃ',
			katakana = 'フィ',
			romaji   = 'fi',
		},
		{
			hiragana = 'ちぇ',
			katakana = 'チェ',
			romaji   = 'che',
		},
		{
			hiragana = 'ふぇ',
			katakana = 'フェ',
			romaji   = 'fe',
		},
		{
			hiragana = 'ふぉ',
			katakana = 'フォ',
			romaji   = 'fo',
		},
		{
			hiragana = 'きゃ',
			katakana = 'キャ',
			romaji   = 'kya',
		},
		{
			hiragana = 'きゅ',
			katakana = 'キュ',
			romaji   = 'kyu',
		},
		{
			hiragana = 'きょ',
			katakana = 'キョ',
			romaji   = 'kyo',
		},
		{
			hiragana = 'しゃ',
			katakana = 'シャ',
			romaji   = 'sha',
		},
		{
			hiragana = 'しゅ',
			katakana = 'シュ',
			romaji   = 'shu',
		},
		{
			hiragana = 'しょ',
			katakana = 'ショ',
			romaji   = 'sho',
		},
		{
			hiragana = 'じゃ',
			katakana = 'ジャ',
			romaji   = 'ja',
		},
		{
			hiragana = 'じゅ',
			katakana = 'ジュ',
			romaji   = 'ju',
		},
		{
			hiragana = 'じょ',
			katakana = 'ジョ',
			romaji   = 'jo',
		},
		{
			hiragana = 'ちゃ',
			katakana = 'チャ',
			romaji   = 'cha',
		},
		{
			hiragana = 'ちゅ',
			katakana = 'チュ',
			romaji   = 'chu',
		},
		{
			hiragana = 'ちょ',
			katakana = 'チョ',
			romaji   = 'cho',
		},
		{
			hiragana = 'ひゃ',
			katakana = 'ヒャ',
			romaji   = 'hya',
		},
		{
			hiragana = 'ひゅ',
			katakana = 'ヒュ',
			romaji   = 'hyu',
		},
		{
			hiragana = 'ひょ',
			katakana = 'ヒョ',
			romaji   = 'hyo',
		},
		{
			hiragana = 'ぴゃ',
			katakana = 'ピャ',
			romaji   = 'pya',
		},
		{
			hiragana = 'ぴゅ',
			katakana = 'ピュ',
			romaji   = 'pyu',
		},
		{
			hiragana = 'ぴょ',
			katakana = 'ピョ',
			romaji   = 'pyo',
		},
		{
			hiragana = 'みゃ',
			katakana = 'ミャ',
			romaji   = 'mya',
		},
		{
			hiragana = 'みゅ',
			katakana = 'ミュ',
			romaji   = 'myu',
		},
		{
			hiragana = 'みょ',
			katakana = 'ミョ',
			romaji   = 'myo',
		},
		{
			hiragana = 'にゃ',
			katakana = 'ニャ',
			romaji   = 'nya',
		},
		{
			hiragana = 'にゅ',
			katakana = 'ニュ',
			romaji   = 'nyu',
		},
		{
			hiragana = 'にょ',
			katakana = 'ニョ',
			romaji   = 'nyo',
		},
		{
			hiragana = 'りゃ',
			katakana = 'リャ',
			romaji   = 'rya',
		},
		{
			hiragana = 'りゅ',
			katakana = 'リュ',
			romaji   = 'ryu',
		},
		{
			hiragana = 'りょ',
			katakana = 'リョ',
			romaji   = 'ryo',
		},
	}
}

Module: Kana/doc[edit | edit source]


Module: Kana/data/doc[edit | edit source]


Attribution[edit | edit source]

  • User:Mr._Stradivarius@En.Wikipedia.Org in July 2017
Community content is available under CC-BY-SA unless otherwise noted.