ข้ามไปเนื้อหา

มอดูล:fa-headword

จาก วิกิพจนานุกรม พจนานุกรมเสรี

local lang = require("Module:languages").getByCode("fa")

local export = {}
local pos_functions = {}

local u = mw.ustring.char

local A = u(0x064E) -- fatḥa
local AN = u(0x064B) -- fatḥatān (fatḥa tanwīn)
local U = u(0x064F) -- ḍamma
local I = u(0x0650) -- kasra
local SK = u(0x0652) -- sukūn = no vowel
local SH = u(0x0651) -- šadda = gemination of consonants

-----------------------
-- Utility functions --
-----------------------

-- If Not Empty
local function ine(arg)
    if arg == "" then
        return nil
    else
        return arg
    end
end

local function list_to_set(list)
    local set = {}
    for _, item in ipairs(list) do set[item] = true end
    return set
end

-- version of mw.ustring.gsub() that discards all but the first return value
function rsub(term, foo, bar)
    local retval = mw.ustring.gsub(term, foo, bar)
    return retval
end

local rfind = mw.ustring.find

-- Tracking functions

local trackfn = require("Module:debug").track

function track(page)
    trackfn("fa-headword/" .. page)
    return true
end

local function append_cat(data, pos)
    --table.insert(data.categories, lang:getCanonicalName() .. " " .. pos)
    table.insert(data.categories, pos .. lang:getCategoryName())
end

function remove_links(text)
    text = rsub(text, "%[%[[^|%]]*|", "")
    text = rsub(text, "%[%[", "")
    text = rsub(text, "%]%]", "")
    return text
end

local function make_unused_key_tracker(t)
    local unused_keys = require"Module:table".listToSet(
                            require"Module:table".keysToList(t))
    local mt = {
        __index = function(self, key)
            if key ~= nil then unused_keys[key] = nil end
            return t[key]
        end,
        __newindex = function(self, key, value) t[key] = value end
    }
    local proxy_table = setmetatable({}, mt)
    return proxy_table, unused_keys
end

function export.ZWNJ(word)
    if mw.ustring.match(word,
                        "[بپتثجچحخسشصضطظعغفقکگلمنهی]",
                        -1) then
        return "\226\128\140" -- U+200C ZERO WIDTH NON-JOINER
    end
    return "" -- empty string
end

-- The main entry point.
function export.show(frame)

    local PAGENAME = mw.title.getCurrentTitle().text

    local poscat = frame.args[1] or error(
                       "Part of speech has not been specified. Please pass parameter 1 to the module invocation.")

    local params = {
        [1] = {list = "head", allow_holes = true, default = ""},
        ["head"] = {default = ""},
        ["tr"] = {list = true, allow_holes = true}
    }

    local args, unused_keys = make_unused_key_tracker(frame:getParent().args)

    -- Gather parameters
    local data = {
        lang = lang,
        pos_category = poscat,
        categories = {},
        heads = {},
        translits = {},
        inflections = {}
    }

    local head = args[1] or args["head"] or PAGENAME or ""
    local translit = ine(args["tr"])
    local i = 1

    local irreg_translit = false

    while head do
        table.insert(data.heads, head)
        data.translits[#data.heads] = translit

        i = i + 1
        head = ine(args["head" .. i])
        translit = ine(args["tr" .. i])
    end

    if pos_functions[poscat] then pos_functions[poscat].func(args, data) end

    local unused_key_list = require"Module:table".keysToList(unused_keys)
    if #unused_key_list > 0 then
        local unused_key_string = require "Module:array"(unused_key_list):map(
                                      function(key)
                return "|" .. key .. "=" .. args[key]
            end):concat("\n")
        error("Unused arguments: " .. unused_key_string)
    end

    return require("Module:headword").full_headword(data)
end

-- Get a list of inflections. See handle_infl() for meaning of ARGS, ARGPREF
local function getargs(args, argpref)
    -- Gather parameters
    local forms = {}

    form = ine(args[argpref])

    local translit = ine(args[argpref .. "tr"])
    local i = 1

    while form do

        table.insert(forms, {term = form, translit = translit})

        i = i + 1
        form = ine(args[argpref .. i])
        translit = ine(args[argpref .. i .. "tr"])
    end

    return forms
end

local function handle_infl(args, data, argpref, label)
    local newinfls = getargs(args, argpref)
    newinfls.label = label

    if #newinfls > 0 then table.insert(data.inflections, newinfls) end
end

local function handle_all_infl(args, data, argpref, label, nobase)
    if not nobase and argpref ~= "" then
        handle_infl(args, data, argpref, label)
    end
end

pos_functions["คำกริยา"] = {
    func = function(args, data)
        data.pos_category = "คำกริยา"
        handle_all_infl(args, data, "prstem", "present stem")
    end
}

pos_functions["คำคุณศัพท์"] = {
    func = function(args, data)
        data.pos_category = "คำคุณศัพท์"
        if ine(args["c"]) and args["c"] == "+" then
            local word = data.heads[1]
            local word_tr = args["tr"]

            table.insert(data.inflections, {
                label = "ขั้นกว่า",
                accel = {form = "comparative", translit = word_tr .. "-tar"},
                {
                    term = word .. export.ZWNJ(word) .. "ت" .. A .. "ر",
                    translit = word_tr .. "-tar"
                }
            })

            table.insert(data.inflections, {
                label = "ขั้นสุด",
                accel = {form = "superlative", translit = word_tr .. "-tarin"},
                {
                    term = word .. export.ZWNJ(word) .. "ت" .. A .. "رین",
                    translit = word_tr .. "-tarin"
                }
            })
        end
    end
}
return export