Jump to content

Module:umu-IPA

From Wiktionary, the free dictionary


-- umu-IPA module
local umu_IPA = {}

local lang = require("Module:languages").getByCode("umu")
local sc = require("Module:scripts").getByCode("Latn")
local m_IPA = require("Module:IPA")

local gsub = mw.ustring.gsub
local gmatch = mw.ustring.gmatch


local replacements = {
    ["aa"] = "ā", ["ee"] = "ē", ["ii"] = "ī", ["oo"] = "ō",
    ["ch"] = "č", ["sh"] = "š", ["zh"] = "ž",
    ["kw"] = "ḱ", ["gw"] = "ǵ",
    ["mb"] = "ḅ", ["nd"] = "ḍ", ["ng"] = "ġ"
}

function umu_IPA.transliterate(text)
    local output = text
    
    -- Perform all replacements
    for key, value in pairs(replacements) do
        output = output:gsub(key, value)
    end
    
    return output
end


local correspondences = {
    -- vowels
    ["á"] = "ɒ", ["é"] = "ɛ", ["í"] = "i", ["ó"] = "o", ["ú"] = "ə", --ignore accents
    ["à"] = "ɒ", ["è"] = "ɛ", ["ì"] = "i", ["ò"] = "o", ["ù"] = "ə", 
    ["û"] = "", 
    ["a"] = "ɒ", ["e"] = "ɛ", ["i"] = "i", ["o"] = "o", ["u"] = "ə",
    ["ā"] = "aː", ["ē"] = "ɛː", ["ī"] = "iː", ["ō"] = "oː",
    --["uy"] = "ɪj", ["uw"] = "ʊw",
    
    -- consonants
    ["č"] = "tʃ", ["š"] = "ʃ", ["ž"] = "ʒ", ["y"] = "j",
    ["ḱ"] = "kʷ", ["ǵ"] = "gʷ", -- labialized consonants
    ["ḅ"] = "ᵐb", ["ḍ"] = "ⁿd", ["ġ"] = "ᵑɡ" --prenasalized consonants
}

function umu_IPA.link(term)
    return require("Module:links").full_link{ term = term, lang = lang, sc = sc }
end

function umu_IPA.toIPA(text)
    local result = {}

    local translit = umu_IPA.transliterate(text)
    if not translit then
        error('The term "' .. text .. '" could not be transliterated.')
    end

    for character in gmatch(translit, ".") do
        table.insert(result, correspondences[character] or character)
    end

    result = table.concat(result)

    return result
end

function umu_IPA.make(frame)
    local args = frame.args

    local p, results = {}, {}

    if args[1] then
        for index, item in ipairs(args) do
            table.insert(p, (item ~= "") and item or nil)
        end
    else
        p = { mw.title.getCurrentTitle().text }
    end

    for _, Val in ipairs(p) do
        table.insert(results, { pron = "/" .. umu_IPA.toIPA(Val) .. "/" })
    end

    return '* ' .. m_IPA.format_IPA_full { lang = lang, items = results }
end

function umu_IPA.show(frame)
    local args = frame.args
    local page_title = mw.title.getCurrentTitle().text
    local text = args[1] or page_title

    local IPA_text = umu_IPA.toIPA(text)

    return m_IPA.format_IPA_full { lang = lang, items = {{ pron = "/" .. IPA_text .. "/" }} }
end

return umu_IPA