Module:IPA

From Wikipedia

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

local bodge = require "Module:mw"

local z = {}

local function audiolink(s)
    local contents = "([[File:Speaker Icon.svg|13px|link=File:" .. s .. "|alt=]] [[:Media:" .. s .. "|listen]])"
    return mw.text.tag({name="small",params={class="nowrap"},contents=contents})
end

local function plainaudiolink(s)
    local contents = "[[File:Speaker Icon.svg|13px|link=Media:" .. s .. "|alt=Listen to this|Listen]]"
    return mw.text.tag({name="small",params={class="nowrap"},contents=contents})
end

local function IPA(s)
    return mw.text.tag({name="span",params={title="Representation in the International Phonetic Alphabet (IPA)",class="IPA"},contents=s})
end

local function HTITLE(s, title, dotted)
    local b
    if dotted then b = " dotted" else b = "" end
    return mw.text.tag({name="span",params={class="IPA",style="border-bottom:1px" .. b .. ";",title=title},contents=s})
end

local INprefixes = { IPA = "IPA:", lang = "Hindustani:", langhi = "Hindi:", langur = "Urdu:", pron = "pronounced", hi = "Hindi:", ur = "Urdu:" }

-- This is used by {{IPA-hns}}
function z.IPAhns(frame)
    local pframe = frame:getParent()
    local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
--  local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself

    local Word = args[1]
    if ( Word ) then Word = IPA("[[Wikipedia:IPA for Hindi and Urdu|[" .. Word .. "]]]") else Word = "" end
    local Prefix = args[2]
    if ( Prefix ) then 
        Prefix = INprefixes[Prefix]
        if ( Prefix ) then Prefix = Prefix .. " " else Prefix = "" end
    else 
        Prefix = "" 
    end
    local Audio = args[3]
    if ( Audio ) then Audio = " " .. audiolink(Audio) else Audio = "" end
    return Prefix .. Word .. Audio
end

local IPAprefixes = { 
    ["lang"]    = "English",
    ["english"]	= "English",
    ["pron"]	= "pronunciation:",
    ["pronunciation"]	= "pronunciation:",
    ["local"]	= "local",
    ["ipa"]	    = "[[International Phonetic Alphabet|IPA]]",
    ["us"]	    = "[[United States English|US]]",
    ["uk"]	    = "[[British English|UK]]"
}

local IPAH = {
    ["b"] = "'b' in 'buy'",
	["d"] = "'d' in 'dye'",
	["dj"] = "'d' in 'dew'",
	["ᵈ"] = "optional 'd'",
	["ð"] = "'th' in 'thy'",
	["dʒ"] = "'j' in 'jam'",
	["f"] = "'f' in 'find'",
	["ɡ"] = "'g' in 'guy'",
	["h"] = "'h' in 'high'",
	["j"] = "'y' in 'yes'",
	["ʲ"] = "optional 'j' in 'Lucas'",
	["k"] = "'k' in 'kind'",
	["l"] = "'l' in 'lie'",
	["lj"] = "'l' in 'lute'",
	["m"] = "'m' in 'my'",
	["n"] = "'n' in 'nigh'",
	["nj"] = "'n' in 'o'''n'''ion'",
	["ŋ"] = "'ng' in 'sing'",
	["ŋɡ"] = "'ng' in 'finger'",
	["θ"] = "'th' in 'thigh'",
	["θj"] = "'th' in 'enthusiasm'",
	["p"] = "'p' in 'pie'",
	["r"] = "'r' in 'rye'",
	["s"] = "'s' in 'sigh'",
	["sj"] = "'s' in 'suit'",
	["ʃ"] = "'sh' in 'shy'",
	["t"] = "'t' in 'tie'",
	["tj"] = "'t' in 'tune'",
	["ᵗ"] = "optional 't'",
	["tʃ"] = "'ch' in 'china'",
	["v"] = "'v' in 'vie'",
	["w"] = "'w' in 'wind'",
	["hw"] = "'wh' in 'why'",
	["z"] = "'z' in 'Zion'",
	["zj"] = "'z' in 'Zeus'",
	["ʒ"] = "'s' in 'pleasure'",
	["x"] = "'ch' in 'loch'",
	["ʔ"] = "the catch in 'uh-oh'",
	["ɑː"] = "'a' in 'father'",
	["ɑr"] = "'ar' in 'bard'",
	["ɒ"] = "short 'o' in 'body'",
	["ɒ̃ "] = "nasal 'an' in 'vin blanc'",
	["ɒr"] = "'or' in 'moral'",
	["æ"] = "short 'a' in 'bad'",
	["aɪ"] = "long 'i' in 'bide'",
	["aɪər"] = "'ire' in 'fire'",
	["aʊ"] = "'ou' in 'pout'",
	["aʊər"] = "'our' in 'hour'",
	["ɛ"] = "short 'e' in 'bed'",
	["ɛr"] = "'err' in 'merry'",
	["eɪ"] = "long 'a' in 'base'",
	["ær"] = "'arr' in 'marry'",
	["ɛər"] = "'are' in 'bare'",
	["ɪ"] = "short 'i' in 'bid'",
	["ɪr"] = "'irr' in 'mirror'",
	["iː"] = "long 'e' in 'bead'",
	["ⁱ"] = "optional 'i' in 'nasturtium'",
	["ɪər"] = "'ear' in 'beard'",
	["ɔː"] = "'au' in 'fraud'",
	["ɔr"] = "'or' in 'born'",
	["ɔɪ"] = "'oy' in 'boy'",
	["ɔɪər"] = "'oir' in 'loir' (rare)",
	["oʊ"] = "long 'o' in 'bode'",
	["ɔər"] = "'ore' in 'bore'",
	["ʊ"] = "short 'oo' in 'foot'",
	["ʊr"] = "'our' in 'courier'",
	["uː"] = "long 'oo' in 'food'",
	["ʊər"] = "'oor' in 'moor'",
	["juː"] = "long 'u' in 'cute'",
	["jʊər"] = "'ure' in 'cure'",
	["ʌ"] = "short 'u' in 'bud'",
	["ʌr"] = "'urr' in 'hurry'",
	["ɜr"] = "'ir' in 'bird'",
	["ə"] = "schwa 'a' in 'about'",
	["ər"] = "'er' in 'finger'",
	["ɨ"] = "schwa 'e' in 'roses'",
	["ɵ"] = "variable 1st 'o' in 'omission'",
	["ʉ"] = "variable 'u' in 'beautiful'",
	["jʉ"] = "variable 2nd 'u' in 'curriculum'",
	["ᵊ"] = "optional 'ə' in 'jewelry'",
	["ən"] = "'on' in 'button'",
	["əm"] = "'m' in 'rhythm'",
	["əl"] = "'le' in 'bottle'",
	["i"] = "'y' in 'happy'",
	["ˈ"] = "primary stress follows",
	["ˌ"] = "secondary stress follows",
	["."] = "syllable break",
	["-"] = "(affix)",
	["_"] = "''none'' (space)",
}

