Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Function to recursively get the "Is Variety Of" property value
function p.search(frame)
local function getParentLanguage(language)
local startTitle = frame.args[1]
    -- Query to get the "Is Variety Of" property for the given language
     if not startTitle or startTitle == "" then
    local query = '[[' .. language .. ']]|?Is Variety Of'
        return "Error: No start title provided"
     local result = mw.ext.SemanticMediaWiki.ask(query)
    end
      
      
    -- If there is a result, return the parent language (i.e., value of "Is Variety Of")
local prop = frame.args[2]
    if result and result[1] then
if not prop or prop == "" then
        return result[1]['?Is Variety Of']
         return "Error: No property name provided"
    else
        -- Return nil if no parent is found (i.e., root language)
         return nil
     end
     end
end


-- Main function to construct the language tree query
    local title = startTitle
function p.languageTree(frame)
 
    local language = frame.args[1] -- The input language name (e.g., "Südrheinfränkisch")
    while true do
    local parent = getParentLanguage(language)
        local queryString = string.format('[[%s]]|?%s', title, prop)
   
        local queryResult = mw.smw.getQueryResult(queryString)
    -- Traverse up the language tree to find the root language
 
    while parent do
        -- Ensure queryResult is valid and extract the first parent
         language = parent
        local parent = nil
         parent = getParentLanguage(language)
        if queryResult and type(queryResult) == "table" then
        for k,v in pairs( queryResult.results ) do
        if v.printouts then
            if v.printouts[prop] and  #v.printouts[prop] > 0 and v.printouts[prop][1].fulltext then
              parent = v.printouts[prop][1].fulltext
            end
            end
        end
        end
 
        -- If no parent is found, return the current title as root
        if not parent or parent == "" then
            return title
        end
 
         -- Move up to the parent language
         title = parent
     end
     end
   
    -- Construct the SMW query to show the tree starting from the root language
    local query = '[[' .. language .. ']]|Category:Language|format=tree|parent=Is Variety Of|limit=5000|root=' .. language
    -- Execute the query and return the result wrapped in frame:preprocess
    return mw.ext.SemanticMediaWiki.framePreprocess('{{#ask:' .. query .. '}}')
end
end


return p
return p

Latest revision as of 13:43, 26 March 2025

Documentation for this module may be created at Module:RootFinder/doc

local p = {}

function p.search(frame)
	local startTitle = frame.args[1]
    if not startTitle or startTitle == "" then
        return "Error: No start title provided"
    end
    
	local prop = frame.args[2]
	if not prop or prop == "" then
        return "Error: No property name provided"
    end

    local title = startTitle

    while true do
        local queryString = string.format('[[%s]]|?%s', title, prop)
        local queryResult = mw.smw.getQueryResult(queryString)

        -- Ensure queryResult is valid and extract the first parent
        local parent = nil
        if queryResult and type(queryResult) == "table" then
        	for k,v in pairs( queryResult.results ) do
        		if v.printouts then
	            	if v.printouts[prop] and  #v.printouts[prop] > 0 and v.printouts[prop][1].fulltext then
	            	  parent = v.printouts[prop][1].fulltext
	            	end
            	end
        	end
        end

        -- If no parent is found, return the current title as root
        if not parent or parent == "" then
            return title
        end

        -- Move up to the parent language
        title = parent
    end
end

return p