Module:languages: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local export = {}
local export = {}
local languages = {
 
-- Proto-Laenkean
function export.get_by_code(code)
lae_pro = {name = "Proto-Laenkean", code = "lae-pro"},
local lang = export.find_by_code(code)
-- Balavic
if lang == nil then error("No such language (" .. code .. ")") end
bal = {name = "Balavic", code = "bal"},
return lang
-- Kilitic
end
kil = {name = "Kilitic", code = "kil"},
 
-- Laefevian
function export.find_by_code(code)
lfv_pro = {name = "Proto-Laefevic", code = "lfv-pro"},
if type(code) ~= "string" then
lfv = {name = "Lafevian", code = "lfv"},
error("Language code must be string. Got " .. type(code))
-- Radestrian
end
rad_pro = {name = "Proto-Radic", code = "rad-pro"},
return mw.loadData("Module:languages/data")[code]
rad = {name = "Radestrian", code = "rad"},
end
-- Riyan
 
ryn_o = {name = "Old Riyan", code = "ryn-o"},
function export.get_by_name(name)
ryn_m = {name = "Middle Riyan", code = "ryn-m"},
local lang = export.find_by_canonical_name(name)
ryn = {name = "Riyan", code = "ryn"}
if lang == nil then error("No such language (" .. name .. ")") end
}
return lang
end
 
function export.find_by_name(name)
local code = mw.loadData("Module:languages/names")[name]
if code == nil then return nil end
return export.find_by_code(code)
end
 
function export.get_field_by_code(code, field)
if type(field) ~= "string" then
error("Desired field must be a string")
end
local l = export.get_by_code(code)
return l[field]
end
 
function export.is_ancestor(language, code)
if language.lineage == nil then return false end
return language.lineage[code] ~= nil
end
 
function export.get_common_ancestors(a, b)
if a.lineage == nil or b.lineage == nil then return {} end
local commons = {}
for ancestor, _ in pairs(a.lineage) do
if b.lineage[ancestor] then
table.insert(commons, ancestor)
end
end
return commons
end
 
function export.stage_at_split(a, b)
return #export.get_common_ancestors(a, b)
end -- returns stage at which the two languages diverged as an integer; if not related, returns 0
 
function export.common_ancestor(a, b)
local ancestors = export.get_common_ancestors(a, b)
table.sort(ancestors, function(a, b)
local al = export.get_by_code(a).lineage or {}
local bl = export.get_by_code(b).lineage or {}
return #al > #bl
end
)
return ancestors[1]
end


function export.getByCode(frame)
function export.getByCode(frame)
local code = frame.args[1]
local code = frame.args[1]
local want = frame.args[2]
local field = frame.args[2]
return languages[code][want]
if type(field) == "string" then
return export.get_field_by_code(code, field)
end
return export.get_by_code(code)
end
end


return export
return export

Latest revision as of 00:43, 7 August 2024

local export = {}

function export.get_by_code(code)
	local lang = export.find_by_code(code)
	if lang == nil then error("No such language (" .. code .. ")") end
	return lang
end

function export.find_by_code(code)
	if type(code) ~= "string" then
		error("Language code must be string. Got " .. type(code))
	end
	return mw.loadData("Module:languages/data")[code]
end

function export.get_by_name(name)
	local lang = export.find_by_canonical_name(name)
	if lang == nil then error("No such language (" .. name .. ")") end
	return lang
end

function export.find_by_name(name)
	local code = mw.loadData("Module:languages/names")[name]
	if code == nil then return nil end
	return export.find_by_code(code)
end

function export.get_field_by_code(code, field)
	if type(field) ~= "string" then
		error("Desired field must be a string")
	end
	local l = export.get_by_code(code)
	return l[field]
end

function export.is_ancestor(language, code)
	if language.lineage == nil then return false end
	return language.lineage[code] ~= nil
end

function export.get_common_ancestors(a, b)
	if a.lineage == nil or b.lineage == nil then return {} end
	
	local commons = {}
	
	for ancestor, _ in pairs(a.lineage) do
		if b.lineage[ancestor] then
			table.insert(commons, ancestor)
		end
	end
	
	return commons
end

function export.stage_at_split(a, b)
	return #export.get_common_ancestors(a, b)
end -- returns stage at which the two languages diverged as an integer; if not related, returns 0

function export.common_ancestor(a, b)
	local ancestors = export.get_common_ancestors(a, b)
	
	table.sort(ancestors, function(a, b)
		local al = export.get_by_code(a).lineage or {}
		local bl = export.get_by_code(b).lineage or {}
		return #al > #bl
	end
	)
	
	return ancestors[1]
end

function export.getByCode(frame)
	local code = frame.args[1]
	local field = frame.args[2]
	if type(field) == "string" then
		return export.get_field_by_code(code, field)
	end
	return export.get_by_code(code)
end

return export