Jump to content

Module:Ko-utils: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
whoops didnt mean to delete that, and comment fix
reducing redundancy
Line 1: Line 1:
local p = {}
local p = {}
local find = mw.ustring.find
local find = mw.ustring.find
local hanja_ranges = "[〇㐀-䶿一-鿿﨎﨏﨑﨓﨔﨟﨡﨣﨤﨧-﨩𠀀-𪛟𪜀-𮹟𰀀-𲎯]"


-- Decomposes Hangul into jamo (e.g. 한 (U+D55C) → ᄒ (U+1112), ᅡ (U+1161), ᆫ (U+11AB))
-- Decomposes Hangul into jamo (e.g. 한 (U+D55C) → ᄒ (U+1112), ᅡ (U+1161), ᆫ (U+11AB))
Line 15: Line 16:
-- Returns boolean on whether input contains any Hanja text at all
-- Returns boolean on whether input contains any Hanja text at all
function p.contains_hanja(text)
function p.contains_hanja(text)
local hanja_ranges = "[〇㐀-䶿一-鿿﨎﨏﨑﨓﨔﨟﨡﨣﨤﨧-﨩𠀀-𪛟𪜀-𮹟𰀀-𲎯]"
return text ~= nil and text ~= "" and find(text, hanja_ranges)
return text ~= nil and text ~= "" and find(text, hanja_ranges)
end
end
Line 21: Line 21:
-- Returns boolean on whether input only contains Hanja text
-- Returns boolean on whether input only contains Hanja text
function p.all_hanja(text)
function p.all_hanja(text)
local hanja_ranges = "[〇㐀-䶿一-鿿﨎﨏﨑﨓﨔﨟﨡﨣﨤﨧-﨩𠀀-𪛟𪜀-𮹟𰀀-𲎯]"
for i = 1, #text do
for i = 1, #text do
local c = text:sub(i, i)
local c = text:sub(i, i)

Revision as of 14:41, 21 April 2025

local p = {}
local find = mw.ustring.find
local hanja_ranges = "[〇㐀-䶿一-鿿﨎﨏﨑﨓﨔﨟﨡﨣﨤﨧-﨩𠀀-𪛟𪜀-𮹟𰀀-𲎯]"

-- Decomposes Hangul into jamo (e.g. 한 (U+D55C) → ᄒ (U+1112), ᅡ (U+1161), ᆫ (U+11AB))
function p.decompose_hangul(text)
	return mw.ustring.gsub(text, "[가-힣]", mw.ustring.toNFD)
end

-- Returns boolean on whether input contains any Hangul text at all
function p.contains_hangul(text)
	local hangul_ranges = "[ᄀ-ᇿ〮〯ㄱ-ㆎ㈀-㈞㉠-㉾ꥠ-꥿가-힣ힰ-퟿]"
	return text ~= nil and text ~= "" and find(text, hangul_ranges)
end

-- Returns boolean on whether input contains any Hanja text at all
function p.contains_hanja(text)
	return text ~= nil and text ~= "" and find(text, hanja_ranges)
end

-- Returns boolean on whether input only contains Hanja text
function p.all_hanja(text)
    for i = 1, #text do
        local c = text:sub(i, i)
        if not find(c, hanja_ranges) then
            return false
        end
    end
    return true
end

-- Returns boolean on whether input directly contains a Wikipedia reference
function p.contains_reference(text)
	return find(text, "'\"`UNIQ--") or find(text, "-QINU`\"'")
end

return p