Difference between revisions 3827 and 43827 on mnwwiktionary

local labels = {}

local subpages = {
	"entry maintenance",
	"figures of speech",
	"lemmas",
	"miscellaneous",
	"ယၟုဂမၠိုင်",
	"non-lemma forms",
	"phrases",
	"rhymes",
	"shortenings",
	"သင်္ကေတဂမၠိုင်",
	"ထာမ်ပလိက်ဂမၠိုင်",
	"terms by etymology",
	"terms by lexical property",
	"terms by semantic function",
	"terms by usage",
	"words by number of syllables",
	}

-- Import subpages
for _, subpage in ipairs(subpages) do
	local datamodule = "Module:category tree/poscatboiler/data/" .. subpage
	for key, data in pairs(require(datamodule)) do
		assert(not labels[key], "Duplicate label: " .. key)
		data.edit = datamodule
		labels[key] = data
	end
end

-- Add child categories to their parents
for key, data in pairs(labels) do
	if data.parents and not data.regex then
		for _, parent in ipairs(data.parents) do
			if type(parent) == "table" then
				if parent.name and not parent.template and not parent.name:find("^Category:") then
					if labels[parent.name].children then
						table.insert(labels[parent.name].children, {name = key, sort = parent.sort})
					else
						labels[parent.name].children = {{name = key, sort = parent.sort}}
					end
				end
			else
				if not parent:find("^Category:") and labels[parent] then
					if labels[parent].children then
						table.insert(labels[parent].children, key)
					else
						labels[parent].children = {key}
					end
				end
			end
		end
	end
end

return labelslocal raw_categories = {}
local handlers = {}
local raw_handlers = {}

local subpages = {
	--"အက္ခရ်", --characters
	"စရင်မင်မွဲထိင်လဝ်", --entry maintenance
	--"ဝံသ-ဘာသာဂကောံ", --families
	"ဗီုရုပ်ဂၞက်", --figures of speech
	--"အရေဝ်ဘာသာသာ်သာ်", --lang-specific
	--"ဘာသာ", --languages
	"ဝေါဟာအဓိက", --lemmas
	"နာနာပတ္တာရ", --miscellaneous
	--"มอดูล", --modules
	"ชื่อ", --names
	"รูปผัน", --non-lemma forms
	"วลี", --phrases
	"สัมผัส", --rhymes
	--"อักษร", --scripts
	"การย่อ", --shortenings
	"สัญลักษณ์", --symbols
	"แม่แบบ", --templates
	"ศัพท์แบ่งตามรากศัพท์", --terms by etymology
	"ศัพท์แบ่งตามประเภทไวยากรณ์", --terms by grammatical category
	"ศัพท์แบ่งตามสมบัติ", --terms by lexical property
	"ศัพท์แบ่งตามหน้าที่เชิงความหมาย", --terms by semantic function
	--"ศัพท์แบ่งตามอักษร", --terms by script
	"ศัพท์แบ่งตามการใช้", --terms by usage
	--"การถอดอักษร", --transliterations
	--"ยูนิโคด", --unicode
	--"ศัพท์ประจำวัน", --word of the day โครงการนี้ไม่ใช้
	"ศัพท์แบ่งตามจำนวนพยางค์", --words by number of syllables
}

-- Import subpages
for _, subpage in ipairs(subpages) do
	local datamodule = "Module:category tree/poscatboiler/data/" .. subpage
	local retval = require(datamodule)
	if not retval["LABELS"] and not retval["RAW_CATEGORIES"] and not retval["HANDLERS"] and not retval["RAW_HANDLERS"] then
		retval = {LABELS = retval}
	end
	if retval["LABELS"] then
		for key, data in pairs(retval["LABELS"]) do
			assert(not labels[key], "Duplicate label: " .. key)
			data.module = datamodule
			labels[key] = data
		end
	end
	if retval["RAW_CATEGORIES"] then
		for key, data in pairs(retval["RAW_CATEGORIES"]) do
			assert(not raw_categories[key], "Duplicate raw category: " .. key)
			data.module = datamodule
			raw_categories[key] = data
		end
	end
	if retval["HANDLERS"] then
		for _, handler in ipairs(retval["HANDLERS"]) do
			table.insert(handlers, { module = datamodule, handler = handler })
		end
	end
	if retval["RAW_HANDLERS"] then
		for _, handler in ipairs(retval["RAW_HANDLERS"]) do
			table.insert(raw_handlers, { module = datamodule, handler = handler })
		end
	end
end

-- Add child categories to their parents
local function add_children_to_parents(hierarchy, raw)
	for key, data in pairs(hierarchy) do
		local parents = data.parents
		if parents then
			if type(parents) ~= "table" then
				parents = {parents}
			end
			if parents.name or parents.module then
				parents = {parents}
			end
			for _, parent in ipairs(parents) do
				if type(parent) ~= "table" or not parent.name and not parent.module then
					parent = {name = parent}
				end
				if parent.name and not parent.module and type(parent.name) == "string" and not parent.name:find("^หมวดหมู่:") then
					local child_is_raw
					if raw then
						child_is_raw = not parent.is_label
					else
						child_is_raw = parent.raw
					end
					local child_hierarchy = child_is_raw and raw_categories or labels
					if child_hierarchy[parent.name] then
						local child = {name = key, sort = parent.sort, raw = raw}
						if child_hierarchy[parent.name].children then
							table.insert(child_hierarchy[parent.name].children, child)
						else
							child_hierarchy[parent.name].children = {child}
						end
					end
				end
			end
		end
	end
end

add_children_to_parents(labels)
add_children_to_parents(raw_categories, true)

return {LABELS = labels, RAW_CATEGORIES = raw_categories, HANDLERS = handlers, RAW_HANDLERS = raw_handlers}