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 get the root language by traversing up the hierarchy
-- Function to recursively find the root language
local function get_root_language(language)
function p.findRootLanguage(languageName)
     local parent_prop = "Is Variety Of"
     local rootLanguage = languageName
     local level = 0
    local varietyProperty = "Is Variety Of"
    local output = {}
      
 
     -- Keep querying for the parent language until no parent is found
     -- Loop upwards to find the root language
     while true do
     while true do
        level = level + 1
         local askQuery = string.format('{{#ask: [[%s]][[%s::+]] |?%s |limit=1 |format=list}}',
         local query = "[[" .. language .. "]]|?" .. parent_prop
            rootLanguage, varietyProperty, varietyProperty)
        local result = mw.smw.ask{ query }
          
 
         local parentLanguage = mw.uri.decode(frame:preprocess(askQuery))
         -- Log the query result to the output
          
         if result then
         -- If no parent language is found, we've reached the root
            table.insert(output, "Level " .. level .. ": " .. mw.text.jsonEncode(result))
         if parentLanguage == "" then
            -- Check if the property exists in the result and log it
            if result[language] and result[language][parent_prop] then
                table.insert(output, "Parent (" .. parent_prop .. "): " .. result[language][parent_prop])
            else
                table.insert(output, "No parent found for " .. language)
            end
        else
            table.insert(output, "Level " .. level .. ": No result for " .. language)
         end
 
         -- If no parent is found, we've reached the root
         if not result or not result[language] or not result[language][parent_prop] then
             break
             break
         end
         end
 
       
         -- Move up the hierarchy
         -- Update the root language to the parent
         language = result[language][parent_prop]
         rootLanguage = parentLanguage:match("^%s*(.-)%s*$")
     end
     end
 
      
     -- Return the output for debugging purposes
     return rootLanguage
     return table.concat(output, "\n")
end
end


-- Main function to print the full tree for the current language
-- Main function to generate the language tree
function p.tree(frame)
function p.languageTree(frame)
     local current_language = mw.title.getCurrentTitle().text
     local languageName = frame.args[1]
     local root_language = get_root_language(current_language)
      
 
     -- Find the root language
     -- Log the result of the get_root_language function
     local rootLanguage = p.findRootLanguage(languageName)
     local log_output = get_root_language(current_language)
      
 
     -- Construct the tree query
    -- Ensure the root language is not the current language, otherwise show nothing
     local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000 |root=%s',  
    if root_language == current_language then
         rootLanguage)
        return "No root language found.\n" .. log_output
      
     end
     -- Return the tree using preprocessing
 
     return frame:preprocess('{{#ask:' .. treeQuery .. '}}')
     -- Construct the query with the root parameter
     local query = string.format(
        '[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000|root=%s',
         root_language
     )
 
     -- Return the processed result along with the debug output
     return frame:preprocess('{{#ask:' .. query .. '}}') .. "\n\n" .. log_output
end
end


return p
return p

Revision as of 11:02, 25 March 2025

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

local p = {}

-- Function to recursively find the root language
function p.findRootLanguage(languageName)
    local rootLanguage = languageName
    local varietyProperty = "Is Variety Of"
    
    -- Keep querying for the parent language until no parent is found
    while true do
        local askQuery = string.format('{{#ask: [[%s]][[%s::+]] |?%s |limit=1 |format=list}}', 
            rootLanguage, varietyProperty, varietyProperty)
        
        local parentLanguage = mw.uri.decode(frame:preprocess(askQuery))
        
        -- If no parent language is found, we've reached the root
        if parentLanguage == "" then
            break
        end
        
        -- Update the root language to the parent
        rootLanguage = parentLanguage:match("^%s*(.-)%s*$")
    end
    
    return rootLanguage
end

-- Main function to generate the language tree
function p.languageTree(frame)
    local languageName = frame.args[1]
    
    -- Find the root language
    local rootLanguage = p.findRootLanguage(languageName)
    
    -- Construct the tree query
    local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000 |root=%s', 
        rootLanguage)
    
    -- Return the tree using preprocessing
    return frame:preprocess('{{#ask:' .. treeQuery .. '}}')
end

return p