Bước tới nội dung

Mô đun:ko-translit

Từ điển mở Wiktionary
local export = {}
local gsub = mw.ustring.gsub
local match = mw.ustring.match

function export.tr(word, lang, sc)
	-- remove hanja from (ex.) 사전(辭典) and 辭典(사전)
	-- Hani regex is a reasonable subset of Hani from [[Module:scripts/data]],
	-- last checked on 20220221
	word = gsub(word, "%([一-鿿㐀-䶿𠀀-𮯯𰀀-𱍏]+%)", "")
	word = gsub(word, "%([一-鿿㐀-䶿𠀀-𮯯𰀀-𱍏]*'''[一-鿿㐀-䶿𠀀-𮯯𰀀-𱍏]+'''[一-鿿㐀-䶿𠀀-𮯯𰀀-𱍏]*%)", "")
	word = gsub(word, "[一-鿿㐀-䶿𠀀-𮯯𰀀-𱍏]+%((.-)%)", "%1")

	-- transform em-dash to plain hyphen-minus
	word = gsub(word, "—", "-")
	
	if match(word, "^[ㄱㄲㄳㄴㄵㄶㄷㄸㄹㄺㄻㄼㄽㄾㄿㅀㅁㅂㅃㅄㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ%-]+$") then
		return (gsub(word,
			"[ㄱㄲㄳㄴㄵㄶㄷㄸㄹㄺㄻㄼㄽㄾㄿㅀㅁㅂㅃㅄㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ]", {
				["ㄱ"] = "g", ["ㄲ"] = "kk", ["ㄳ"] = "ks", ["ㄴ"] = "n", ["ㄵ"] = "nj", ["ㄶ"] = "nh", ["ㄷ"] = "d", ["ㄸ"] = "tt", ["ㄹ"] = "l", ["ㄺ"] = "lg",
				["ㄻ"] = "lm", ["ㄼ"] = "lb", ["ㄽ"] = "ls", ["ㄾ"] = "lt", ["ㄿ"] = "lp", ["ㅀ"] = "lh", ["ㅁ"] = "m", ["ㅂ"] = "b", ["ㅃ"] = "pp", ["ㅄ"] = "ps",
				["ㅅ"] = "s", ["ㅆ"] = "ss", ["ㅇ"] = "'", ["ㅈ"] = "j", ["ㅉ"] = "jj", ["ㅊ"] = "ch", ["ㅋ"] = "k", ["ㅌ"] = "t", ["ㅍ"] = "p", ["ㅎ"] = "h",
				["ㅏ"] = "a", ["ㅐ"] = "ae", ["ㅑ"] = "ya", ["ㅒ"] = "yae", ["ㅓ"] = "eo", ["ㅔ"] = "e", ["ㅕ"] = "yeo",
				["ㅖ"] = "ye", ["ㅗ"] = "o", ["ㅘ"] = "wa", ["ㅙ"] = "wae", ["ㅚ"] = "oe", ["ㅛ"] = "yo", ["ㅜ"] = "u",
				["ㅝ"] = "wo", ["ㅞ"] = "we", ["ㅟ"] = "wi", ["ㅠ"] = "yu", ["ㅡ"] = "eu", ["ㅢ"] = "ui", ["ㅣ"] = "i" }
		))
	end

	if not match(word, "[가-힣]") then
		return nil
	end

	-- transform compat jamo into a form [[Module:ko-pron]] can handle
	-- for [[-ㅂ니까]] [[-ㅁ둥]] etc.
	-- could be moved to [[Module:ko-pron]]
	if match(word, "%-[ㄱㄲㄳㄴㄵㄶㄷㄹㄺㄻㄼㄽㄾㄿㅀㅁㅂㅄㅅㅆㅇㅈㅊㅋㅌㅍㅎ]") then
		word = gsub(word,
			"[ㄱㄲㄳㄴㄵㄶㄷㄹㄺㄻㄼㄽㄾㄿㅀㅁㅂㅄㅅㅆㅇㅈㅊㅋㅌㅍㅎ]", {
				["ㄱ"] = "ᆨ", ["ㄲ"] = "ᆩ", ["ㄳ"] = "ᆪ", ["ㄴ"] = "ᆫ", ["ㄵ"] = "ᆬ", ["ㄶ"] = "ᆭ", ["ㄷ"] = "ᆮ", ["ㄹ"] = "ᆯ", ["ㄺ"] = "ᆰ",
				["ㄻ"] = "ᆱ", ["ㄼ"] = "ᆲ", ["ㄽ"] = "ᆳ", ["ㄾ"] = "ᆴ", ["ㄿ"] = "ᆵ", ["ㅀ"] = "ᆶ", ["ㅁ"] = "ᆷ", ["ㅂ"] = "ᆸ", ["ㅄ"] = "ᆹ",
				["ㅅ"] = "ᆺ", ["ㅆ"] = "ᆻ", ["ㅇ"] = "ᆼ", ["ㅈ"] = "ᆽ", ["ㅊ"] = "ᆾ", ["ㅋ"] = "ᆿ", ["ㅌ"] = "ᇀ", ["ㅍ"] = "ᇁ", ["ㅎ"] = "ᇂ" }
		)
	end
	
	local m_pron = require("Module:ko-pron")
	local revised = m_pron.romanise(word, 2, {}, true)
	
	if not revised then
		return nil
	end
	
	if match(revised, "[%.%?%!]") then
		revised = mw.ustring.upper(mw.ustring.sub(revised, 1, 1)) .. mw.ustring.sub(revised, 2, -1)
		revised = gsub(revised, "([%.%?%!]) ([a-z%'])", "%1 ^%2")
		revised = gsub(revised, "^%'%'%'", "'''^")
	end
	revised = gsub(revised, "([a-z])%-%'([a-z])", "%1-%2")
	revised = gsub(revised, "%^%'%'%'", "'''^")
	revised = gsub(revised, "%^%l", mw.ustring.upper)
	revised = gsub(revised, '%^', '')
	revised = gsub(revised, "%-'''%-", "'''-")
	revised = gsub(revised, "%-%-", "-")
	
	return revised
end

export.tr_revised = export.tr

return export