이 모듈에 대한 설명문서는 모듈:ko/설명문서에서 만들 수 있습니다

local m_str_utils = require("Module:string utilities")

local m = {}

local codepoint = m_str_utils.codepoint
local concat = table.concat
local floor = math.floor
local gmatch = m_str_utils.gmatch
local gsplit = m_str_utils.gsplit
local gsub = m_str_utils.gsub
local insert = table.insert
local len = m_str_utils.len
local match = m_str_utils.match
local min = math.min
local remove = table.remove
local sub = m_str_utils.sub
local u = m_str_utils.char
local upper = m_str_utils.upper

local lang = require("Module:languages").getByCode("ko")
m.lang = lang
local HangChars = require("Module:scripts").getByCode("Hang"):getCharacters()
local HaniChars = require("Module:scripts").getByCode("Hani"):getCharacters()

-- 한자 탭(ko-hanjatab)을 자동으로 생성
function m.hanjatab()
	local hanja = gsub(mw.title.getCurrentTitle().text, '[^' .. HaniChars .. ']', '')
	local common_style = 'color: var(--text-color); background: var(--background);'
	local table_head = '<table class="floatright wikitable" style="text-align:center; font-size:small;' .. common_style .. '"><tr><th colspan="' .. 
		len(hanja) .. 
		'" style="font-weight:normal;">이 단어에 사용되는 [[한자]]</th></tr><tr lang="ko" class="Kore" style="font-size:2em;' .. common_style .. ' line-height:1em;">'
	return table_head .. 
		gsub(hanja, '(.)', '<td style="padding:0.5em;' .. common_style .. '">[[%1#한국어|%1]]</td>') .. 
		'</tr></table>'
end

-- return only non-hangeul contained in text
function m.remove_hangeul(f)
	local nonhangeul = mw.ustring.gsub(f.args[1], '[가-힣]', '')
	return nonhangeul
end

function m.boldify(f)
	local pagename = mw.title.getCurrentTitle().text
	hangul = f.args[1]
	if match(hangul, pagename) and not match(hangul, "'") then
		hangul = gsub(hangul, pagename, "'''" .. pagename .. "'''")
	end
	return hangul
end

function m.usex_hangul(f)
	local pagename = mw.title.getCurrentTitle().text
	hangul = f.args[1]
	if match(hangul, pagename) and not match(hangul, "'") then
		hangul = gsub(hangul, pagename, "'''" .. pagename .. "'''")
	end
	i = 1
	local front, back = '<span style="background&#45;color:#FEF8EA"><b>', '</b></span>'
	for bold in mw.ustring.gmatch(hangul, "'''") do
		hangul = gsub(hangul, "'''", (i % 2 == 1 and front or back), 1)
		i = i + 1
	end
	hangul = gsub(hangul, '[%^%-]', '')
	return hangul
end

