Module:sce-pron

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Pronunciation module for Dongxiang language. See {{sce-pron}}. All tests passed. (refresh)

TextExpectedActual
test_phonemic_IPA:
Passedbaer/pɑˈɚ//pɑˈɚ/
Passedtiigha/tʰɯˈqɑ//tʰɯˈqɑ/
Passedniere/njəˈrə//njəˈrə/
Passedenzhegve/ənt͡ʂəˈʁə//ənt͡ʂəˈʁə/
Passedxiaojierun (xiaojieruŋ)/ɕjɑwt͡ɕjəˈruŋ//ɕjɑwt͡ɕjəˈruŋ/
Passedershi ('ershi)/ˈɚʂi//ˈɚʂi/
Passedruhher ('ruhher)/ˈʐuhɚ//ˈʐuhɚ/

local export = {}

local gsub = mw.ustring.gsub
local gsplit = mw.text.gsplit
local lower = mw.ustring.lower
local match = mw.ustring.match
local str = require("Module:string")
local trim = mw.text.trim

--[[
	Sourced from A grammatical overview of Santa Mongolian by Kenneth Lynn Field (1997),
	东乡语汉语词典 (Dongxiang-Chinese Dictionary) by Ma Guozhong (马国忠) (2001)
]]--
-- List of valid phonemes
local phons = {
	-- list of valid consonant phonemes
	["cons_m"] = {
		["b"] = "p", ["p"] = "pʰ", ["m"] = "m", ["f"] = "f",
		["d"] = "t", ["t"] = "tʰ", ["n"] = "n", ["l"] = "l", ["r"] = "r",
		["g"] = "k", ["k"] = "kʰ", ["gh"] = "q", ["kh"] = "qʰ",
		["h"] = "x", ["gv"] = "ʁ", ["hh"] = "h",
		["j"] = "t͡ɕ", ["q"] = "t͡ɕʰ", ["x"] = "ɕ",
		["zh"] = "t͡ʂ", ["ch"] = "t͡ʂʰ", ["sh"] = "ʂ",
		["z"] = "t͡s", ["c"] = "t͡sʰ", ["s"] = "s",
		["w"] = "w", ["y"] = "j"
	},
	-- list of valid vowel phomemes
	["vowel_m"] = {
		["iao"] = "jɑw", ["uai"] = "wɑj",
		["ai"] = "ɑj", ["ei"] = "əj", ["ao"] = "ɑw", ["ou"] = "əw", ["ii"] = "ɯ",
		["ia"] = "jɑ", ["ie"] = "jə", ["iu"] = "ju", ["ui"] = "wəj", ["ua"] = "wɑ",
		["a"] = "ɑ", ["o"] = "o", ["e"] = "ə", ["i"] = "i", ["u"] = "u"
	},
	-- list of phonetic diphthong representations
	["diph_t"] = {
		["iao"] = "i̯ɑu̯~ou̯", ["uai"] = "u̯ɑi̯~u̯ɛi̯",
		["ai"] = "ɑi̯~ɛi̯", ["ei"] = "ə̝i̯", ["ao"] = "ɑu̯~ou̯", ["ou"] = "əu̯~ɤu̯",
		["ia"] = "i̯ɑ", ["ie"] = "i̯ɛ", ["iu"] = "i̯u", "u̯ɑi̯~u̯ɛi̯", ["ui"] = "u̯(ə̝)i̯", ["ua"] = "u̯ɑ"
	}
}
-- List of valid graphemes
local graphs = {
	cons_g = "[bpmfdtnlrgkhjqxzcswy]?[hv]?", -- list of valid consonant graphemes
	vowel_g = "[aeiou]+" -- list of valid vowel graphemes
}

-- Returns the number of syllables in a given string
local function count_syll(text)
	return str.count(text, "ɚ") + str.count(text, "[aeiou]+")
end

-- Handles ambiguous syllable-final n
local function n_final(text)
	local n = match(text, "n[^aeiou]+")
	if n and str.count(text, n) > 1 then
		error("ŋ is ambiguous. Please provide the full respelling.")
	elseif text == "ŋ" then
		text = gsub(text, "n", "ŋ")
	end

	return text
end

-- Adjusts the text including respelling the term
local function adjust(text, etyl)
	text = lower(text) -- make text lowercase
	text = " " .. text .. " " -- make all word borders have a space
	text = gsub(text, "'", "ˈ") -- convert apostrophe to a stress mark
	text = gsub(text, "gvi", "gvii") -- i after gv is pronounced as ii
	text = gsub(text, "([gkaeiou ]hi)", "%1i") -- same after gh, kh and h
	text = gsub(text, "er([^aeiou])", "ɚ%1") -- "er" is a unique syllable in its own right
	if etyl == "zh" or etyl == "ar" then text = gsub(text, "r", "ʐ") end -- r is pronounced as ʐ in words of Chinese and Arabic origin

	return text
end

-- Convert the text to a phonemic IPA transcription
function export.IPA_m(text, etyl)
	text = n_final(text) -- handle syllable-final n
	text = adjust(text, etyl) -- adjust the text by respelling

	text = gsub(text, "[^ ]* ", function(word)
		if not match(word, "ˈ") and count_syll(word) > 1 then -- add stress mark to the final syllable if no stress mark is present
			word = match(word, "ɚ ") and gsub(word, "(" .. graphs.cons_g .. "ɚ) ", "ˈ%1 ") or gsub(word, "(" .. graphs.cons_g .. graphs.vowel_g .. "[^aeiou]*) ", "ˈ%1 ")
		end

		return gsub(word, graphs.cons_g, phons.cons_m):gsub(graphs.vowel_g, phons.vowel_m)
	end)

	return trim(text)
end

-- Convert the text to a phonetic IPA transcription from the phonemic transcription
function export.IPA_t(text, etyl)
	text = adjust(text, etyl) -- adjust the text by respelling
	gsub(graphs.vowel_g, phons.diph_t)

	return trim(text)
end

function export.show(frame)
	local args = frame:getParent().args -- get the arguments
	local etyl = args.etyl -- get the etymology
	local text = mw.title.getCurrentTitle().text -- get current page title
	local results = {} -- store the results

	if args[1] then
		for _, param in ipairs(args) do
			if param ~= "" then
				table.insert(results, {pron = "/" .. export.IPA_m(param, etyl) .. "/"})
			end
		end
	else
		table.insert(results, {pron = "/" .. export.IPA_m(text, etyl) .. "/"})
	end

	return "*" .. require("Module:IPA").format_IPA_full { lang = require("Module:languages").getByCode("sce"), items = results }
end

return export