Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 8: Line 8:
         -- Perform a semantic query to get the parent language
         -- Perform a semantic query to get the parent language
         local result = mw.smw.ask{ "[[" .. title .. "]]|?" .. prop }
         local result = mw.smw.ask{ "[[" .. title .. "]]|?" .. prop }
       
 
         -- Extract the first result
         -- Extract the first available parent
         local parent
         local parent = nil
         for _, data in pairs(result) do
         for _, data in pairs(result) do
             parent = data[prop]
             if type(data[prop]) == "table" then
             break -- Only need the first value
                parent = data[prop][1] -- Take the first parent if multiple values exist
            else
                parent = data[prop]
            end
             break -- Only need the first result
         end
         end


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

Revision as of 08:07, 26 March 2025

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

local p = {}

function p.getRootLanguage(frame)
    local title = mw.title.getCurrentTitle().fullText
    local prop = "Is Variety Of"

    while true do
        -- Perform a semantic query to get the parent language
        local result = mw.smw.ask{ "[[" .. title .. "]]|?" .. prop }

        -- Extract the first available parent
        local parent = nil
        for _, data in pairs(result) do
            if type(data[prop]) == "table" then
                parent = data[prop][1] -- Take the first parent if multiple values exist
            else
                parent = data[prop]
            end
            break -- Only need the first result
        end

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

        -- Move up the hierarchy
        title = parent
    end
end

return p