Actions

Module

Module:lexicon

From SUALEX

Revision as of 07:32, 29 January 2026 by Jawad (talk | contribs) (Created page with "-- Module:lexicon local parser = require("Module:parser") local parameters = require("Module:parameters") local utilities = require("Module:utilities") local languages = require("Module:languages") local lexicon = {} local all_lexicons = {} function lexicon.get_all_languages() return require("Module:lexicon/data") end local function normalize_etymon(et) et = et or {} local norm = {} norm.descendant_type = et.descendant_type or "" norm.uncertainty...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:lexicon/doc

-- Module:lexicon

local parser = require("Module:parser")
local parameters = require("Module:parameters")
local utilities = require("Module:utilities")
local languages = require("Module:languages")

local lexicon = {}
local all_lexicons = {}

function lexicon.get_all_languages()
    return require("Module:lexicon/data")
end

local function normalize_etymon(et)
    et = et or {}
    local norm = {}
    norm.descendant_type = et.descendant_type or ""
    norm.uncertainty = (et.uncertainty == true)
    norm.items = {}
    if type(et.items) == "table" then
        for _, it in ipairs(et.items) do
            local nit = {}
            nit.lang_code = it.lang_code or "unknown"
            nit.etymon_item = it.etymon_item or ""
            nit.homonym = it.homonym or nil
            nit.display_form = it.display_form or nil
            nit.sup = it.sup or nil  -- item-level trailing sup (e.g. "?")
            table.insert(norm.items, nit)
        end
    end
    return norm
end

function lexicon.load_lexicon(lang_code)
    if all_lexicons[lang_code] then return all_lexicons[lang_code] end
    local ok, data = pcall(require, "Module:lexicon/data/" .. lang_code)
    data = ok and data or {}
    local parsed_entries = {}

    for _, entry in ipairs(data) do
        local parsed = parser.parse_entry(entry, lang_code) or {}

        local normalized_etymons = {}
        if parsed.etymon and type(parsed.etymon) == "table" then
            for _, raw_et in ipairs(parsed.etymon) do
                local n = normalize_etymon(raw_et)
                table.insert(normalized_etymons, n)
            end
        end
        parsed.etymon = normalized_etymons
        table.insert(parsed_entries, parsed)
    end

    all_lexicons[lang_code] = parsed_entries
    return parsed_entries
end

function lexicon.preload_all_lexicons()
    local langs = lexicon.get_all_languages()
    for _, lang in ipairs(langs) do
        lexicon.load_lexicon(lang)
    end
end

function lexicon.get_entry_by_term(lang_code, term_item, homonym)
    local entries = lexicon.load_lexicon(lang_code)
    for _, entry in ipairs(entries) do
        for _, term in ipairs(entry.term or {}) do
            if term.term_item == term_item and term.homonym == homonym then
                return entry
            end
        end
    end
    return nil
end

local function etymon_is_uncertain(etym)
    if not etym then return false end
    if etym.uncertainty == true then return true end
    for _, it in ipairs(etym.items or {}) do
        if it.sup == "?" then return true end
    end
    return false
end

local function etymon_suppressed(etym, options)
    if not options then return false end
    local t = etym.descendant_type
    if options.noinh and t == "inh" then return true end
    if options.noder and t == "der" then return true end
    if options.nobor and t == "bor" then return true end
    if options.nounc and etymon_is_uncertain(etym) then return true end
    return false
end

function lexicon.get_descendants(lang, item, homonym, options)
    local results = {}
    for _, l in ipairs(lexicon.get_all_languages()) do
        for _, entry in ipairs(lexicon.load_lexicon(l)) do
            for _, et in ipairs(entry.etymon or {}) do
                if not etymon_suppressed(et, options) then
                    for _, it in ipairs(et.items or {}) do
                        if it.lang_code == lang
                        and it.etymon_item == item
                        and (not homonym or it.homonym == homonym) then
                            table.insert(results, {
                                lang = l,
                                entry = entry,
                                matching_etym = et,
                                matching_item = it
                            })
                            break
                        end
                    end
                end
            end
        end
    end
    return results
end

function lexicon.get_sources(lang, item, homonym, options)
    local entry = lexicon.get_entry_by_term(lang, item, homonym)
    if not entry then return {} end
    local out = {}
    for _, et in ipairs(entry.etymon or {}) do
        if not etymon_suppressed(et, options) then
            table.insert(out, et)
        end
    end
    return out
end

return lexicon