Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


function p._findRoot(language)
-- Function to recursively get the "Is Variety Of" property value
     local current = language
local function getParentLanguage(language)
    while true do
     -- Query to get the "Is Variety Of" property for the given language
        -- Query for the 'Is Variety Of' property of the current language
    local query = '[[' .. language .. ']]|?Is Variety Of'
        local results = mw.smw.ask({
    local result = mw.ext.SemanticMediaWiki.ask(query)
            '[[Category:Language]]',
   
            '[[' .. current .. ']]',
    -- If there is a result, return the parent language (i.e., value of "Is Variety Of")
            '?Is Variety Of'
    if result and result[1] then
        })
         return result[1]['?Is Variety Of']
       
    else
        -- Exit if no results or invalid response
         -- Return nil if no parent is found (i.e., root language)
        if not results or #results == 0 then break end
         return nil
          
        -- Extract parent data from the first result
        local parentData = results[1]['Is Variety Of']
       
         -- Exit if no parent found
        if not parentData or #parentData == 0 then break end
       
        -- Move to the parent language (using fulltext for page name)
         current = parentData[1].fulltext
     end
     end
    return current
end
end


function p.main(frame)
-- Main function to construct the language tree query
     local language = frame.args[1] -- Get input language from template
function p.languageTree(frame)
     return p._findRoot(language)
     local language = frame.args[1] -- The input language name (e.g., "Südrheinfränkisch")
    local parent = getParentLanguage(language)
   
    -- Traverse up the language tree to find the root language
    while parent do
        language = parent
        parent = getParentLanguage(language)
    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

Revision as of 13:39, 25 March 2025

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

local p = {}

-- Function to recursively get the "Is Variety Of" property value
local function getParentLanguage(language)
    -- Query to get the "Is Variety Of" property for the given language
    local query = '[[' .. language .. ']]|?Is Variety Of'
    local result = mw.ext.SemanticMediaWiki.ask(query)
    
    -- If there is a result, return the parent language (i.e., value of "Is Variety Of")
    if result and result[1] then
        return result[1]['?Is Variety Of']
    else
        -- Return nil if no parent is found (i.e., root language)
        return nil
    end
end

-- Main function to construct the language tree query
function p.languageTree(frame)
    local language = frame.args[1] -- The input language name (e.g., "Südrheinfränkisch")
    local parent = getParentLanguage(language)
    
    -- Traverse up the language tree to find the root language
    while parent do
        language = parent
        parent = getParentLanguage(language)
    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

return p