Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
Tag: Manual revert
No edit summary
Line 1: Line 1:
-- Module:LanguageTree
local p = {}
local p = {}


function p.getLanguageTree(frame)
-- Finds the root language by following the "Is Variety Of" property chain
     -- Get the language name from the first argument
function p.findRootLanguage(frame, languageName)
     local languageName = frame.args[1]
     local currentLanguage = languageName
     local visited = {}
      
      
     if not languageName then
     while currentLanguage and not visited[currentLanguage] do
         return "No language name provided"
         visited[currentLanguage] = true
    end
       
   
        -- Query to get the parent language
    -- Function to trace the language to its root
         local query = "[[" .. currentLanguage .. "]]|?Is Variety Of"
    local function findRootLanguage(lang)
         local result = frame:preprocess('{{#ask:' .. query .. '}}')
         local currentLang = lang
         local visited = {}
          
          
         while true do
         -- Extract parent language from the result
             -- Prevent infinite loops
        local parent = nil
             if visited[currentLang] then
        for line in result:gmatch("[^\r\n]+") do
                 return currentLang
             local parentMatch = line:match("Is Variety Of:%s*(.+)")
             if parentMatch and parentMatch:gsub("%s+", "") ~= "" then
                 parent = parentMatch:gsub("^%s*(.-)%s*$", "%1") -- Trim whitespace
                break
             end
             end
            visited[currentLang] = true
        end
           
       
            -- Query for 'Is Variety Of' property
        if not parent or parent == "" then
            local rootQuery = string.format('[[%s]]|?Is Variety Of', currentLang)
             -- No parent found, this is the root language
            local varietyResult = mw.smw.ask(rootQuery)
             return currentLanguage
           
        else
             -- If no 'Is Variety Of' value found, we've reached the root
             -- Move up to the parent language
             if not varietyResult or #varietyResult == 0 or not varietyResult[1]['Is Variety Of'] then
             currentLanguage = parent
                return currentLang
            end
           
             -- Update current language to its parent
             currentLang = varietyResult[1]['Is Variety Of']
         end
         end
    end
   
    -- Handle potential circular reference
    return currentLanguage
end
-- Main function to generate the language tree
function p.main(frame)
    -- Get the language name from the frame parameters
    local languageName = frame.args[1]
   
    if not languageName or languageName == "" then
        return "Error: No language name provided"
     end
     end
      
      
     -- Find the root language
     -- Find the root language
     local rootLanguage = findRootLanguage(languageName)
     local rootLanguage = p.findRootLanguage(frame, 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
     -- Construct the query for the tree
     local treeResult = frame:preprocess('{{#ask:' .. treeQuery .. '}}')
     local treeQuery = "[[Category:Language]]|format=tree|parent=Is Variety Of|limit=5000|root=" .. rootLanguage
      
      
     return treeResult
     -- Execute the query using #ask and return the result
    return frame:preprocess('{{#ask:' .. treeQuery .. '}}')
end
end


return p
return p

Revision as of 14:25, 25 March 2025

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

-- Module:LanguageTree
local p = {}

-- Finds the root language by following the "Is Variety Of" property chain
function p.findRootLanguage(frame, languageName)
    local currentLanguage = languageName
    local visited = {}
    
    while currentLanguage and not visited[currentLanguage] do
        visited[currentLanguage] = true
        
        -- Query to get the parent language
        local query = "[[" .. currentLanguage .. "]]|?Is Variety Of"
        local result = frame:preprocess('{{#ask:' .. query .. '}}')
        
        -- Extract parent language from the result
        local parent = nil
        for line in result:gmatch("[^\r\n]+") do
            local parentMatch = line:match("Is Variety Of:%s*(.+)")
            if parentMatch and parentMatch:gsub("%s+", "") ~= "" then
                parent = parentMatch:gsub("^%s*(.-)%s*$", "%1") -- Trim whitespace
                break
            end
        end
        
        if not parent or parent == "" then
            -- No parent found, this is the root language
            return currentLanguage
        else
            -- Move up to the parent language
            currentLanguage = parent
        end
    end
    
    -- Handle potential circular reference
    return currentLanguage
end

-- Main function to generate the language tree
function p.main(frame)
    -- Get the language name from the frame parameters
    local languageName = frame.args[1]
    
    if not languageName or languageName == "" then
        return "Error: No language name provided"
    end
    
    -- Find the root language
    local rootLanguage = p.findRootLanguage(frame, languageName)
    
    -- Construct the query for the tree
    local treeQuery = "[[Category:Language]]|format=tree|parent=Is Variety Of|limit=5000|root=" .. rootLanguage
    
    -- Execute the query using #ask and return the result
    return frame:preprocess('{{#ask:' .. treeQuery .. '}}')
end

return p