function m.link(frame)
    local arg = frame:getParent().args
    local args = {}
    local word, definition, hanja, note = false, false, false, false
    
    for i = 1, 4, 1 do
        if arg[i] and arg[i] ~= "" then
            table.insert(args, arg[i])
        end
    end

    local curr_hangul_level, closest_hangul = 0, 0
    for i, parameter in ipairs(args) do
        local _, tentative_hangul_level = gsub(parameter, "[가-힣]", "")
        if tentative_hangul_level > curr_hangul_level then
            curr_hangul_level = tentative_hangul_level
            closest_hangul = i
        end
    end

    if curr_hangul_level > 0 then
        word = args[closest_hangul]
        table.remove(args, closest_hangul)
    end
    
    if arg[5] and arg[5] ~= "" then
        note = arg[5]
    end
    
    if arg["gloss"] then
        definition = arg["gloss"]
        arg["gloss"] = nil
    end
    
    for i, parameter in ipairs(args) do
        if match(parameter, "[㐀-䶵一-鿌]") then
            hanja = parameter
            table.remove(args, i)
        end
    end
    
    if not hanja and not word then
        word = args[1]
        table.remove(args, 1)
    end
    
    if #args > 0 then
        definition = args[1]
    end
    
    if hanja and not match(hanja, "[%[%]]") then
        for hanja_word in mw.ustring.gmatch(hanja, "[㐀-䶵一-鿌]+") do
            hanja = gsub(hanja, hanja_word, "[[" .. hanja_word .. "]]")
        end
        for hangul_word in mw.ustring.gmatch(hanja, "[가-힣]+") do
            hanja = gsub(hanja, hangul_word, "[[" .. hangul_word .. "]]")
        end
        hanja = gsub(hanja, "%[%[%[%[", "[[")
        hanja = gsub(hanja, "%]%]%]%]", "]]")
    end
    
    if definition then
        if not match(definition, "^''.+''$") then definition = "“" .. definition .. "”" end
    end
    
    word = gsub(word, "%^", "")
    
    if not match(word, "[%[%]]") then
        if match(word, "^—.+—$") then
            word = gsub(word, "—(.+)—", "—[[%1]]—")
            
        elseif match(word, "^—.+$") then
            word = gsub(word, "—(.+)", "—[[%1]]")
            
        elseif match(word, "^.+—$") then
            word = gsub(word, "(.+)—", "[[%1다|%1—]]")
        
        elseif match(word, "^.+–$") then
            word = gsub(word, "(.+)–", "[[%1]]—")
        
        elseif match(word, "^%*") then
            word = gsub(word, "%*", "")
        
        else
            word = "[[" .. word .. "]]"
        end
    end
    
    local info = {}
    table.insert(info, word and (hanja or nil) or nil)
    table.insert(info, definition or nil)

    local result = word
        and
            ("<span lang=\"ko\" class=\"Hang\">" .. word .. "</span>")
        or
            ('<span lang="ko" class="Hani">' .. hanja .. '</span>')
    
    -- ko-pron 모듈의 인수들을 수집
    local pron_args = {}
    local pron_params = { "nn", "l", "com", "cap", "ui", "uie", "nobc", "ni", "bcred", "svar", "iot" }
    for _, param in ipairs(pron_params) do
        if arg[param] then
            pron_args[param] = arg[param]
        end
    end

    -- 발음 결과 추가 (괄호 유지, 위키링크 제거)
    if word then
        local m_pron = require("Module:ko-pron")
        pron_args[1] = word -- word 인수 추가
        local pron_result = m_pron.hangul_pron(word, pron_args)
        result = result .. " (" .. pron_result .. ")"
    end
    
    if #info > 0 then
        result = result .. " (" .. table.concat(info, ", ") .. ")"
    end
    
    if note then
        result = result .. " (<i>" .. note .. "</i>)"
    end
    
    return result
end

