Module:RootFinder: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 8: Line 8:
     local title = startTitle
     local title = startTitle
     local prop = "Is Variety Of"
     local prop = "Is Variety Of"
    mw.log("Starting root language search for: " .. title)


     while true do
     while true do
        -- Perform a semantic query to get the parent language
         local queryString = string.format('[[%s]]|?%s', title, prop)
         local queryString = string.format('[[%s]]|?%s', title, prop)
        mw.log(queryString)
         local queryResult = mw.smw.getQueryResult(queryString)
         local queryResult = mw.smw.getQueryResult(queryString)
        -- Log the query result
        mw.log("Query result for " .. title .. ": " .. mw.text.jsonEncode(queryResult))


         -- Ensure queryResult is valid and extract the first parent
         -- Ensure queryResult is valid and extract the first parent
Line 24: Line 17:
         if queryResult and type(queryResult) == "table" then
         if queryResult and type(queryResult) == "table" then
         for k,v in pairs( queryResult.results ) do
         for k,v in pairs( queryResult.results ) do
        mw.log(mw.text.jsonEncode(v))
         if v.printouts then
         if v.printouts then
        mw.log("Printout: " .. mw.text.jsonEncode(v.printouts))
            if v.printouts["Is Variety Of"] and  #v.printouts["Is Variety Of"] > 0 and v.printouts["Is Variety Of"][1].fulltext then
            if v.printouts["Is Variety Of"] and  #v.printouts["Is Variety Of"] > 0 and v.printouts["Is Variety Of"][1].fulltext then
              parent = v.printouts["Is Variety Of"][1].fulltext
              parent = v.printouts["Is Variety Of"][1].fulltext
Line 36: Line 27:
         -- If no parent is found, return the current title as root
         -- If no parent is found, return the current title as root
         if not parent or parent == "" then
         if not parent or parent == "" then
            mw.log("Root language found: " .. title)
             return title
             return title
         end
         end
Line 51: Line 41:
local query = string.format('[[Category:Language]]|format=tree|parent=Is Variety Of|root=%s', rootLanguage)
local query = string.format('[[Category:Language]]|format=tree|parent=Is Variety Of|root=%s', rootLanguage)
return frame:preprocess(mw.smw.ask(query))
return frame:preprocess(tostring(mw.smw.ask(query)))
end
end


return p
return p

Revision as of 11:59, 26 March 2025

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

local p = {}

local function getRootLanguage(startTitle)
    if not startTitle or startTitle == "" then
        return "Error: No start title provided"
    end

    local title = startTitle
    local prop = "Is Variety Of"

    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["Is Variety Of"] and  #v.printouts["Is Variety Of"] > 0 and v.printouts["Is Variety Of"][1].fulltext then
	            	  parent = v.printouts["Is Variety Of"][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

function p.buildLanguageTree(frame)
	local language = frame.args[1]
	local rootLanguage = getRootLanguage(language)
	
	local query = string.format('[[Category:Language]]|format=tree|parent=Is Variety Of|root=%s', rootLanguage)
	
	return frame:preprocess(tostring(mw.smw.ask(query)))
	
end

return p