Jump to content

Module:aau-noun

From Wiktionary, the free dictionary


local export = {}

-- Mapping from Roman numeral (uppercase) to Arabic numeral
local roman_to_arabic = {
    ["I"] = 1, ["II"] = 2, ["III"] = 3, ["IV"] = 4, ["V"] = 5, ["VI"] = 6,
    ["VII"] = 7, ["VIII"] = 8, ["IX"] = 9, ["X"] = 10, ["XI"] = 11, ["XII"] = 12
}

-- Short descriptions for noun classes (based on Lock 2011, p. 57, 59-61)
local class_descriptions = {
    [1] = "humans/spirits",
    [2] = "non-human/default",
    [3] = "small 3D objects",
    [4] = "flat objects/experience",
    [5] = "long thin objects",
    [6] = "geographical locations",
    [7] = "flat 2D objects",
    [8] = "selected trees",
    [9] = "stored long bundles",
    [10] = "temporal",
    [11] = "transported long bundles",
    [12] = "pieces/chunks"
}

-- Full descriptions for gender codes
local gender_descriptions = {
    ["m"] = "masculine",
    ["f"] = "feminine",
    ["mf"] = "masculine+feminine"
}

function export.show(frame)
    local args = frame:getParent().args
    local class_input = args[1]
    local gender_input = args[2]

    local class_arabic = nil
    local class_display = tostring(class_input or "") -- Default to input string or empty

    -- Validate and process class input (handle numbers and Roman numerals)
    if class_input then
        -- Check if it's a number string
        local num_val = tonumber(class_input)
        if num_val and num_val >= 1 and num_val <= 12 then
            class_arabic = num_val
            -- Assume Roman numeral display for 1-12 as in the grammar tables
             local arabic_to_roman = {}
             for roman, arabic in pairs(roman_to_arabic) do
                 arabic_to_roman[arabic] = roman
             end
            class_display = arabic_to_roman[class_arabic] -- Use Roman numeral
        else
            -- Check if it's a valid Roman numeral string (only I-XII)
            local roman_upper = mw.ustring.upper(tostring(class_input))
            if roman_to_arabic[roman_upper] and roman_to_arabic[roman_upper] >= 1 and roman_to_arabic[roman_upper] <= 12 then
                class_arabic = roman_to_arabic[roman_upper]
                class_display = roman_upper -- Display the uppercase Roman numeral
            else
                 -- Input is not a valid number 1-12 or Roman numeral I-XII
                 class_display = tostring(class_input) -- Display as given
            end
        end
    end

    -- Get class description for tooltip
    local class_tooltip = "invalid class"
    if class_arabic and class_descriptions[class_arabic] then
        class_tooltip = "noun class " .. class_arabic .. " (" .. class_descriptions[class_arabic] .. ")"
    end

    -- Validate and process gender input
    local gender_display = tostring(gender_input or "") -- Default to input string or empty
    local gender_tooltip = "invalid gender"
    local gender_lower = mw.ustring.lower(gender_display)
    if gender_descriptions[gender_lower] then
        gender_tooltip = gender_descriptions[gender_lower]
        gender_display = gender_lower -- Use lowercase code for consistent display if valid
    end

    -- Construct the output string using mw.text.tag for <abbr>
    -- This generates the wikitext "{{#tag:abbr|...}}" syntax.
    local class_abbr_wikitext = mw.text.tag('abbr', { title = class_tooltip }, class_display)
    local gender_abbr_wikitext = mw.text.tag('abbr', { title = gender_tooltip }, gender_display)

    -- Combine the parts into the final output string
    local output_string = "''class " .. class_abbr_wikitext .. " gender " .. gender_abbr_wikitext .. "''"

    return output_string
end

return export