Module:etymology: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
Line 14: Line 14:
     ["onomatopoeic"] = {
     ["onomatopoeic"] = {
         categories = {"onomatopoeias"},
         categories = {"onomatopoeias"},
         glossary = true,
         text = true,
     },
     },
     ["unknown"] = {
     ["unknown"] = {
         categories = {"terms with unknown etymologies"},
         categories = {"terms with unknown etymologies"},
        text = function(nocap) return (nocap and "u" or "U") .. "nknown" end,
     },
     },
}
}


local single_term_params = {
local single_term_params = {
[1] = {required = true},
    [1] = {required = true},
[2] = {required = true},
    [2] = {required = true},
[3] = {alias_of = "alt"},
    [3] = {alias_of = "alt"},
[4] = {alias_of = "t"},
    [4] = {alias_of = "t"},
["alt"] = {},
    ["alt"] = {},
["t"] = {},
    ["t"] = {},
["pos"] = {},
    ["pos"] = {},
["nocap"] = {type = "boolean"},
    ["nocap"] = {type = "boolean"},
["notext"] = {type = "boolean"},
    ["notext"] = {type = "boolean"},
["nocat"] = {type = "boolean"},
    ["nocat"] = {type = "boolean"},
}
}
local single_term_data = {
local single_term_data = {
     ["abbreviation"] = {
     ["abbreviation"] = {
         glossary = true,
         text = true,
         categories = {"abbreviations"}
         categories = {"abbreviations"}
     },
     },
     ["back-formation"] = {
     ["back-formation"] = {
         glossary = true,
         text = true,
         categories = {"back-formations"}
         categories = {"back-formations"}
     },
     },
     ["clipping"] = {
     ["clipping"] = {
         glossary = true,
         text = true,
         categories = {"clippings"},
         categories = {"clippings"},
     },
     },
     ["doublet"] = {
     ["doublet"] = {
    glossary = true,
        text = true,
    categories = {"doublets"},
        categories = {"doublets"},
     },
     },
     ["ellipsis"] = {
     ["ellipsis"] = {
         glossary = true,
         text = true,
         categories = {"ellipses"},
         categories = {"ellipses"},
     },
     },
     ["initialism"] = {
     ["initialism"] = {
         glossary = true,
         text = true,
         categories = {"initialisms"},
         categories = {"initialisms"},
     },
     },
     ["rebracketing"] = {
     ["rebracketing"] = {
         glossary = true,
         text = true,
         categories = {"rebracketings"},
         categories = {"rebracketings"},
     },
     },
     ["reduplication"] = {
     ["reduplication"] = {
         glossary = true,
         text = true,
         categories = {"reduplications"},
         categories = {"reduplications"},
     },
     },
Line 83: Line 84:
local derived_term_data = {
local derived_term_data = {
     ["borrowed"] = {
     ["borrowed"] = {
        silent = true,
         categories = {"terms borrowed from"},
         categories = {"terms borrowed from"},
     },
     },
     ["calque"] = {
     ["calque"] = {
         glossary = "calque",
         text = function(nocap) return (nocap and "b" or "B") .. "y [[Appendix:Glossary#surface analysis|surface analysis]], " end,
         categories = {"terms calqued from"},
         categories = {"terms calqued from"},
     },
     },
     ["derived"] = {
     ["derived"] = {
        silent = true,
         categories = {"terms derived from"},
         categories = {"terms derived from"},
     },
     },
     ["inherited"] = {
     ["inherited"] = {
    silent = true,
        categories = {"terms inherited from"},
    categories = {"terms inherited from"},
     },
     },
     ["learned borrowing"] = {
     ["learned borrowing"] = {
         glossary = true,
         text = true,
         categories = {"terms borrowed from", "learned borrowings from"},
         categories = {"terms borrowed from", "learned borrowings from"},
     },
     },
     ["orthographic borrowing"] = {
     ["orthographic borrowing"] = {
         glossary = true,
         text = true,
         categories = {"terms borrowed from", "orthographic borrowings from"},
         categories = {"terms borrowed from", "orthographic borrowings from"},
     },
     },
     ["semantic loan"] = {
     ["semantic loan"] = {
         glossary = true,
         text = true,
         categories = {"terms derived from", "semantic loans from"},
         categories = {"terms derived from", "semantic loans from"},
     },
     },
     ["phono-semantic matching"] = {
     ["phono-semantic matching"] = {
         glossary = true,
         text = true,
         categories = {"terms derived from", "phono-semantic matchings from"},
         categories = {"terms derived from", "phono-semantic matchings from"},
     },
     },
     ["transliteration"] = {
     ["transliteration"] = {
         glossary = true,
         text = true,
         categories = {"terms derived from", "transliterations of"},
         categories = {"terms derived from", "transliterations of"},
     },
     },
Line 147: Line 145:
end
end


function export.format_etymology(out, categories)
local function format_etymology(out, categories)
     for _, category in ipairs(categories) do
     for _, category in ipairs(categories) do
         out = out .. "[[Category:" .. category .. "]]"
         out = out .. "[[Category:" .. category .. "]]"
Line 154: Line 152:
end
end


function export.format_glossary(text, glossary_arg, nocap_arg)
local function format_solo_text(label, text_data, nocap_arg)
     if not glossary_arg then return nocap_arg and text or mw.ustring.gsub(text, "^%l", string.upper) end
     if not text_data then return "" end
     local glossary_link = type(glossary_arg) == "string" and glossary_arg or text
     if type(text_data) == "function" then return text_data(nocap_arg) end
     local glossary_display = nocap_arg and text or mw.ustring.gsub(text, "^%l", string.upper)
     return "[[Appendix:Glossary#" .. label .. "|" .. (nocap_arg and label or mw.ustring.gsub(label, "^%l", string.upper)) .. "]]"
     return "[[Appendix:Glossary#" .. glossary_link .. "|" .. glossary_display .. "]]"
end
 
local function format_prefixed_text(label, text_data, nocap_arg)
    if not text_data then return "" end
    if type(text_data) == "function" then return text_data(nocap_arg) end
     return "[[Appendix:Glossary#" .. label .. "|" .. (nocap_arg and label or mw.ustring.gsub(label, "^%l", string.upper)) .. "]] of "
end
end


function export.hydrate_category(category, language_to, language_from)
local function hydrate_category(category, language_to, language_from)
     local new_category = language_to.name .. " " .. category
     local new_category = language_to.name .. " " .. category
     if language_from then new_category = new_category .. " " .. language_from.name end
     if language_from then new_category = new_category .. " " .. language_from.name end
Line 167: Line 170:
end
end


function export.hydrate_categories(categories, language_to, language_from)
local function hydrate_categories(categories, language_to, language_from)
     local new_categories = {}
     local new_categories = {}
     for i, category in ipairs(categories) do
     for i, category in ipairs(categories) do
         new_categories[i] = export.hydrate_category(category, language_to, language_from)
         new_categories[i] = hydrate_category(category, language_to, language_from)
     end
     end
     return new_categories
     return new_categories
end
end


function export.no_term_etymology(template, frame)
local function no_term_etymology(template, frame)
     local data, args = no_term_data[template], m_parameters.process(frame:getParent().args, no_term_params)
     local data, args = no_term_data[template], m_parameters.process(frame:getParent().args, no_term_params)
     local out, categories = "", {}
     local out, categories = "", {}
     local language = m_languages.get_by_code(args[1])
     local language = m_languages.get_by_code(args[1])
     if not args["nocat"] then categories = export.hydrate_categories(data["categories"], language) end
     if not args["nocat"] then categories = hydrate_categories(data["categories"], language) end
     if not args["notext"] then out = export.format_glossary(template, data["glossary"], args["nocap"]) end
     if not args["notext"] then out = format_solo_text(template, data["text"], args["nocap"]) end
     return export.format_etymology(out, categories)
     return format_etymology(out, categories)
end
end


function export.single_term_etymology(template, frame)
local function single_term_etymology(template, frame)
     local data, args = single_term_data[template], m_parameters.process(frame:getParent().args, single_term_params)
     local data, args = single_term_data[template], m_parameters.process(frame:getParent().args, single_term_params)
     local out, categories = "", {}
     local out, categories = "", {}
     local language = m_languages.get_by_code(args[1])
     local language = m_languages.get_by_code(args[1])
     if not args["notext"] then out = out .. export.format_glossary(template, data["glossary"], args["nocap"]) .. " of " end
     if not args["notext"] then out = out .. format_prefixed_text(template, data["text"], args["nocap"]) end
     out = out .. m_links.full_link({
     out = out .. m_links.full_link({
         term = args[2],
         term = args[2],
Line 196: Line 199:
         pos = args["pos"],
         pos = args["pos"],
         nobold = true,
         nobold = true,
     },"term")
     }, "term")
     if not args["nocat"] then categories = export.hydrate_categories(data["categories"], language) end
     if not args["nocat"] then categories = hydrate_categories(data["categories"], language) end
     return export.format_etymology(out, categories)
     return format_etymology(out, categories)
end
end


function export.derived_term_etymology(template, frame)
local function derived_term_etymology(template, frame)
     local data, args = derived_term_data[template], m_parameters.process(frame:getParent().args, derived_term_params)
     local data, args = derived_term_data[template], m_parameters.process(frame:getParent().args, derived_term_params)
     local out, categories = "", {}
     local out, categories = "", {}
     local language_to = m_languages.get_by_code(args[1])
     local language_to = m_languages.get_by_code(args[1])
     local language_from = m_languages.get_by_code(args[2])
     local language_from = m_languages.get_by_code(args[2])
     if (not args["notext"]) and (not data["silent"]) then out = out .. export.format_glossary(template, data["glossary"], args["nocap"]) .. " of " end
     if (not args["notext"]) and (not data["silent"]) then out = out .. format_prefixed_text(template, data["text"], args["nocap"]) end
     out = out .. m_links.full_link({
     out = out .. m_links.full_link({
         term = args[3],
         term = args[3],
Line 215: Line 218:
         showlanguage = true,
         showlanguage = true,
         nobold = true,
         nobold = true,
     },"term")
     }, "term")
     if not args["nocat"] then categories = export.hydrate_categories(data["categories"], language_to, language_from) end
     if not args["nocat"] then categories = hydrate_categories(data["categories"], language_to, language_from) end
     return export.format_etymology(out, categories)
     return format_etymology(out, categories)
end
end


function export.affix_etymology(frame)
local function affix_etymology(frame)
     local args = m_parameters.process(frame:getParent().args, affix_params)
     local args = m_parameters.process(frame:getParent().args, affix_params)
     local pre_out, categories = {}, {}
     local pre_out, categories = {}, {}
Line 233: Line 236:
         if not args["nocat"] then
         if not args["nocat"] then
             if args["noaff"][i] then
             if args["noaff"][i] then
                 --cool, this is a marked non-affix, don't let it be classified as one!
                 -- this is a marked non-affix, don't let it be classified as one!
             elseif language_from then
             elseif language_from then
            table.insert(categories, language_to.name .. " terms derived from " .. language_from.name)
                table.insert(categories, language_to.name .. " terms derived from " .. language_from.name)
            if is_infix(term) or is_prefix(term) or is_suffix(term) then n_affixes = n_affixes + 1 end
                if is_infix(term) or is_prefix(term) or is_suffix(term) then n_affixes = n_affixes + 1 end
             elseif is_infix(term) then
             elseif is_infix(term) then
                 table.insert(categories, language_to.name .. " terms infixed with " .. cite_term)
                 table.insert(categories, language_to.name .. " terms infixed with " .. cite_term)
Line 256: Line 259:
             showlanguage = (language_from and true),
             showlanguage = (language_from and true),
             nobold = true,
             nobold = true,
         },"term"))
         }, "term"))
     end
     end
     if (not args["nocat"]) and (n_parts > 1 and n_affixes == 0) then
     if (not args["nocat"]) and (n_parts > 1 and n_affixes == 0) then
         table.insert(categories, language_to.name .. " compound terms")
         table.insert(categories, language_to.name .. " compound terms")
     end
     end
     return export.format_etymology(table.concat(pre_out, " + "), categories)
     return format_etymology(table.concat(pre_out, " + "), categories)
