Module:ko-translit/sandbox

From Wiktionary, the free dictionary
Jump to navigation Jump to search


local export = {}
local gsub = mw.ustring.gsub
local match = mw.ustring.match

function export.tr(word, lang, sc)
	word = gsub(word, "%<%/?r[pt]%>", "")
	word = gsub(word, "%<%/?ruby%>", "")
	-- 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
	
	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