function m.new(frame)
	local title = mw.title.getCurrentTitle().text
	local args = frame:getParent().args
	local pos = args[1] or ""
	local def = args[2] or "{{rfdef|lang=ko}}"
	local pos2 = args[3] or (args[4] and "" or false)
	local def2 = args[4] or "{{rfdef|lang=ko}}"
	local pos3 = args[5] or (args[6] and "" or false)
	local def3 = args[6] or "{{rfdef|lang=ko}}"
	local etym = args["e"] or false
	local head = args["head"] or false
	local cat = args["cat"] or false
	local image = args["pic"] or false
	local pedia = args["wp"] or false
	local irreg = args["irreg"] or false
	
	local result = ""
	
	local function genTitle(text)
		local pos_title = {
			[""] = "Noun", ["n"] = "Noun", ["pn"] = "Proper noun", ["propn"] = "Proper noun", ["pron"] = "Pronoun",
			["v"] = "Verb", ["a"] = "Adjective", ["adj"] = "Adjective", ["adv"] = "Adverb",
			["prep"] = "Preposition", ["postp"] = "Postposition", ["conj"] = "Conjunction",
			["part"] = "Particle", ["suf"] = "Suffix",
			["prov"] = "Proverb", ["id"] = "Idiom", ["ph"] = "Phrase", ["intj"] = "Interjection", ["interj"] = "Interjection",
			["cl"] = "Classifier", ["cls"] = "Classifier", ["num"] = "Numeral", ["abb"] = "Abbreviation",
			["det"] = "Determiner", ["deter"] = "Determiner", ["root"] = "Root",
		};
		return pos_title[text] or mw.ustring.upper(mw.ustring.sub(text, 1, 1)) .. mw.ustring.sub(text, 2, -1)
	end
	
	local function genHead(text)
		local pos_head = {
			[""] = "noun", ["n"] = "noun", ["pn"] = "proper noun", ["propn"] = "proper noun", ["v"] = "verb", ["a"] = "adj",
			["postp"] = "pos|post", ["conj"] = "pos|con", ["part"] = "pos|particle", ["pron"] = "pos|pronoun",
			["prov"] = "proverb", ["id"] = "pos|idiom", ["ph"] = "pos|phrase", ["intj"] = "interj",
			["abb"] = "pos|abbr", ["cl"] = "pos|cls", ["det"] = "det", ["deter"] = "det", ["root"] = "root", ["num"] = "num",
		};
		return pos_head[text] or "pos|" .. text
	end
	
	local function other(class, title, args, level)
		local code = ""
		if args[class] then
			code = code .. "\n\n" .. level .. title .. level .. "\n* {{ko-l|" .. args[class] .. "}}"
			i = 2
			while args[class .. i] do
				code = code .. "\n* {{ko-l|" .. args[class .. i] .. "}}"
				i = i + 1
			end
		end
		return code
	end
	
	if args["2e"] or args["2h"] or args["2nat"] or args["2ee"] or args["2c1"] or args["2p"] or args["multiEtym"] then
		multiEtym = true
	end
	
	local function iterate_param(args, genPos, etymNo)
		if genPos == "proper noun" then args[(etymNo > 1 and etymNo or "") .. "cap"] = "y" end
		text = ""
		for _, arg in ipairs( { "l", "com", "nn", "ui", "nobc", "cap", "ni", "bcred", "a" } ) do
			if etymNo > 1 then arg = etymNo .. arg end
			if args[arg] then text = text .. "|" .. arg .. "=" .. args[arg] end
		end
		return text
	end
	
	local function add_etym(args, etymNo)
		etymText = ""
		n = etymNo > 1 and etymNo or ""
		if args[n.."e"] then etymText = etymText .. args[n.."e"]
		elseif args[n.."h"] then etymText = etymText .. "{{ko-etym-sino|" .. args[n.."h"] .. (args[n.."he"] and "|" .. args[n.."he"] or "") .. "}}."
		elseif args[n.."nat"] then etymText = etymText .. "{{ko-etym-native|" .. gsub(args[n.."nat"], ",", "|") .. "}}"
		elseif args[n.."ee"] then etymText = etymText .. "{{bor|ko|" .. (args[n.."el"] or "en") .. "|" .. args[n.."ee"] .. "}}."
		elseif args[n.."c1"] then etymText = etymText .. 
			"{{ko-l|" .. args[n.."c1"] .. (args[n.."hj1"] and "|" .. args[n.."hj1"] or "") .. (args[n.."t1"] and "|" .. args[n.."t1"] or "") .. "}}" ..
			" + {{ko-l|" .. args[n.."c2"] .. (args[n.."hj2"] and "|" .. args[n.."hj2"] or "") .. (args[n.."t2"] and "|" .. args[n.."t2"] or "") .. "}}" ..
			(args[n.."c3"] and " + {{ko-l|" .. args[n.."c3"] .. (args[n.."hj3"] and "|" .. args[n.."hj3"] or "") .. (args[n.."t3"] and "|" .. args[n.."t3"] or "") .. "}}" or "") .. 
			(args[n.."c4"] and " + {{ko-l|" .. args[n.."c4"] .. (args[n.."hj4"] and "|" .. args[n.."hj4"] or "") .. (args[n.."t4"] and "|" .. args[n.."t4"] or "") .. "}}" or "")
		elseif mw.ustring.match(title, "[하되]다$") then
			etymText = etymText .. "From " .. (args["c1r"] and "the root " or "") .. "{{ko-l|" .. mw.ustring.sub(title, 1, -3) .. (args[n.."hj1"] and "|" .. args[n.."hj1"] or "") .. (args[n.."t1"] and "|" .. args[n.."t1"] or "") .. "}}" .. 
			" + the suffix {{ko-l|—" .. mw.ustring.sub(title, -2, -1) .. "}}."
		elseif mw.ustring.match(title, "시키다$") or mw.ustring.match(title, "스럽다$") then
			etymText = etymText .. "From " .. (args["c1r"] and "the root " or "") .. "{{ko-l|" .. mw.ustring.sub(title, 1, -4) .. (args[n.."hj1"] and "|" .. args[n.."hj1"] or "") .. (args[n.."t1"] and "|" .. args[n.."t1"] or "") .. "}}" .. 
			" + the suffix {{ko-l|—" .. mw.ustring.sub(title, -3, -1) .. "}}."
		end
		return etymText
	end
	
	result = result .. "==Korean=="
	if wp then result = result .. "\n{{wp|lang=ko" .. (pedia ~= "y" and "|" .. pedia or "") .. "}}" end
	if image then result = result .. "\n[[File:" .. image .. "|thumb|right|300px|" .. title .. ".]]" end
	result = result .. other("alt", "Alternative forms", args, "===")
	
	if mw.ustring.match(title, "[하되]다$") or mw.ustring.match(title, "시키다$") or mw.ustring.match(title, "스럽다$") then autoEtym = true end
	if args["e"] or args["h"] or args["nat"] or args["ee"] or args["c1"] or autoEtym then
		etym = "\n\n===Etymology" .. (multiEtym and " 1" or "") .. "===\n"
		etym = etym .. add_etym(args, 1)
	end
	
	if etym then result = result .. etym end
	level = multiEtym and "====" or "==="
	result = result .. other("1alt", "Alternative forms", args, "====")
	
	result = result .. "\n\n" .. level .. "Pronunciation" .. level ..
	"\n{{ko-IPA" .. iterate_param(args, genHead(pos), 1) .. "}}"
	if genHead(pos) == "root" then def = "{{ko-root of|" .. def .. "}}" end
	result = result .. "\n\n" .. level .. genTitle(pos) .. level .. "\n{{ko-" .. genHead(pos) ..
	(head and ("|head=" .. head) or "") .. (args["h"] and ("|hanja=" .. args["h"]) or "") ..
	(irreg and "|irreg=y" or "") .. "}}\n\n# " .. def
	
	local function add_der(args, etymNo)
		n = etymNo > 1 and etymNo or ""
		local translDer = { ["h"] = "하다", ["d"] = "되다", ["s"] = "시키다" }
		if args[n .. "der"] and gsub(args[n .. "der"], "[sdh]", "") == "" then
			i = 1
			for ch in mw.text.gsplit(args[n .. "der"], "") do
				args[n .. "der" .. (i == 1 and "" or i)] = title .. translDer[ch]
				i = i + 1
			end
		end
		return args
	end
	
	args = add_der(args, 1)
	result = result .. other("syn", "=Synonyms=", args, level)
	result = result .. other("ant", "=Antonyms=", args, level)
	result = result .. other("der", "=Derived terms=", args, level)
	result = result .. other("rel", "=Related terms=", args, level)
	result = result .. other("also", "=See also=", args, level)
	if genHead(pos) == "adj" or genHead(pos) == "verb" then
		result = result .. "\n\n" .. level .. "Conjugation" .. level .. "\n{{ko-conj/" .. genHead(pos) .. (irreg and "|irreg=y" or "") .. "}}"
	end
	
	if pos2 then
		if multiEtym then
			result = result .. "\n\n===Etymology 2===\n" .. add_etym(args, 2)
			level = "===="
			result = result .. other("2alt", "Alternative forms", args, level)
			
			result = result .. "\n\n" .. level .. "Pronunciation" .. level .. 
			"\n{{ko-IPA" .. iterate_param(args, genHead(pos), 2) .. "}}"
			if genHead(pos2) == "root" then def2 = "{{ko-root of|" .. def2 .. "}}" end
			result = result .. "\n\n" .. level .. genTitle(pos2) .. level .. "\n{{ko-" .. genHead(pos2) ..
			(head and ("|head=" .. head) or "") .. (args["2h"] and ("|hanja=" .. args["2h"]) or "") .. "}}\n\n# " .. def2
			args = add_der(args, 2)
			result = result .. other("2syn", "=Synonyms=", args, level)
			result = result .. other("2ant", "=Antonyms=", args, level)
			result = result .. other("2der", "=Derived terms=", args, level)
			result = result .. other("2rel", "=Related terms=", args, level)
			result = result .. other("2also", "=See also=", args, level)
			if genHead(pos2) == "adj" or genHead(pos2) == "verb" then
				result = result .. "\n\n" .. level .. "Conjugation" .. level .. "\n{{ko-conj/" .. genHead(pos2) .. (irreg and "|irreg=y" or "") .. "}}"
			end

		else
			result = result .. "\n\n===" .. genTitle(pos2) .. "===\n{{ko-" .. genHead(pos2) ..
			(head and ("|head=" .. head) or "") .. (args["2h"] and ("|hanja=" .. args["2h"]) or "") .. 
			"}}\n\n# " .. def2
			if genHead(pos2) == "adj" or genHead(pos2) == "verb" then
				result = result .. "\n\n====Conjugation====\n{{ko-conj/" .. genHead(pos2) .. (irreg and "|irreg=y" or "") .. "}}"
			end
		end
	end
	
	if pos3 then
		if multiEtym then
			result = result .. "\n\n===Etymology 3===\n" .. add_etym(args, 3)
			level = "===="
			result = result .. other("3alt", "Alternative forms", args, level)
			
			result = result .. "\n\n" .. level .. "Pronunciation" .. level .. 
			"\n{{ko-IPA" .. iterate_param(args, genHead(pos), 3) .. "}}"
			if genHead(pos3) == "root" then def3 = "{{ko-root of|" .. def3 .. "}}" end
			result = result .. "\n\n" .. level .. genTitle(pos3) .. level .. "\n{{ko-" .. genHead(pos3) ..
			(head and ("|head=" .. head) or "") .. (args["3h"] and ("|hanja=" .. args["3h"]) or "") .. "}}\n\n# " .. def3
			args = add_der(args, 3)
			result = result .. other("3syn", "=Synonyms=", args, level)
			result = result .. other("3ant", "=Antonyms=", args, level)
			result = result .. other("3der", "=Derived terms=", args, level)
			result = result .. other("3rel", "=Related terms=", args, level)
			result = result .. other("3also", "=See also=", args, level)
			if genHead(pos3) == "adj" or genHead(pos3) == "verb" then
				result = result .. "\n\n" .. level .. "Conjugation" .. level .. "\n{{ko-conj/" .. genHead(pos3) .. (irreg and "|irreg=y" or "") .. "}}"
			end
		else
			result = result .. "\n\n===" .. genTitle(pos3) .. "===\n{{ko-" .. genHead(pos3) ..
			(head and ("|head=" .. head) or "") .. (args["3h"] and ("|hanja=" .. args["3h"]) or "") .. 
			"}}\n\n# " .. def3
			if genHead(pos3) == "adj" or genHead(pos3) == "verb" then
				result = result .. "\n\n====Conjugation====\n{{ko-conj/" .. genHead(pos3) .. (irreg and "|irreg=y" or "") .. "}}"
			end
		end
	end
	
	if cat then
		result = result .. "\n\n{{C|ko|" .. cat .. "}}"
	end
	
	return result
