Module:auto cat: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
 
(29 intermediate revisions by the same user not shown)
Line 1: Line 1:
local export = {}
local export = {}
local d_languages = mw.loadData("Module:languages/data")
local d_languages = mw.loadData("Module:languages/data")
local d_pos = {
local d = mw.loadData("Module:auto cat/data")
    "adjectives",
local d_cats, d_pos = d.cats, d.pos
    "adverbs",
"articles",
    "conjunctions",
    "determiners",
    "interjections",
"nouns",
    "numerals",
    "particles",
    "phrases",
    "postpositions",
    "prepositions",
    "pronouns",
    "proper nouns",
    "symbols",
    "verbs",
}


-- Alternative to mw.ustring.gsub that is neater to call in code
-- Alternative to mw.ustring.gsub that is neater to call in code
-- and can handle multiple replacements at once.
-- and can handle multiple replacements at once.
local function replace(str, gsubs)
local function replace(str, gsubs)
    local res = str
     for from, to in pairs(gsubs) do
     for from, to in pairs(gsubs) do
     res = tostring(mw.ustring.gsub(res, from, to))
     str = tostring(mw.ustring.gsub(str, from, to))
     end
     end
     return res
     return str
end
 
-- provides initial capitalisation
local function capitalize(word)
if mw.ustring.len(word) > 1 then
return mw.ustring.upper(mw.ustring.sub(word, 1, 1)) .. mw.ustring.sub(word, 2)
elseif mw.ustring.len(word) == 1 then
return mw.ustring.upper(word)
end
error("Empty input for capitalisation")
end
end


Line 43: Line 36:
end
end


-- provides initial capitalisation
local function pluralize(word)
local function capitalize(word)
local ending = mw.ustring.sub(word, -1)
if mw.ustring.len(word) > 1 then
if ending == "h" and mw.ustring.sub(word, -2, -1) == "s" then ending = "sh" end
return mw.ustring.upper(mw.ustring.sub(word, 1, 1)) .. mw.ustring.sub(word, 2)
if ending == "s" or ending == "z" or ending == "x" or ending == "sh" then
elseif mw.ustring.len(word) == 1 then
return word .. "es"
return mw.ustring.upper(word)
end
end
error("Empty input for capitalisation")
return word .. "s"
end
end


-- Expands a pattern containing %l (and optionally %u), adding versions
-- Expands a pattern containing %l (and optionally %u), adding versions
-- of the pattern for each possible language
-- of the pattern for each possible language
local function hydrate_pattern_l(out_patterns, p_lua, p_categories, also_u)
local function hydrate_pattern_l(out_patterns, p_lua, p_categories, also_u, also_p)
     for _, l_data in pairs(d_languages) do
     for _, l_data in pairs(d_languages) do
        if also_u then
    if also_p then
            for _, l2_data in pairs(d_languages) do
    for _, pos in ipairs(d_pos) do
                if l2_data.code ~= l_data.code then
    if also_u then
                    local p_out_cats = {}
            for _, l2_data in pairs(d_languages) do
                    for _, p_cat in ipairs(p_categories) do
                if l2_data.code ~= l_data.code then
                        table.insert(p_out_cats, replace(p_cat, {["%%l"] = l_data.name, ["%%u"] = l2_data.name}))
                    local p_out_cats = {}
                    end
                    for _, p_cat in ipairs(p_categories) do
                    table.insert(out_patterns, to_pattern(replace(p_lua, {["%%l"] = sanitize(l_data.name), ["%%u"] = sanitize(l2_data.name)}), p_out_cats))
                    table.insert(p_out_cats, capitalize(replace(p_cat, {["%%l"] = l_data.name, ["%%u"] = l2_data.name, ["%%ps"] = pluralize(pos), ["%%p"] = pos})))
                end
                    end
            end
                    table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%l"] = sanitize(l_data.name), ["%%u"] = sanitize(l2_data.name), ["%%ps"] = pluralize(pos), ["%%p"] = pos})), p_out_cats))
        else
                end
            local p_out_cats = {}
            end
            for _, p_cat in ipairs(p_categories) do
        else
                table.insert(p_out_cats, replace(p_cat, {["%%l"] = l_data.name}))
            local p_out_cats = {}
            end
            for _, p_cat in ipairs(p_categories) do
            table.insert(out_patterns, to_pattern(replace(p_lua, {["%%l"] = sanitize(l_data.name)}), p_out_cats))
            table.insert(p_out_cats, capitalize(replace(p_cat, {["%%l"] = l_data.name, ["%%ps"] = pluralize(pos), ["%%p"] = pos})))
            end
            table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%l"] = sanitize(l_data.name), ["%%ps"] = pluralize(pos), ["%%p"] = pos})), p_out_cats))
        end
    end
    else
        if also_u then
            for _, l2_data in pairs(d_languages) do
                if l2_data.code ~= l_data.code then
                    local p_out_cats = {}
                    for _, p_cat in ipairs(p_categories) do
                    table.insert(p_out_cats, replace(p_cat, {["%%l"] = l_data.name, ["%%u"] = l2_data.name}))
                    end
                    table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%l"] = sanitize(l_data.name), ["%%u"] = sanitize(l2_data.name)})), p_out_cats))
                end
            end
        else
            local p_out_cats = {}
            for _, p_cat in ipairs(p_categories) do
            table.insert(p_out_cats, replace(p_cat, {["%%l"] = l_data.name}))
            end
            table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%l"] = sanitize(l_data.name)})), p_out_cats))
        end
         end
         end
     end
     end