end
end


function export.show(frame)
function export.show(frame)
     local template = frame.args[1]
     local template = frame.args[1]
     if no_term_data[template] then return export.no_term_etymology(template, frame) end
     if no_term_data[template] then return no_term_etymology(template, frame) end
     if single_term_data[template] then return export.single_term_etymology(template, frame) end
     if single_term_data[template] then return single_term_etymology(template, frame) end
     if derived_term_data[template] then return export.derived_term_etymology(template, frame) end
     if derived_term_data[template] then return derived_term_etymology(template, frame) end
     if template == "affix" then return export.affix_etymology(frame) end
     if template == "affix" then return affix_etymology(frame) end
     error("No such sub-template type is defined!")
     error("No such sub-template type is defined!")
end
end


return export
return export

Revision as of 12:36, 20 February 2024

The etymology module provides functionality for various etymology templates:


local export = {}

local m_languages = require("Module:languages")
local m_links = require("Module:links")
local m_parameters = require("Module:parameters")

local no_term_params = {
    [1] = {required = true},
    ["nocap"] = {type = "boolean"},
    ["notext"] = {type = "boolean"},
    ["nocat"] = {type = "boolean"},
}
local no_term_data = {
    ["onomatopoeic"] = {
        categories = {"onomatopoeias"},
        text = true,
    },
    ["unknown"] = {
        categories = {"terms with unknown etymologies"},
        text = function(nocap) return (nocap and "u" or "U") .. "nknown" end,
    },
}