end

function m.decompose_jamo(syllable)
	if not match(syllable, "[가-힣]") then
		if match(syllable, "[ᄀ-ᄒ]") then return { initial = syllable, vowel = "Ø", final = "Ø" }
		elseif match(syllable, "[ᅡ-ᅵ]") then return { initial = "Ø", vowel = syllable, final = "Ø" }
		elseif match(syllable, "[ᆨ-ᇂ]") then return { initial = "Ø", vowel = "Ø", final = syllable }
		elseif match(syllable, "[ㄱ-ㆎ]") then return { initial = "Ø", vowel = "Ø", final = syllable }
		else return { initial = "Ø", vowel = " ", final = "X" } end
	end
	local char = mw.ustring.char
	local cp = mw.ustring.codepoint(syllable)
	if not cp then return { "", "", "" } end
	local relative_cp = cp - 0xAC00
	local jongseong = relative_cp % 28
	local jungseong = math.floor((relative_cp % 588) / 28)
	local choseong = math.floor(relative_cp / 588)
	choseong, jungseong, jongseong = 
		char(0x1100 + choseong), 
		char(0x1161 + jungseong), 
		jongseong ~= 0 and char(0x11A7 + jongseong) or ""
	return { initial = choseong, vowel = jungseong, final = jongseong }
end

-- 종성 판별 및 조사 선택 함수 추가; 분류명에 존재하는 이형태를 처리하기 위함
function m.allomorphy(word, type)
    local suffixes = {
        top = { final = "은", no_final = "는" },  -- 보조사
        sbj = { final = "이", no_final = "가" },  -- 주격 조사
        obj = { final = "을", no_final = "를" },  -- 목적격 조사
        conj = { final = "과", no_final = "와" }, -- 공동격 조사
        ins = { final = "으로", no_final = "로" } -- 도구격 조사
    }

    local particle = suffixes[type]
    if not particle then
        error("유효하지 않은 조사 형태입니다.")
    end

    local cp = codepoint(sub(word, -1))
    if not cp then return word end
    local relative_cp = cp - 0xAC00
    local jongseong = relative_cp % 28
    local has_final = jongseong ~= 0

    local suffix = has_final and particle.final or particle.no_final
    return word .. suffix
end

return m