Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
 
(39 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Function to sanitize page titles
function p.search(frame)
local function sanitizeTitle(title)
local startTitle = frame.args[1]
     if not title then return "" end
     if not startTitle or startTitle == "" then
        return "Error: No start title provided"
    end
      
      
     -- Trim whitespace
local prop = frame.args[2]
     title = title:match("^%s*(.-)%s*$")
if not prop or prop == "" then
   
        return "Error: No property name provided"
    -- Replace spaces with underscores
     end
    title = title:gsub(" ", "_")
 
   
     local title = startTitle
    -- Remove any problematic characters
 
    title = title:gsub("[<>%[%]|{}]", "")
    while true do
   
        local queryString = string.format('[[%s]]|?%s', title, prop)
    return title
        local queryResult = mw.smw.getQueryResult(queryString)
end
 
        -- Ensure queryResult is valid and extract the first parent
        local parent = nil
        if queryResult and type(queryResult) == "table" then
        for k,v in pairs( queryResult.results ) do
        if v.printouts then
            if v.printouts[prop] and  #v.printouts[prop] > 0 and v.printouts[prop][1].fulltext then
              parent = v.printouts[prop][1].fulltext
            end
            end
        end
        end


-- Main function to generate the language tree
         -- If no parent is found, return the current title as root
function p.languageTree(frame)
         if not parent or parent == "" then
    local languageName = frame.args[1]
             return title
   
    if not languageName or languageName == "" then
        return "No language name provided"
    end
   
    -- Sanitize the initial language name
    local currentLanguage = sanitizeTitle(languageName)
    local varietyProperty = "Is_Variety_Of"
    local maxIterations = 10
    local iterations = 0
    local rootLanguage = currentLanguage
   
    -- Find the ultimate root language
    while iterations < maxIterations do
        -- Query to find parent language
        local askQuery = string.format('{{#ask: [[%s]][[%s::+]] |?%s |limit=1}}',
            currentLanguage, varietyProperty, varietyProperty)
       
        local rawResult = frame:preprocess(askQuery)
       
        -- Extract parent language using a more robust method
        local parentLanguage = rawResult:match("%[%[" .. varietyProperty .. "::([^%]]+)%]%]")
       
         -- If no parent language is found, we've reached the root
         if not parentLanguage then
             break
         end
         end
       
 
         -- Sanitize and update current language
         -- Move up to the parent language
         currentLanguage = sanitizeTitle(parentLanguage)
         title = parent
        rootLanguage = currentLanguage
        iterations = iterations + 1
     end
     end
   
    -- Construct the comprehensive tree query
    local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is_Variety_Of |limit=5000 |root=%s |depth=10',
        rootLanguage)
   
    -- Return the tree using preprocessing
    return frame:preprocess('{{#ask:' .. treeQuery .. '}}')
end
end


return p
return p

Latest revision as of 13:43, 26 March 2025

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

local p = {}

function p.search(frame)
	local startTitle = frame.args[1]
    if not startTitle or startTitle == "" then
        return "Error: No start title provided"
    end
    
	local prop = frame.args[2]
	if not prop or prop == "" then
        return "Error: No property name provided"
    end

    local title = startTitle

    while true do
        local queryString = string.format('[[%s]]|?%s', title, prop)
        local queryResult = mw.smw.getQueryResult(queryString)

        -- Ensure queryResult is valid and extract the first parent
        local parent = nil
        if queryResult and type(queryResult) == "table" then
        	for k,v in pairs( queryResult.results ) do
        		if v.printouts then
	            	if v.printouts[prop] and  #v.printouts[prop] > 0 and v.printouts[prop][1].fulltext then
	            	  parent = v.printouts[prop][1].fulltext
	            	end
            	end
        	end
        end

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

        -- Move up to the parent language
        title = parent
    end
end

return p