Line 87: Line 101:
end
end


local function hydrate_pattern_pos(out_patterns, p_lua, p_categories)
local function hydrate_pattern_p(out_patterns, p_lua, p_categories)
     for _, pos in ipairs(d_pos) do
     for _, pos in ipairs(d_pos) do
        local p_out_cats = {}
    local p_out_cats = {}
         for _, p_cat in ipairs(p_categories) do
         for _, p_cat in ipairs(p_categories) do
             table.insert(p_out_cats, replace(p_cat, {["^%%pos"] = capitalize(pos), ["%%pos"] = pos}))
             table.insert(p_out_cats, capitalize(replace(p_cat, {["%%ps"] = pluralize(pos), ["%%p"] = pos})))
         end
         end
         table.insert(out_patterns, to_pattern(replace(p_lua, {["^%%pos"] = capitalize(pos), ["%%pos"] = pos}), p_out_cats))
         table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%ps"] = pluralize(pos), ["%%p"] = pos})), p_out_cats))
    end
end
end
end


local function hydrate_patterns()
local function hydrate_patterns()
     local out_patterns = {}
     local out_patterns = {}
     for _, p_data in ipairs(mw.loadData("Module:auto cat/data")) do
     for _, p_data in ipairs(d_cats) do
         local p_template, p_categories = p_data[1], p_data[2]
         local p_template, p_categories = p_data[1], p_data[2]
         local p_lua = replace(sanitize(p_template), {["%%s"] = "(..-)"})
         local p_lua = replace(sanitize(p_template), {["%%s"] = "(..-)"})
         if mw.ustring.find(p_template, "%%l") ~= nil then
         if mw.ustring.find(p_template, "%%l") ~= nil then
             hydrate_pattern_l(out_patterns, p_lua, p_categories, mw.ustring.find(p_template, "%%u") ~= nil)
             hydrate_pattern_l(out_patterns, p_lua, p_categories, mw.ustring.find(p_template, "%%u") ~= nil, mw.ustring.find(p_template, "%%p") ~= nil)
         elseif mw.ustring.find(p_template, "%%c") ~= nil then
         elseif mw.ustring.find(p_template, "%%c") ~= nil then
            hydrate_pattern_c(out_patterns, p_lua, p_categories)
hydrate_pattern_c(out_patterns, p_lua, p_categories)
        elseif mw.ustring.find(p_template, "%%pos") ~= nil then
elseif mw.ustring.find(p_template, "%%p") ~= nil then
        hydrate_pattern_pos(out_patterns, p_lua, p_categories)
            hydrate_pattern_p(out_patterns, p_lua, p_categories)
         else
         else
             table.insert(out_patterns, to_pattern(p_lua, p_categories))
             table.insert(out_patterns, to_pattern(p_lua, p_categories))
         end
         end
     end
     end
    mw.logObject(out_patterns)
     return out_patterns
     return out_patterns
end
end
Line 121: Line 134:
     local _, _, s_match = mw.ustring.find(category_name, p_lua)
     local _, _, s_match = mw.ustring.find(category_name, p_lua)
     for _, p_category in ipairs(p_categories) do
     for _, p_category in ipairs(p_categories) do
         out = out .. "[[Category:" .. (is_s and replace(p_category, {["%%s"] = s_match}) or p_category) .. "]]"
         out = out .. "[[Category:" .. (is_s and replace(p_category, {["^%%s"] = capitalize(s_match), ["%%s"] = s_match}) or p_category) .. "]]"
     end
     end
     return out
     return out
Line 127: Line 140:


