Module:languages: Difference between revisions

ਕੋਈ ਸੋਧ ਸਾਰ ਨਹੀਂ
No edit summary
No edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 14: Line 14:
end
end


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


function export.find_by_canonical_name(name)
function export.find_by_name(name)
local code = mw.loadData("Module:languages/canonical names")[name]
local code = mw.loadData("Module:languages/names")[name]
if code == nil then return nil end
if code == nil then return nil end
return export.find_by_code(code)
return export.find_by_code(code)
Line 32: Line 32:
local l = export.get_by_code(code)
local l = export.get_by_code(code)
return l[field]
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
end