Module:Ko-translit/clean

Documentation for this module may be created at Module:Ko-translit/clean/doc

Documentation for this module may be created at Module:Ko-translit/clean/doc

local p = {}
local m_utils = require('Module:Ko-utils')
local find = mw.ustring.find
local gsub_iterate = m_utils.gsub_iterate

-- Initial input validations
function p.validate_input(text)
	if not m_utils.contains_hangul(text) then
		error("Input must contain Hangul")
	elseif m_utils.contains_reference(text) then
		error("Input cannot contain references")
	-- Note: If Yale support is added, remove below line and make overall romanization "N/A"
	elseif find(text, "[ᄓ-ᅠᅶ-ᆧᇃ-ᇿ〮〯ㅤ-ㆎꥠ-꥿ힰ-퟿]") then
		error("Input contains Hangul not supported by RR and MR")
	elseif find(text, "[ᄀ-ᄒ]") or find(text, "[ᅡ-ᅵᆨ-ᇂ]") then
		error("Do not input conjoining Hangul jamo directly")
	elseif find(text, "`%*") then
		error("Use *` instead of `*")
	elseif find(text, "@%*") then
		error("Use *@ instead of @*")
	elseif find(text, "%^[^가-힣]") then
		error("^ must be immediately followed by Hangul syllabic block")
	elseif find(text, "[^%*0-9A-Za-z]`") or find(text, "[^0-9A-Za-z]%*`") or find(text, "`[^가-깋다-딯바-빟자-짛]") then
		error("Found invalid sequence containing `")
	elseif find(text, "[^%*ㄹ가-힣]@") or find(text, "[^가-힣]%*@") or find(text, "%*@[^가-깋다-딯바-빟자-짛]") or find(text, "ㄹ@[^가-깋다-딯바-빟사-싷자-짛]") or find(text, "@[^가-깋다-딯라-맇바-빟사-싷야-얳여-옅옇-옣요-욯유-윶윸-윻이-잍잏자-짛하-힣]") or find(text, "[받붙]잇@[가-깋]") or find(text, "앞엣@것") or find(text, "한솥엣@밥") then
		error("Found invalid sequence containing @")
	elseif find(text, "[^가-힣]%$") or find(text, "%$[^아어에엔엘여요으은을음읍의이인일임입]") then
		error("Found invalid sequence containing $")
	elseif find(text, "%%$") then
		error("Remove final %")
	elseif find(text, "[ _][ _]") then
		error("No two or more consecutive space characters")
	elseif find(text, "^[%$%*@_`]") or find(text, "^%%[^_가-힣]") or find(text, "[ _]%*") or find(text, "%*[ %*%-_]") or find(text, "%-%*") or find(text, "[﷐-﷒]") or find(text, "%%_$") or find(text, "[%$%*@%^`]$") then
		error("Invalid input")
	end
end

-- Validation after processing personal names and removing links & markups (before decomposing Hangul)
-- some invalid sequences can only be detected after doing those
function p.validate_composed(text)
	if find(text, "[ _][ _]") then
		error("No two or more consecutive space characters")
	elseif find(text, "^[%$%*@_`]") or find(text, "[ _]%*") or find(text, "%*[ %*%-_]") or find(text, "%-%*") or find(text, "[%$%*@%^_`]$") then
		error("Invalid input")
	end
end

-- Validation after Hangul syllable decomposition
-- some invalid sequences are difficult to detect with precomposed Hangul syllables
function p.validate_decomposed(text)
	if find(text, "[ᆨ-ᆪᆬ-ᆮᆴ-ᆶᆸᆹᆻᆽ-ᇂ]%*?﷐?@﷐?[ᄀᄃᄇᄉᄌ]") or find(text, "ᆰ%*?﷐?@﷐?[ᄀ-ᄊᄌ-ᄑ]") or find(text, "ᆲ﷐?@﷐?[ᄀ-ᄊᄌ-ᄑ]") or find(text, "ᆺ%*@[ᄀᄇ]") or find(text, "ᆺ%*?﷐?@﷐?[ᄁ-ᄆᄈ-ᄊᄌ-ᄑ]") or find(text, "[ᅡ-ᅵᆨ-ᆪᆬ-ᇂ]﷐?@﷐?ᄅ") or find(text, "[ᅡ-ᅵ]﷐?@﷐?ᄋ") or find(text, "[ᅡ-ᅵᆫ-ᆭᆯᆱ-ᆷᆼ]﷐?@﷐?ᄒ") then
		error("Found invalid sequence containing @")
	elseif find(text, "[ᅡ-ᅵᆨᆫᆭ-ᆯᆶ-ᆸᆼ]﷐?%$") then
		error("Found invalid sequence containing $")
	end
