Module:descendants

Revision as of 05:57, 30 May 2024 by TheNightAvl (talk | contribs)

Underlies {{descendant}} and {{descendant tree}}.


local export = {}
local getArgs = require("Module:Arguments").getArgs
local m_links = require("Module:links")
local m_languages = require("Module:languages")
local m_inline = require("Module:inline")

function export.descendant(frame)
	local args = getArgs(frame)
	local out = ""
	local lang = m_languages.get_by_code(args[1])
	local borrowed = args["borrowed"] or args["bor"] or args["b"]
	local see_desc = args["see"]
	local noname = args["noname"]
	local lostcap = args["lost"] or "lost"
	
	if borrowed then out = "<span class=\"desc-arrow\" title=\"borrowed\">→</span>" .. out end
	if not noname then out = out .. lang.name .. ":&nbsp;" end
	
	if args[2] then
		local terms = {}
		local i = 2
		while args[i] do
			local term, term_args = m_inline.parse(args[i])
			table.insert(terms, m_links.full_link({
					language = lang,
					term = term,
					alt = term_args.alt,
					anchor = term_args.anchor or term_args.a,
					gloss = term_args.t,
					pos = term_args.pos,
					nobold = true,
				})
			)
			i = i + 1	
		end
		out = out .. table.concat(terms, ", ")
		if see_desc then out = out .. frame:expandTemplate{ title = 'see descendants' } end
	else
		out = out .. "— (''" .. lostcap .. "'')"
	end
	
	return out
end

function export.langtree(list)
	
	-- build in-set ancestry
	local ancestors = {}
	local ancestors_num = 0
	for _, lang in ipairs(list) do
		ancestors[lang] = {}
		ancestors_num = ancestors_num + 1
		for _, test in ipairs(list) do
			if m_languages.get_by_code(lang).lineage[test] then
				table.insert(ancestors[lang], test)
			end
		end
	end
	
	table.sort(ancestors, sort_list)
	mw.logObject(ancestors)
	
	local structure = {}
	
	mw.log("========== LEVEL 1 ==========")
	for lang, _ in pairs(ancestors) do
		if #ancestors[lang] == 0 then
			structure[lang] = {}
			ancestors[lang] = nil
			ancestors_num = ancestors_num - 1
			mw.log("Apex recognised: " .. lang)
		end
	end
	
	local swept = {}
	local its = 2
	local function search_and_sort(superordinate)
		mw.log("========== ITERATION " .. its .. " ==========")
		mw.log("Remaining data:")
		for lang, _ in pairs(ancestors) do
			mw.log("· " .. lang)
		end
		local changes = false
		for mother, _ in pairs(superordinate) do
			mw.log("————— Testing [" .. mother .. "] —————")
			for lang, lang_ancestors in pairs(ancestors) do
				if #lang_ancestors == 1 and lang_ancestors[1] == mother then
					superordinate[mother][lang] = {}
					swept[mother] = true
					ancestors[lang] = nil
					ancestors_num = ancestors_num - 1
					changes = true
					mw.log("Testing [" .. lang .. "] against [" .. mother .. "]: ✓")
				else
					mw.log("Testing [" .. lang .. "] against [" .. mother .. "]: ×")
				end
			end
			for lang, lang_ancestors in pairs(ancestors) do
				for i, lang_ancestor in ipairs(ancestors[lang]) do
					if swept[lang_ancestor] then table.remove(ancestors[lang], i) end
				end
			end
		end
		if changes and ancestors_num > 0 then
			its = its + 1
			for _, it in pairs(superordinate) do
				search_and_sort(it)
			end
		elseif ancestors_num > 0 then mw.log("PATH EXHAUSTED")
		else mw.log("DATA EXHAUSTED")
		end
	end
	
	search_and_sort(structure)
	
	mw.log("=========================")
	mw.logObject(structure)

end

function export.desctree(frame)
	local args = getArgs(frame)
	local out = ""
	local current_lang
	local terms = {}
	local langs = {}
	
	for i, term in ipairs(args) do --ryn:term1|term2|term3|rad:term1|term2 ...
		local lang_change = mw.ustring.match(term, "^([^%:%<]+)%:")
		local lang_change_arged = mw.ustring.match(term, "^(.+%>)%:")
		if i == 1 and not (lang_change or lang_change_arged) then error("Language code needs to be specified in the first parameter as xyz(<bor>):term₁") end
		if lang_change or lang_change_arged then
			if lang_change then
				current_lang = m_languages.get_by_code(lang_change)
				term = mw.ustring.gsub(term, "^[^%:]+%:%s*", "")
			elseif lang_change_arged then
				current_lang = m_languages.get_by_code(mw.ustring.match(lang_change_arged, "^[^%<]+"))
				term = mw.ustring.gsub(term, "^.+%>%:%s*", "")
			end
			if terms[current_lang.code] == nil then
				terms[current_lang.code] = {}
				table.insert(langs, current_lang.code)
			end
		end
		if lang_change_arged then
			for lang_arg in mw.ustring.gmatch(lang_change_arged, "%<([^%>]+)%>") do
				terms[current_lang.code][lang_arg] = true
			end
		end
		table.insert(terms[current_lang.code], term)
	end
	
	local items = {}
	
	for lang, lang_args in pairs(terms) do
		table.insert(lang_args, 1, lang)
		items[lang] = export.descendant(lang_args)
	end
	
	mw.logObject(items)
	
	-- BEGIN TRIAGE --
	
	export.langtree(langs)
	
end

return export

--[[
Debug console test string:
=p.descendant(mw.getCurrentFrame():newChild{title="whatever",args={"rad"}})
=p.desctree(mw.getCurrentFrame():newChild{title="whatever",args={"rad:hello", "hi", "ryn:hello", "rad-o:hello", "rad-pro:hello", "hrd-pro:hi", "ryn-o:hihe", "lfv-pro:hihe", "lfv-o:hihe", "lfv:hihe"}})
]]--