Module:descendants: Difference between revisions

ਕੋਈ ਸੋਧ ਸਾਰ ਨਹੀਂ
m (Optimisation)
No edit summary
 
(12 intermediate revisions by the same user not shown)
Line 4: Line 4:
local m_languages = require("Module:languages")
local m_languages = require("Module:languages")
local m_inline = require("Module:inline")
local m_inline = require("Module:inline")
local m_table = require("Module:table")
local see_further_text = "see there for further descendants"


function export.descendant(frame)
function export.descendant(frame)
Line 19: Line 21:
local see_desc = args["see"]
local see_desc = args["see"]
local noname = args["noname"]
local noname = args["noname"]
local nocat = args["nocat"]
local lostcap = args["lost"] or "lost"
local lostcap = args["lost"] or "lost"
if not nocat then out = out .. "[[Category:Terms with " .. lang.name .. " descendants|" .. mw.title.getCurrentTitle().subpageText .. "]]" end
if borrowed then out = "<span class=\"desc-arrow\" title=\"borrowed\">→</span>" .. out end
if borrowed then out = "<span class=\"desc-arrow\" title=\"borrowed\">→</span>" .. out end
if not noname then out = out .. lang.name .. ":&nbsp;" end
if not noname then out = out .. lang.name .. ":&nbsp;" end
Line 36: Line 40:
gloss = term_args.t,
gloss = term_args.t,
pos = term_args.pos,
pos = term_args.pos,
nolink = term_args.nolink,
hypo = term_args.hypo or term_args.hypothetical,
nobold = true,
nobold = true,
})
})
Line 42: Line 48:
end
end
out = out .. table.concat(terms, ", ")
out = out .. table.concat(terms, ", ")
if see_desc then out = out .. " (''see there for further descendants'')" end
if see_desc then out = out .. " (''" .. see_further_text .. "'')" end
else
else
out = out .. "— (''" .. lostcap .. "'')"
out = out .. "— (''" .. lostcap .. "'')"
Line 51: Line 57:


function export.langtree(list)
function export.langtree(list)
 
-- build in-set ancestry
-- build in-set ancestry
local ancestors = {}
local ancestors = {}
for _, lang in ipairs(list) do
for _, lang in ipairs(list) do
local function mismatch_check(base, test) -- function for processing doublets
for _, test_index in ipairs(list) do
if mw.ustring.find(test_index, "[0-9]+$") and mw.ustring.gsub(test_index, "[0-9]+$", "") == test and not m_table.contains(list, base .. mw.ustring.match(test_index, "[0-9]+$")) then
mw.addWarning("Multiple possible ancestors found for [" .. lang .. "] and mismatch of corresponding descendants detected ([" .. test_index .. "] but no [" .. base .. mw.ustring.match(test_index, "[0-9]+$") .. "]); please double-check numbering and identical code calls, as these may not have been properly triaged.")
end
end
end
-- process ancestors
ancestors[lang] = {}
ancestors[lang] = {}
if mw.ustring.find(lang, "[%>%_]") then
if mw.ustring.find(lang, "[%>%_]") then
local ancestor_temp = ""
for piece in mw.ustring.gmatch(lang, "([^%>%_]+[%>%_])") do
-- process ad hoc ancestors
ancestor_temp = ancestor_temp .. piece
local ancestor_path = ""
ancestor_add = mw.ustring.gsub(ancestor_temp, "[%>%_]$", "")
for piece, delim in mw.ustring.gmatch(lang, "([^%>%_]+)([%>%_])") do
table.insert(ancestors[lang], ancestor_add)
ancestor_path = ancestor_path .. piece
if mw.ustring.match(lang, "^%>") then
ancestor_path = ">" .. ancestor_path
end
table.insert(ancestors[lang], ancestor_path)
ancestor_path = ancestor_path .. delim
end
end
elseif mw.ustring.find(lang, "[0-9]+$") then
local lang_temp, index = mw.ustring.match(lang, "^([^0-9]+)([0-9]+)$")
else -- if ad hoc ancestors unspecified, i.e. the code does not contain the path symbols > or _
for _, test in ipairs(list) do
if mw.ustring.find(test, index .. "$") then
if mw.ustring.find(lang, "[0-9]+$") then
local test_temp = mw.ustring.gsub(test, index .. "$", "")
local lang_temp, index = mw.ustring.match(lang, "^([^0-9]+)([0-9]+)$")
if m_languages.get_by_code(lang_temp).lineage[test_temp] then
for _, test in ipairs(list) do
if mw.ustring.find(test, "[^0-9]" .. index .. "$") then -- matches existing historical doublets with identical, e.g. rad-o2, if rad2 is present
local test_temp = mw.ustring.gsub(test, index .. "$", "") -- get base
if m_languages.get_by_code(lang_temp).lineage[test_temp] then
table.insert(ancestors[lang], test)
end
elseif m_languages.get_by_code(lang_temp).lineage[test] and not m_table.contains(list, test .. index) then -- if test is still an ancestor to current language, e.g. rad-o, if rad2 is present, and checks if rad-o2 is not present
mismatch_check(lang_temp, test)
table.insert(ancestors[lang], test)
table.insert(ancestors[lang], test)
end
end
elseif m_languages.get_by_code(lang_temp).lineage[test] then
end
local nope = false
else
for _, contain_test in ipairs(list) do
for _, test in ipairs(list) do
if contain_test == test .. index then nope = true end
if m_languages.get_by_code(lang).lineage[test] then
mismatch_check(lang, test)
table.insert(ancestors[lang], test)
end
end
if not nope then table.insert(ancestors[lang], test) end
end
end
else
for _, test in ipairs(list) do
if m_languages.get_by_code(lang).lineage[test] then
table.insert(ancestors[lang], test)
end
end
end
end
Line 129: Line 153:
for i, term in ipairs(args) do --ryn:term1|term2|term3|rad:term1|term2 ...
for i, term in ipairs(args) do --ryn:term1|term2|term3|rad:term1|term2 ...
local lang_change = mw.ustring.match(term, "^([^%:%<]+)%:")
local lang_change = mw.ustring.match(term, "^([^%:%<]+)%:") or mw.ustring.match(term, "^(.+%>)%:")
local lang_change_arged = mw.ustring.match(term, "^(.+%>)%:")
if i == 1 and lang_change == nil then error("No language code in the first parameter") end
local from_lang, index
if lang_change then
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
term = mw.ustring.sub(term, mw.ustring.len(lang_change) + 2)
if lang_change or lang_change_arged then
if lang_change then
local auto_ancestor, auto_bor = mw.ustring.sub(lang_change, 1, 1) == "_", mw.ustring.sub(lang_change, 1, 2) == ">>"
current_lang = lang_change
if auto_ancestor or auto_bor then
term = mw.ustring.gsub(term, "^[^%:]+%:%s*", "")
if code == nil then error("No preceding language code specified") end
elseif lang_change_arged then
if auto_bor then lang_change = mw.ustring.sub(lang_change, 2) end
current_lang = mw.ustring.match(lang_change_arged, "^[^%<]+")
lang_change = code .. lang_change
term = mw.ustring.gsub(term, "^.+%>%:%s*", "")
end
end
if mw.ustring.find(current_lang, "[%>%_][^%:]") then
from_lang, current_lang = mw.ustring.match(current_lang, "^(.*[%>%_])([^%>%_%:]+)$")
local lang_args = {}
if mw.ustring.match(from_lang, "%>$") then
code, lang_args = m_inline.parse(lang_change)
if lang_change_arged then
-- if new language, build table
lang_change_arged = lang_change_arged .. "<bor>"
if terms[code] then
else
local base_code, old_i = mw.ustring.match(code, "^(.*[^0-9])([0-9]*)$")
lang_change_arged = "<bor>"
local new_i = 2
end
while terms[base_code .. new_i] do
new_i = new_i + 1
end
end
code = base_code .. new_i
mw.addWarning("Duplicate code detected: converted [" .. base_code .. (old_i or "") .. "] to [" .. code .. "]")
end
end
if mw.ustring.find(current_lang, "[0-9]+$") then
terms[code] = {}
current_lang, index = mw.ustring.match(current_lang, "^(.*)([0-9]+)$")
table.insert(langs, code)
end
current_lang = m_languages.get_by_code(current_lang)
current_lang = code
code = current_lang.code
if from_lang then code = from_lang .. code end
local delim
if index then code = code .. index end
lang_args["from"], delim, current_lang = mw.ustring.match(current_lang, "^(.*)([%>%_])([^%>%_%:0-9]+)[0-9]*$")
if terms[code] == nil then
if delim and delim == ">" then lang_args["bor"] = true end
terms[code] = {}
-- process arguments
table.insert(langs, code)
for lang_arg, lang_val in pairs(lang_args) do
    terms[code][lang_arg] = lang_val