local single_term_params = {
    [1] = {required = true},
    [2] = {required = true},
    [3] = {alias_of = "alt"},
    [4] = {alias_of = "t"},
    ["alt"] = {},
    ["t"] = {},
    ["pos"] = {},
    ["nocap"] = {type = "boolean"},
    ["notext"] = {type = "boolean"},
    ["nocat"] = {type = "boolean"},
}
local single_term_data = {
    ["abbreviation"] = {
        text = true,
        categories = {"abbreviations"}
    },
    ["back-formation"] = {
        text = true,
        categories = {"back-formations"}
    },
    ["clipping"] = {
        text = true,
        categories = {"clippings"},
    },
    ["doublet"] = {
        text = true,
        categories = {"doublets"},
    },
    ["ellipsis"] = {
        text = true,
        categories = {"ellipses"},
    },
    ["initialism"] = {
        text = true,
        categories = {"initialisms"},
    },
    ["rebracketing"] = {
        text = true,
        categories = {"rebracketings"},
    },
    ["reduplication"] = {
        text = true,
        categories = {"reduplications"},
    },
}

local derived_term_params = {
    [1] = {required = true},
    [2] = {required = true},
    [3] = {required = true},
    [4] = {alias_of = "alt"},
    [5] = {alias_of = "t"},
    ["alt"] = {},
    ["t"] = {},
    ["pos"] = {},
    ["nocap"] = {type = "boolean"},
    ["notext"] = {type = "boolean"},
    ["nocat"] = {type = "boolean"},
}
local derived_term_data = {
    ["borrowed"] = {
        categories = {"terms borrowed from"},
    },
    ["calque"] = {
        text = function(nocap) return (nocap and "b" or "B") .. "y [[Appendix:Glossary#surface analysis|surface analysis]], " end,
        categories = {"terms calqued from"},
    },
    ["derived"] = {
        categories = {"terms derived from"},
    },
    ["inherited"] = {
        categories = {"terms inherited from"},
    },
    ["learned borrowing"] = {
        text = true,
        categories = {"terms borrowed from", "learned borrowings from"},
    },
    ["orthographic borrowing"] = {
        text = true,
        categories = {"terms borrowed from", "orthographic borrowings from"},
    },
    ["semantic loan"] = {
        text = true,
        categories = {"terms derived from", "semantic loans from"},
    },
    ["phono-semantic matching"] = {
        text = true,
        categories = {"terms derived from", "phono-semantic matchings from"},
    },
    ["transliteration"] = {
        text = true,
        categories = {"terms derived from", "transliterations of"},
    },
}