end

-- Remove links and markup, which are unnecessary or interfere with assimilation
function p.remove_links_and_markup(text)
	local replacements = {
		-- Remove bold/italic wikitext syntax
		-- It is not impossible to allow bold/italic when it does not interfere with assimilation, but determining when to allow or disallow that adds complication for little practical gain
		{"'''", ""},
		{"''", ""},
		-- Remove HTML tags (except br)
		{"<[Bb][Rr] */?>", "&#10;"},
		{"</?[A-Za-z][^>]->", ""},
		{"&#10;", "<br>"},
		-- Remove wikilinks
		{"%[%[[^%|]+%|(..-)%]%]", "%1"},
		{"%[%[", ""},
		{"%]%]", ""}
	}
	text = gsub_iterate(text, replacements)
	text = mw.text.killMarkers(text)
	return text
end

-- Converting escaped special chars to HTML tags to preserve them
function p.escaped_to_html(text)
	local replacements = {
		{"\\%$", "&#36;"},
		{"\\%%", "&#37;"},
		{"\\%*", "&#42;"},
		{"\\@", "&#64;"},
		{"\\%^", "&#94;"},
		{"\\_", "&#95;"},
		{"\\`", "&#96;"}
	}
	return gsub_iterate(text, replacements)
end

-- Converting HTML tags back to unescaped chars
function p.html_to_ascii(text)
	local replacements = {
		{"&#36;", "$"},
		{"&#37;", "%%"},
		{"&#42;", "*"},
		{"&#64;", "@"},
		{"&#94;", "^"},
		{"&#95;", "_"},
		{"&#96;", "`"}
	}
	return gsub_iterate(text, replacements)
end

-- Unwrapping enclosed Hangul text
-- Actually not very necessary, but these are also classified as Hangul chars in Unicode
-- No distinction is made between parenthesized and circled chars
-- Needs to be executed before decomposing Hangul
function p.unwrap_enclosed_hangul(text)
	local replacements = {
		{"[㈀㉠]", "(기역)"},
		{"[㈁㉡]", "(니은)"},
		{"[㈂㉢]", "(디귿)"},
		{"[㈃㉣]", "(리을)"},
		{"[㈄㉤]", "(미음)"},
		{"[㈅㉥]", "(비읍)"},
		{"[㈆㉦]", "(시옷)"},
		{"[㈇㉧]", "(이응)"},
		{"[㈈㉨]", "(지읒)"},
		{"[㈉㉩]", "(치읓)"},
		{"[㈊㉪]", "(키읔)"},
		{"[㈋㉫]", "(티읕)"},
		{"[㈌㉬]", "(피읖)"},
		{"[㈍㉭]", "(히읗)"},
		{"[㈎㉮]", "(가)"},
		{"[㈏㉯]", "(나)"},
		{"[㈐㉰]", "(다)"},
		{"[㈑㉱]", "(라)"},
		{"[㈒㉲]", "(마)"},
		{"[㈓㉳]", "(바)"},
		{"[㈔㉴]", "(사)"},
		{"[㈕㉵]", "(아)"},
		{"[㈖㉶]", "(자)"},
		{"[㈗㉷]", "(차)"},
		{"[㈘㉸]", "(카)"},
		{"[㈙㉹]", "(타)"},
		{"[㈚㉺]", "(파)"},
		{"[㈛㉻]", "(하)"},
		{"㈜", "(주)"},
		{"㈝", "(오전)"},
		{"㈞", "(오후)"},
		{"㉼", "(참고)"},
		{"㉽", "(주의)"},
		{"㉾", "(우)"}
	}
	return gsub_iterate(text, replacements)
end

-- Final validation and postprocessing (both MR and RR)
function p.final_processing(text)
	-- result should not contain Hangul
	if m_utils.contains_hangul(text) then
		error("Result contains Hangul; debugging required")
	end

	text = p.html_to_ascii(text) -- convert HTML encodings back to ASCII

	-- if result is nothing (e.g. when input is just ㅇ)
	if text == "" then
		text = "—"
	end

	return text
end
return p


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.