end
end
end
end
if lang_change_arged then
for lang_arg in mw.ustring.gmatch(lang_change_arged, "<([^>]+)>") do
    local s = mw.text.split(lang_arg, ":", true)
    terms[code][s[1]] = s[2] or true
    if #s > 2 then mw.addWarning("multiple : found in inline string") end
    end
end
if from_lang then terms[code]["from"] = from_lang end
table.insert(terms[code], term)
table.insert(terms[code], term)
end
end
Line 178: Line 197:
for lang, lang_args in pairs(terms) do
for lang, lang_args in pairs(terms) do
table.insert(lang_args, 1, lang)
table.insert(lang_args, 1, lang)
lang_args["nocat"] = args["nocat"]
items[lang] = export.descendant(lang_args)
items[lang] = export.descendant(lang_args)
end
end
-- BEGIN TRIAGE --
-- BEGIN TRIAGE --
 
local structure = export.langtree(langs)
local structure = export.langtree(langs)
 
local outs = {}
local outs = {}
Line 237: Line 257:
Debug console test string:
Debug console test string:
=p.descendant(mw.getCurrentFrame():newChild{title="whatever",args={"rad"}})
=p.descendant(mw.getCurrentFrame():newChild{title="whatever",args={"rad"}})
=p.desctree(mw.getCurrentFrame():newChild{title="whatever",args={"ryn>rad-o:1", "aeg:2", "3", "ryn:4", "ryn>rad-o_rad:5", ">rad-pro:6", "hrd-pro:7", "ryn-o:8", "lfv-pro:9", "lfv-o:10", "lfv:11", "rad-pro:12"}})
=p.desctree(mw.getCurrentFrame():newChild{title="whatever",args={"ryn>rad-o:1", "aeg:2", "3", "ryn:4", "ryn>rad-o_rad:5", ">rad-pro:6", "hrd-pro:7", "ryn-o:8", "lfv-pro:9", "lfv-o:10", "lfv:11", "rad-pro:12", "kil<lost:oh no>:", "bal<see>:13"}})
mw.logObject(p.langtree({"rad", "rad-o", "ryn", "hrd-pro", "lfv"}))
]]--
]]--