function export.auto_cat(category_name)
function export.auto_cat(category_name)
if category_name == "Fundamental" then return "" end
     local lua_patterns = hydrate_patterns()
     local lua_patterns = hydrate_patterns()
     for _, p_data in ipairs(lua_patterns) do
     for _, p_data in ipairs(lua_patterns) do
         local p_lua, p_categories = p_data[1], p_data[2]
         local p_lua, p_categories = p_data[1], p_data[2]
         if mw.ustring.find(category_name, p_lua) ~= nil then
         if mw.ustring.find(category_name, p_lua) ~= nil then
        --[[
        if mw.ustring.find(category_name, "^Rhymes%:%S+%/%S+$") and mw.site.stats.pagesInCategory(category_name, "pages") > 1 then
        -- Add active rhymes category if Rhyme category and also contains more than two terms
        local p_new = mw.ustring.gsub(category_name, "^Rhymes%:(%S+)%/(%S+)$", "%1 active rhymes|%2")
        table.insert(p_categories, p_new)
    ]]--
    if mw.ustring.find(category_name, "^Rhymes%:%S+%/%S+$") and mw.site.stats.pagesInCategory(category_name, "pages") < 2 then
        -- Hide category if only one entry
        local return_cat = mw.ustring.gsub(category_name, "^Rhymes%:(%S+)%/%S+$", "__HIDDENCAT__\n[[Category:%1 inactive rhymes]]")
        return return_cat
        end
        -- Process the categories
             return process_category(category_name, p_lua, p_categories)
             return process_category(category_name, p_lua, p_categories)
         end
         end

Latest revision as of 18:44, 4 August 2024

local export = {}
local d_languages = mw.loadData("Module:languages/data")
local d = mw.loadData("Module:auto cat/data")
local d_cats, d_pos = d.cats, d.pos

-- Alternative to mw.ustring.gsub that is neater to call in code
-- and can handle multiple replacements at once.
local function replace(str, gsubs)
    for from, to in pairs(gsubs) do
    	str = tostring(mw.ustring.gsub(str, from, to))
    end
    return str
end

-- provides initial capitalisation
local function capitalize(word)
	if mw.ustring.len(word) > 1 then
		return mw.ustring.upper(mw.ustring.sub(word, 1, 1))	.. mw.ustring.sub(word, 2)
	elseif mw.ustring.len(word) == 1 then
		return mw.ustring.upper(word)
	end
	error("Empty input for capitalisation")
end

-- Sanitizes a string containing hyphens so that the hyphens
-- do not count as Lua pattern special characters
local function sanitize(str)
    return tostring(mw.ustring.gsub(str, "%-", "%-"))
end

-- Takes a Lua pattern and a table of categories and returns
-- the auto-cat-style table format required, containing the
-- given data
local function to_pattern(pattern, categories)
    return {"^" .. pattern .. "$", categories}
end

local function pluralize(word)
	local ending = mw.ustring.sub(word, -1)
	if ending == "h" and mw.ustring.sub(word, -2, -1) == "s" then ending = "sh" end
	if ending == "s" or ending == "z" or ending == "x" or ending == "sh" then
		return word .. "es"
	end
	return word .. "s"
end

-- Expands a pattern containing %l (and optionally %u), adding versions
-- of the pattern for each possible language
local function hydrate_pattern_l(out_patterns, p_lua, p_categories, also_u, also_p)
    for _, l_data in pairs(d_languages) do
    	if also_p then
    		for _, pos in ipairs(d_pos) do
    			if also_u then
		            for _, l2_data in pairs(d_languages) do
		                if l2_data.code ~= l_data.code then
		                    local p_out_cats = {}
		                    for _, p_cat in ipairs(p_categories) do
		                    	table.insert(p_out_cats, capitalize(replace(p_cat, {["%%l"] = l_data.name, ["%%u"] = l2_data.name, ["%%ps"] = pluralize(pos), ["%%p"] = pos})))
		                    end
		                    table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%l"] = sanitize(l_data.name), ["%%u"] = sanitize(l2_data.name), ["%%ps"] = pluralize(pos), ["%%p"] = pos})), p_out_cats))
		                end
		            end
		        else
		            local p_out_cats = {}
		            for _, p_cat in ipairs(p_categories) do
		            	table.insert(p_out_cats, capitalize(replace(p_cat, {["%%l"] = l_data.name, ["%%ps"] = pluralize(pos), ["%%p"] = pos})))
		            end
		            table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%l"] = sanitize(l_data.name), ["%%ps"] = pluralize(pos), ["%%p"] = pos})), p_out_cats))
		        end
    		end
    	else
	        if also_u then
	            for _, l2_data in pairs(d_languages) do
	                if l2_data.code ~= l_data.code then
	                    local p_out_cats = {}
	                    for _, p_cat in ipairs(p_categories) do
	                    	table.insert(p_out_cats, replace(p_cat, {["%%l"] = l_data.name, ["%%u"] = l2_data.name}))
	                    end
	                    table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%l"] = sanitize(l_data.name), ["%%u"] = sanitize(l2_data.name)})), p_out_cats))
	                end
	            end
	        else
	            local p_out_cats = {}
	            for _, p_cat in ipairs(p_categories) do
	            	table.insert(p_out_cats, replace(p_cat, {["%%l"] = l_data.name}))
	            end
	            table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%l"] = sanitize(l_data.name)})), p_out_cats))
	        end
        end
    end
