انتقل إلى المحتوى

وحدة:IPA

من ويكاموس، القاموس الحر

يمكن إنشاء صفحة توثيق الوحدة في وحدة:IPA/شرح

local p = {}

-- Function to map English phonemes to IPA symbols for American English
local function phonemeToIPA(phoneme)
    local ipaMap = {
        ["a"] = "æ",    -- as in "cat"
        ["ai"] = "eɪ",  -- as in "rain"
        ["au"] = "ɔ",   -- as in "caught"
        ["aw"] = "ɔ",   -- as in "saw"
        ["b"] = "b",    -- as in "bat"
        ["ch"] = "tʃ",  -- as in "chop"
        ["d"] = "d",    -- as in "dog"
        ["ea"] = "i",   -- as in "bead"
        ["ee"] = "i",   -- as in "see"
        ["ei"] = "eɪ",  -- as in "they"
        ["f"] = "f",    -- as in "fish"
        ["g"] = "g",    -- as in "go"
        ["gh"] = "f",   -- as in "enough"
        ["h"] = "h",    -- as in "hat"
        ["i"] = "ɪ",    -- as in "bit"
        ["ie"] = "aɪ",  -- as in "die"
        ["j"] = "dʒ",   -- as in "jam"
        ["k"] = "k",    -- as in "cat"
        ["l"] = "l",    -- as in "lamp"
        ["m"] = "m",    -- as in "man"
        ["n"] = "n",    -- as in "no"
        ["o"] = "oʊ",   -- as in "go"
        ["oe"] = "oʊ",  -- as in "foe"
        ["ou"] = "aʊ",  -- as in "out"
        ["ow"] = "aʊ",  -- as in "cow"
        ["p"] = "p",    -- as in "pen"
        ["qu"] = "kw",  -- as in "quick"
        ["r"] = "ɹ",    -- as in "red" (American English)
        ["s"] = "s",    -- as in "snake"
        ["sh"] = "ʃ",   -- as in "shoe"
        ["t"] = "t",    -- as in "top"
        ["u"] = "ju",   -- as in "unicorn"
        ["ue"] = "ju",  -- as in "blue"
        ["v"] = "v",    -- as in "van"
        ["w"] = "w",    -- as in "water"
        ["x"] = "ks",   -- as in "box"
        ["y"] = "j",    -- as in "yes"
        ["z"] = "z",    -- as in "zebra"
        ["ng"] = "ŋ",   -- as in "sing"
        ["er"] = "ɜr",  -- as in "her"
        ["ar"] = "ɑr",  -- as in "car"
        ["or"] = "ɔr",  -- as in "for"
        ["air"] = "ɛr", -- as in "air"
        ["ear"] = "ɪr", -- as in "ear"
        ["ure"] = "jʊr", -- as in "sure"
        ["sch"] = "ʃ",  -- as in "school"
        ["wr"] = "",     -- silent 'w' as in "write"
        ["igh"] = "aɪ", -- as in "high"
        ["ough"] = "ɔ", -- as in "thought" or "fought"
        ["c"] = "k",    -- as in "cat" (when hard)
        ["ci"] = "s",   -- as in "city"
        ["ge"] = "dʒ",  -- as in "gem"
        ["gu"] = "g",   -- as in "guitar"
        ["qu"] = "kw",  -- as in "quick"
        ["th"] = "θ",   -- as in "think"
        ["th_"] = "ð",  -- as in "this" (contextual)
    }
    return ipaMap[phoneme] or phoneme -- Return the phoneme if not found
end

-- Function to generate IPA pronunciation from American English text
function p.generateIPA(text)
    local ipa = ""
    local i = 1
    local length = #text

    while i <= length do
        local letter = text:sub(i, i):lower()
        
        -- Check for two-letter combinations first
        if i < length then
            local nextLetter = text:sub(i + 1, i + 1):lower()
            local digraph = letter .. nextLetter
            
            if phonemeToIPA(digraph) ~= digraph then
                ipa = ipa .. phonemeToIPA(digraph)
                i = i + 2 -- Move past the digraph
            else
                -- Handle "th" based on context
                if letter == "t" and nextLetter == "h" then
                    if i > 1 and text:sub(i - 1, i - 1):lower() ~= " " then
                        ipa = ipa .. phonemeToIPA("th_") -- Use voiced "th" if not at the start
                    else
                        ipa = ipa .. phonemeToIPA("th") -- Use voiceless "th" if at the start
                    end
                    i = i + 2 -- Move past "th"
                else
                    ipa = ipa .. phonemeToIPA(letter)
                    i = i + 1
                end
            end
        else
            ipa = ipa .. phonemeToIPA(letter)
            i = i + 1
        end
    end

    return ipa
end

return p