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 to recursively get the "Is Variety Of" property value
function p.getLanguageTree(frame)
local function getParentLanguage(language)
     -- Get the language name from the first argument
     -- Use the semantic query function provided by Semantic Scribunto
     local languageName = frame.args[1]
     local query = '[[' .. language .. ']]|?Is Variety Of'
    local result = mw.smw.ask(query)
      
      
    -- If there is a result, return the parent language (i.e., value of "Is Variety Of")
     if not languageName then
     if result and result[1] then
         return "No language name provided"
         return result[1]['?Is Variety Of']
    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
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
     -- Function to trace the language to its root
     while parent do
     local function findRootLanguage(lang)
        language = parent
        local currentLang = lang
        parent = getParentLanguage(language)
        local visited = {}
       
        while true do
            -- Prevent infinite loops
            if visited[currentLang] then
                return currentLang
            end
            visited[currentLang] = true
           
            -- Query for 'Is Variety Of' property
            local rootQuery = string.format('[[%s]]|?Is Variety Of', currentLang)
            local varietyResult = mw.smw.ask(rootQuery)
           
            -- If no 'Is Variety Of' value found, we've reached the root
            if not varietyResult or #varietyResult == 0 or not varietyResult[1]['Is Variety Of'] then
                return currentLang
            end
           
            -- Update current language to its parent
            currentLang = varietyResult[1]['Is Variety Of']
        end
     end
     end
      
      
     -- Construct the SMW query to show the tree starting from the root language
     -- Find the root language
     local query = '[[' .. language .. ']]|Category:Language|format=tree|parent=Is Variety Of|limit=5000|root=' .. language
     local rootLanguage = findRootLanguage(languageName)
     -- Execute the query and return the result
   
     return mw.smw.query(query)
    -- Generate the tree query
    local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000|root=%s', rootLanguage)
   
     -- Preprocess the query to render the tree
     local treeResult = frame:preprocess('{{#ask:' .. treeQuery .. '}}')
   
    return treeResult
end
end


return p
return p

Revision as of 14:20, 25 March 2025

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

local p = {}

function p.getLanguageTree(frame)
    -- Get the language name from the first argument
    local languageName = frame.args[1]
    
    if not languageName then
        return "No language name provided"
    end
    
    -- Function to trace the language to its root
    local function findRootLanguage(lang)
        local currentLang = lang
        local visited = {}
        
        while true do
            -- Prevent infinite loops
            if visited[currentLang] then
                return currentLang
            end
            visited[currentLang] = true
            
            -- Query for 'Is Variety Of' property
            local rootQuery = string.format('[[%s]]|?Is Variety Of', currentLang)
            local varietyResult = mw.smw.ask(rootQuery)
            
            -- If no 'Is Variety Of' value found, we've reached the root
            if not varietyResult or #varietyResult == 0 or not varietyResult[1]['Is Variety Of'] then
                return currentLang
            end
            
            -- Update current language to its parent
            currentLang = varietyResult[1]['Is Variety Of']
        end
    end
    
    -- Find the root language
    local rootLanguage = findRootLanguage(languageName)
    
    -- Generate the tree query
    local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000|root=%s', rootLanguage)
    
    -- Preprocess the query to render the tree
    local treeResult = frame:preprocess('{{#ask:' .. treeQuery .. '}}')
    
    return treeResult
end

return p