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 = {} | ||
-- Function to | -- Function to recursively find the root language | ||
function p.findRootLanguage(languageName) | |||
local | local rootLanguage = languageName | ||
local varietyProperty = "Is Variety Of" | |||
-- Keep querying for the parent language until no parent is found | |||
-- | |||
while true do | while true do | ||
local askQuery = string.format('{{#ask: [[%s]][[%s::+]] |?%s |limit=1 |format=list}}', | |||
local | rootLanguage, varietyProperty, varietyProperty) | ||
local parentLanguage = mw.uri.decode(frame:preprocess(askQuery)) | |||
-- If no parent language is found, we've reached the root | |||
if parentLanguage == "" then | |||
-- If no parent is found, we've reached the root | |||
if | |||
break | break | ||
end | end | ||
-- | -- Update the root language to the parent | ||
rootLanguage = parentLanguage:match("^%s*(.-)%s*$") | |||
end | end | ||
return rootLanguage | |||
return | |||
end | end | ||
-- Main function to | -- Main function to generate the language tree | ||
function p. | function p.languageTree(frame) | ||
local | local languageName = frame.args[1] | ||
-- Find the root language | |||
-- | local rootLanguage = p.findRootLanguage(languageName) | ||
local | |||
-- Construct the tree query | |||
local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000 |root=%s', | |||
rootLanguage) | |||
-- Return the tree using preprocessing | |||
return frame:preprocess('{{#ask:' .. treeQuery .. '}}') | |||
-- Construct the query | |||
local | |||
-- Return the | |||
return frame:preprocess('{{#ask:' .. | |||
end | end | ||
return p | return p | ||
Revision as of 11:02, 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(languageName)
local rootLanguage = languageName
local varietyProperty = "Is Variety Of"
-- Keep querying for the parent language until no parent is found
while true do
local askQuery = string.format('{{#ask: [[%s]][[%s::+]] |?%s |limit=1 |format=list}}',
rootLanguage, varietyProperty, varietyProperty)
local parentLanguage = mw.uri.decode(frame:preprocess(askQuery))
-- If no parent language is found, we've reached the root
if parentLanguage == "" then
break
end
-- Update the root language to the parent
rootLanguage = parentLanguage:match("^%s*(.-)%s*$")
end
return rootLanguage
end
-- Main function to generate the language tree
function p.languageTree(frame)
local languageName = frame.args[1]
-- Find the root language
local rootLanguage = p.findRootLanguage(languageName)
-- Construct the tree query
local treeQuery = string.format('[[Category:Language]] |format=tree |parent=Is Variety Of |limit=5000 |root=%s',
rootLanguage)
-- Return the tree using preprocessing
return frame:preprocess('{{#ask:' .. treeQuery .. '}}')
end
return p