local SAMPA = {
    ["p"] = "p",
    ["b"] = "b",
    ["t"] = "t",
    ["d"] = "d",
    ["ts"] = "ts",
    ["dz"] = "dz",
    ["tS"] = "tʃ",
    ["dZ"] = "dʒ",
    ["c"] = "c",
    ["J\\"] = "ɟ",
    ["k"] = "k",
    ["g"] = "ɡ",
    ["q"] = "q",
    ["p\\"] = "ɸ",
    ["B"] = "β",
    ["f"] = "f",
    ["v"] = "v",
    ["T"] = "θ",
    ["D"] = "ð",
    ["s"] = "s",
    ["z"] = "z",
    ["S"] = "ʃ",
    ["Z"] = "ʒ",
    ["C"] = "ç",
    ["j\\"] = "ʝ",
    ["x"] = "x",
    ["G"] = "ɣ",
    [" "] = "ɰ",
    ["X\\"] = "ħ",
    ["?\\"] = "ʕ",
    ["h"] = "h",
    ["h\\"] = "ɦ",
    ["m"] = "m",
    ["F"] = "ɱ",
    ["n"] = "n",
    ["J"] = "ɲ",
    ["N"] = "ŋ",
    ["l"] = "l",
    ["L"] = "ʎ",
    ["5"] = "ɫ",
    ["4"] = "ɾ",
    ["r"] = "r",
    ["r\\"] = "ɹ",
    ["R"] = "ʀ",
    ["P"] = "ʋ",
    ["w"] = "w",
    ["H"] = "ɥ",
    ["j"] = "j",
    ["i"] = "i",
    ["I"] = "ɪ",
    ["e"] = "e",
    ["E"] = "ɛ",
    ["{"] = "æ",
    ["y"] = "y",
    ["2"] = "ø",
    ["9"] = "œ",
    ["1"] = "i",
    ["@"] = "ə",
    ["6"] = "ɐ",
    ["3"] = "ɜ",
    ["a"] = "a",
    ["}"] = "ʉ",
    ["8"] = "ɵ",
    ["&"] = "ɶ",
    ["M"] = "ɯ",
    ["7"] = "ɤ",
    ["V"] = "ʌ",
    ["A"] = "ɑ",
    ["u"] = "u",
    ["U"] = "ʊ",
    ["o"] = "o",
    ["O"] = "ɔ",
    ["Q"] = "ɒ",
}

-- This is used by {{IPAc-en}}
function z.IPAcen(frame)
    local pframe = frame:getParent()
    local args = pframe.args -- the arguments passed TO the template, in the wikitext that instantiates the template
--  local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself

    local index = 1
    
    local result = {}
    while true do
        local arg = args[index]
        if nil == arg then break end
        if "icon" == arg then
            table.insert(result, HTITLE("[[File:Loudspeaker.svg|13px|link=|alt=play]]", "pronunciation:"))
        else
            local prefix = IPAprefixes[arg]
            if nil == prefix then break end
            table.insert(result, prefix)
        end
        index = index + 1
    end
    
    local Audio = args.audio
    if ( Audio ) then 
        table.insert(result, plainaudiolink(Audio))
    end
    
    local phonemes = {}
    local failure = "?"
    while true do
        local arg = args[index]
        if nil == arg then break end
        local title = IPAH[arg] or IPAH[SAMPA[arg]]
        if nil ~= title then
            table.insert(phonemes, "[[Wikipedia:IPA for English#Key|" .. HTITLE(arg, title, true) .. "]]")
        else
            table.insert(phonemes, HTITLE(arg, "Unknown phoneme", false) .. "[[Category:Ill-formatted IPAc-en transclusions]]")
        end
        index = index + 1
    end
    local slash = "[[Wikipedia:IPA for English|/]]"
    local text = mw.text.tag({name="span",contents=table.concat(phonemes),params={class="IPA"}})
    table.insert(result, slash .. text .. slash)
    
    return table.concat(result, " ")
end

return z