Module:RootFinder: Difference between revisions
From The Seven Sages of Rome
No edit summary |
No edit summary |
||
| Line 2: | Line 2: | ||
function p.getRootLanguage(frame) | function p.getRootLanguage(frame) | ||
local | local startTitle = frame.args[1] -- Expect a plain string as input (e.g., via {{PAGENAME}}) | ||
if not startTitle or startTitle == "" then | |||
return "Error: No start title provided" | |||
end | |||
local title = startTitle | |||
local prop = "Is Variety Of" | local prop = "Is Variety Of" | ||
mw.log("Starting root language search for: " .. title) | |||
while true do | while true do | ||
-- Perform a semantic query to get the parent language | -- Perform a semantic query to get the parent language | ||
local queryResult = mw.smw.ask{"[[" .. title .. "]]|?" .. prop} | local queryResult = mw.smw.ask{"[[" .. title .. "]]|?" .. prop} | ||
-- Log the query result | |||
mw.log("Query result for " .. title .. ": " .. mw.text.jsonEncode(queryResult)) | |||
-- Ensure queryResult is valid and extract the first parent | -- Ensure queryResult is valid and extract the first parent | ||
| Line 19: | Line 29: | ||
parent = row[prop] | parent = row[prop] | ||
end | end | ||
mw.log("Found parent: " .. parent) | |||
end | end | ||
break -- Only need the first row | break -- Only need the first row | ||
| Line 26: | Line 37: | ||
-- If no parent is found, return the current title as root | -- If no parent is found, return the current title as root | ||
if not parent or parent == "" then | if not parent or parent == "" then | ||
mw.log("Root language found: " .. title) | |||
return title | return title | ||
end | end | ||
Revision as of 08:18, 26 March 2025
Documentation for this module may be created at Module:RootFinder/doc
local p = {}
function p.getRootLanguage(frame)
local startTitle = frame.args[1] -- Expect a plain string as input (e.g., via {{PAGENAME}})
if not startTitle or startTitle == "" then
return "Error: No start title provided"
end
local title = startTitle
local prop = "Is Variety Of"
mw.log("Starting root language search for: " .. title)
while true do
-- Perform a semantic query to get the parent language
local queryResult = mw.smw.ask{"[[" .. title .. "]]|?" .. prop}
-- Log the query result
mw.log("Query result for " .. title .. ": " .. mw.text.jsonEncode(queryResult))
-- Ensure queryResult is valid and extract the first parent
local parent = nil
if queryResult and type(queryResult) == "table" then
for _, row in pairs(queryResult) do
if row[prop] then
if type(row[prop]) == "table" then
parent = row[prop][1] -- Take the first value if it's a list
else
parent = row[prop]
end
mw.log("Found parent: " .. parent)
end
break -- Only need the first row
end
end
-- If no parent is found, return the current title as root
if not parent or parent == "" then
mw.log("Root language found: " .. title)
return title
end
-- Move up to the parent language
title = parent
end
end
return p