end

local function hydrate_pattern_c(out_patterns, p_lua, p_categories)
    for l_code, l_data in pairs(d_languages) do
        local p_out_cats = {}
        for _, p_cat in ipairs(p_categories) do
            table.insert(p_out_cats, replace(p_cat, {["%%l"] = l_data.name, ["%%c"] = l_code}))
        end
        table.insert(out_patterns, to_pattern(replace(p_lua, {["%%c"] = l_code}), p_out_cats))
    end
end

local function hydrate_pattern_p(out_patterns, p_lua, p_categories)
    for _, pos in ipairs(d_pos) do
    	local p_out_cats = {}
        for _, p_cat in ipairs(p_categories) do
            table.insert(p_out_cats, capitalize(replace(p_cat, {["%%ps"] = pluralize(pos), ["%%p"] = pos})))
        end
        table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%ps"] = pluralize(pos), ["%%p"] = pos})), p_out_cats))
	end
end

local function hydrate_patterns()
    local out_patterns = {}
    for _, p_data in ipairs(d_cats) do
        local p_template, p_categories = p_data[1], p_data[2]
        local p_lua = replace(sanitize(p_template), {["%%s"] = "(..-)"})
        if mw.ustring.find(p_template, "%%l") ~= nil then
            hydrate_pattern_l(out_patterns, p_lua, p_categories, mw.ustring.find(p_template, "%%u") ~= nil, mw.ustring.find(p_template, "%%p") ~= nil)
        elseif mw.ustring.find(p_template, "%%c") ~= nil then
			hydrate_pattern_c(out_patterns, p_lua, p_categories)
		elseif mw.ustring.find(p_template, "%%p") ~= nil then
            hydrate_pattern_p(out_patterns, p_lua, p_categories)
        else
            table.insert(out_patterns, to_pattern(p_lua, p_categories))
        end
    end
    return out_patterns
end

local function process_category(category_name, p_lua, p_categories)
    local out = ""
    local is_s = mw.ustring.find(p_lua, "%(%.%.%-%)") ~= nil
    local _, _, s_match = mw.ustring.find(category_name, p_lua)
    for _, p_category in ipairs(p_categories) do
        out = out .. "[[Category:" .. (is_s and replace(p_category, {["^%%s"] = capitalize(s_match), ["%%s"] = s_match}) or p_category) .. "]]"
    end
    return out
end

function export.auto_cat(category_name)
	if category_name == "Fundamental" then return "" end
	
    local lua_patterns = hydrate_patterns()
    for _, p_data in ipairs(lua_patterns) do
        local p_lua, p_categories = p_data[1], p_data[2]
        if mw.ustring.find(category_name, p_lua) ~= nil then
        	--[[
        	if mw.ustring.find(category_name, "^Rhymes%:%S+%/%S+$") and mw.site.stats.pagesInCategory(category_name, "pages") > 1 then
        		-- Add active rhymes category if Rhyme category and also contains more than two terms
        		local p_new = mw.ustring.gsub(category_name, "^Rhymes%:(%S+)%/(%S+)$", "%1 active rhymes|%2")
        		table.insert(p_categories, p_new)
    		]]--
    		if mw.ustring.find(category_name, "^Rhymes%:%S+%/%S+$") and mw.site.stats.pagesInCategory(category_name, "pages") < 2 then
        		-- Hide category if only one entry
        		local return_cat = mw.ustring.gsub(category_name, "^Rhymes%:(%S+)%/%S+$", "__HIDDENCAT__\n[[Category:%1 inactive rhymes]]")
        		return return_cat
        	end
        	-- Process the categories
            return process_category(category_name, p_lua, p_categories)
        end
    end

    return "[[Category:Category pages not matched by auto cat]]"
end

function export.show(frame)
    local title_obj = mw.title.getCurrentTitle()
    if title_obj.nsText ~= "Category" then
        error("{{auto cat}} can only be used on pages in the Category namespace")
    end
    return export.auto_cat(title_obj.text)
end

return export