local affix_params = {
    [1] = {required = true},
    [2] = {list = true},
    ["t"] = {list = true, allow_holes = true},
    ["l"] = {list = true, allow_holes = true},
    ["alt"] = {list = true, allow_holes = true},
    ["pos"] = {list = true, allow_holes = true},
    ["noaff"] = {list = true, allow_holes = true, type = "boolean"},
    ["nocat"] = {type = "boolean"},
}
local affix_delimiter = {
    ["-"] = true,
    ["·"] = true,
}

local function is_infix(word)
    return affix_delimiter[mw.ustring.sub(word, 1, 1)] and affix_delimiter[mw.ustring.sub(word, -1)]
end

local function is_prefix(word)
    return affix_delimiter[mw.ustring.sub(word, -1)]
end

local function is_suffix(word)
    return affix_delimiter[mw.ustring.sub(word, 1, 1)]
end

local function format_etymology(out, categories)
    for _, category in ipairs(categories) do
        out = out .. "[[Category:" .. category .. "]]"
    end
    return out
end

local function format_solo_text(label, text_data, nocap_arg)
    if not text_data then return "" end
    if type(text_data) == "function" then return text_data(nocap_arg) end
    return "[[Appendix:Glossary#" .. label .. "|" .. (nocap_arg and label or mw.ustring.gsub(label, "^%l", string.upper)) .. "]]"
end

local function format_prefixed_text(label, text_data, nocap_arg)
    if not text_data then return "" end
    if type(text_data) == "function" then return text_data(nocap_arg) end
    return "[[Appendix:Glossary#" .. label .. "|" .. (nocap_arg and label or mw.ustring.gsub(label, "^%l", string.upper)) .. "]] of "
