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 = {}


local function findRootLanguage(frame, languageName, visited, path)
-- Function to recursively find the root language
  -- Prevent infinite loops in case of circular references
function p.findRootLanguage(language)
  visited = visited or {}
    -- Query to get the "Is Variety Of" value for the given language
  path = path or {}
    local query = string.format('[[%s]]|?Is Variety Of', language)
  if visited[languageName] then
    -- Execute the query using mw.smw.ask
    return nil, "Circular reference detected: " .. languageName, path
    local result = mw.smw.ask(query)
  end
   
  visited[languageName] = true
    -- If there's a result (i.e., the language has a "Is Variety Of" value)
  table.insert(path, languageName)
    if result and result[1] and result[1].values and result[1].values["Is Variety Of"] then
 
        -- Recursively call the function with the parent language
  -- Query the current language for its Is Variety Of value
        return p.findRootLanguage(result[1].values["Is Variety Of"])
  local query = "[[ " .. languageName .. " ]] | ?Is Variety Of"
     else
  local results = frame:preprocess('{{#ask:' .. query .. '|limit=1}}')
        -- Return the current language if it doesn't have a "Is Variety Of" value
 
        return language
  -- Debugging: Append raw query results to the path
    end
  table.insert(path, "Query results: " .. results)
 
  -- Extract the parent language from the query results
  local parentLanguage = string.match(results, "Is Variety Of=(.-)")
 
  -- If there is no parent language, we have found the root
  if not parentLanguage then
    return languageName, nil, path
  end
 
  -- Recursively find the root
  return findRootLanguage(frame, mw.text.trim(parentLanguage), visited, path)
end
 
function p.main(frame)
  local languageName = frame.args[1]
  if not languageName then
    return "Error: Language name not provided."
  end
 
  local rootLanguage, errorMessage, path = findRootLanguage(frame, languageName)
 
  if errorMessage then
     return errorMessage .. "\nPath: " .. table.concat(path, " -> ")
  end
 
  -- Construct the Semantic MediaWiki query for the language tree
  local query = "[[Category:Language]]|format=tree|parent=Is Variety Of|limit=5000|root=" .. rootLanguage
  local tree = frame:preprocess('{{#ask:' .. query .. '}}')
 
  return tree .. "\nPath: " .. table.concat(path, " -> ")
end
end


return p
return p

Revision as of 13:09, 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(language)
    -- Query to get the "Is Variety Of" value for the given language
    local query = string.format('[[%s]]|?Is Variety Of', language)
    -- Execute the query using mw.smw.ask
    local result = mw.smw.ask(query)
    
    -- If there's a result (i.e., the language has a "Is Variety Of" value)
    if result and result[1] and result[1].values and result[1].values["Is Variety Of"] then
        -- Recursively call the function with the parent language
        return p.findRootLanguage(result[1].values["Is Variety Of"])
    else
        -- Return the current language if it doesn't have a "Is Variety Of" value
        return language
    end
end

return p