Jump to content

Module:ajp-IPA

From Wiktionary, the free dictionary


local export = {}

local lang = require("Module:languages").getByCode("ajp")
local m_IPA = require("Module:IPA")
local gsub = mw.ustring.gsub

local correspondences = {
	["g"] = "ɡ",
	["ḵ"] = "x",
	["ḥ"] = "ħ",
	["ḡ"] = "ɣ",
	["ṣ"] = "sˤ",
	["ḍ"] = "dˤ",
	["ẓ"] = "zˤ",
	["ṭ"] = "tˤ",
	["š"] = "ʃ",
	["j"] = "ʒ",
	["ā"] = "aː",
	["ū"] = "uː",
	["ō"] = "oː",
	["ī"] = "iː",
	["ē"] = "eː",
	["y"] = "j",
	["ṯ"] = "θ",
	["ḏ"] = "ð",
	["ḅ"] = "bˤ",
	["ḷ"] = "lˤ",
	["ṃ"] = "mˤ",
}

local vowels = "aeiuoāēīūō"
local vowel = "[" .. vowels .. "]"
local long_vowels = "āēīūō"
local long_vowel = "[" .. long_vowels .. "]"
local consonant = "[^" .. vowels .. ". -]"
local tie = "‿"
local syllabify_pattern = "(" .. vowel .. ")(" .. consonant .. "?)(" ..
		consonant .. "?)(" .. vowel .. ")"

local function parse_params(item)
	local term = item
	local phonetic = {}
	local qualifier = {}
	while true do
		local new_term, angle_bracketed = term:match("^(.-)(%b<>)$")
		if not new_term then
			break
		end
		local prefix, content = angle_bracketed:match "^<(%w+):(.+)>$"
		if not prefix then
			break
		end
		if prefix == "q" then
			table.insert(qualifier, 1, content)
		elseif prefix == "p" then
			table.insert(phonetic, 1, content)
		else
			error("Unrecognized prefix '" .. prefix .. "'")
			break
		end
		term = new_term
	end
	return term, phonetic, qualifier
end

local function syllabify(text)
	text = gsub(text, "%-(" .. consonant .. ")%-(" .. consonant .. ")", "%1.%2")
	text = gsub(text, "%-", ".")

	-- Add syllable breaks.
	for _ = 1, 2 do
		text = gsub(text, syllabify_pattern, function(a, b, c, d)
			if c == "" and b ~= "" then
				c, b = b, ""
			end

			return a .. b .. "." .. c .. d
		end)
	end

	-- Add ties between word-final vowels and word-initial consonant clusters.
	text = gsub(text, "(" .. vowel .. ") (" .. consonant .. ")%.?(" ..
			consonant .. ")", "%1" .. tie .. "%2.%3")

	return text
end

local function pronunciation_phonemic(word)

	word = syllabify(word)
	word = gsub(word, '.', correspondences)

	return word
end

function export.show(frame)

	local args = require("Module:parameters").process(frame:getParent().args, {
		[1] = { list = true }
	})

	local words = args[1]

	local pronunciation = ""

	for i, item in ipairs(words) do
		local transcriptions = {}
		local term, phonetic, qualifier = parse_params(item)
		table.insert(transcriptions, { pron = "/" .. pronunciation_phonemic(term) .. "/", qualifiers = qualifier })
		if phonetic then
			for _, p_item in ipairs(phonetic) do
				table.insert(transcriptions, { pron = "[" .. p_item .. "]" })
			end
		end
		if i > 1 then
			pronunciation = pronunciation .. "\n* "
		end
		pronunciation = pronunciation .. m_IPA.format_IPA_full { lang = lang, items = transcriptions }
	end

	return pronunciation
end

return export