end

local function hydrate_category(category, language_to, language_from)
    local new_category = language_to.name .. " " .. category
    if language_from then new_category = new_category .. " " .. language_from.name end
    return new_category
end

local function hydrate_categories(categories, language_to, language_from)
    local new_categories = {}
    for i, category in ipairs(categories) do
        new_categories[i] = hydrate_category(category, language_to, language_from)
    end
    return new_categories
end

local function no_term_etymology(template, frame)
    local data, args = no_term_data[template], m_parameters.process(frame:getParent().args, no_term_params)
    local out, categories = "", {}
    local language = m_languages.get_by_code(args[1])
    if not args["nocat"] then categories = hydrate_categories(data["categories"], language) end
    if not args["notext"] then out = format_solo_text(template, data["text"], args["nocap"]) end
    return format_etymology(out, categories)
end

local function single_term_etymology(template, frame)
    local data, args = single_term_data[template], m_parameters.process(frame:getParent().args, single_term_params)
    local out, categories = "", {}
    local language = m_languages.get_by_code(args[1])
    if not args["notext"] then out = out .. format_prefixed_text(template, data["text"], args["nocap"]) end
    out = out .. m_links.full_link({
        term = args[2],
        language = language,
        alt = args["alt"],
        gloss = args["t"],
        pos = args["pos"],
        nobold = true,
    }, "term")
    if not args["nocat"] then categories = hydrate_categories(data["categories"], language) end
    return format_etymology(out, categories)
end

local function derived_term_etymology(template, frame)
    local data, args = derived_term_data[template], m_parameters.process(frame:getParent().args, derived_term_params)
    local out, categories = "", {}
    local language_to = m_languages.get_by_code(args[1])
    local language_from = m_languages.get_by_code(args[2])
    if (not args["notext"]) and (not data["silent"]) then out = out .. format_prefixed_text(template, data["text"], args["nocap"]) end
    out = out .. m_links.full_link({
        term = args[3],
        language = language_from,
        alt = args["alt"],
        gloss = args["t"],
        pos = args["pos"],
        showlanguage = true,
        nobold = true,
    }, "term")
    if not args["nocat"] then categories = hydrate_categories(data["categories"], language_to, language_from) end
    return format_etymology(out, categories)
end

local function affix_etymology(frame)
    local args = m_parameters.process(frame:getParent().args, affix_params)
    local pre_out, categories = {}, {}
    local language_to = m_languages.get_by_code(args[1])
    local n_parts, n_affixes = 0, 0
    for i, term in ipairs(args[2]) do
        n_parts = n_parts + 1
        local language_from = nil
        if args["l"][i] then language_from = m_languages.get_by_code(args["l"][i]) end
        local cite_term = term
        if (language_from and language_from.proto) or ((not language_from) and language_to.proto) then cite_term = "*" .. cite_term end
        if not args["nocat"] then
            if args["noaff"][i] then
                -- this is a marked non-affix, don't let it be classified as one!
            elseif language_from then
                table.insert(categories, language_to.name .. " terms derived from " .. language_from.name)
                if is_infix(term) or is_prefix(term) or is_suffix(term) then n_affixes = n_affixes + 1 end
            elseif is_infix(term) then
                table.insert(categories, language_to.name .. " terms infixed with " .. cite_term)
                n_affixes = n_affixes + 1
            elseif is_prefix(term) then
                table.insert(categories, language_to.name .. " terms prefixed with " .. cite_term)
                n_affixes = n_affixes + 1
            elseif is_suffix(term) then
                table.insert(categories, language_to.name .. " terms suffixed with " .. cite_term)
                n_affixes = n_affixes + 1
            end
        end
        table.insert(pre_out, m_links.full_link({
            term = term,
            language = language_from or language_to,
            alt = args["alt"][i],
            gloss = args["t"][i],
            pos = args["pos"][i],
            showlanguage = (language_from and true),
            nobold = true,
        }, "term"))
    end
    if (not args["nocat"]) and (n_parts > 1 and n_affixes == 0) then
        table.insert(categories, language_to.name .. " compound terms")
    end
    return format_etymology(table.concat(pre_out, " + "), categories)
end

function export.show(frame)
    local template = frame.args[1]
    if no_term_data[template] then return no_term_etymology(template, frame) end
    if single_term_data[template] then return single_term_etymology(template, frame) end
    if derived_term_data[template] then return derived_term_etymology(template, frame) end
    if template == "affix" then return affix_etymology(frame) end
    error("No such sub-template type is defined!")
end

return export