Module:zhx-sic-pron

Hello, you have come here looking for the meaning of the word Module:zhx-sic-pron. In DICTIOUS you will not only get to know all the dictionary meanings for the word Module:zhx-sic-pron, but we will also tell you about its etymology, its characteristics and you will know how to say Module:zhx-sic-pron in singular and plural. Everything you need to know about the word Module:zhx-sic-pron you have here. The definition of the word Module:zhx-sic-pron will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofModule:zhx-sic-pron, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.

This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local export = {}
local m_string_utils = require("Module:string utilities")

local find = m_string_utils.find
local gsub = m_string_utils.gsub
local match = m_string_utils.match
local gmatch = m_string_utils.gmatch
local gsplit = mw.text.gsplit
local lower = m_string_utils.lower
local upper = m_string_utils.upper

local initialConv = {
	 = "p",  = "t",  = "k",
	 = "pʰ",  = "tʰ",  = "kʰ",
	 = "t͡s",  = "t͡ɕ",
	 = "t͡sʰ",  = "t͡ɕʰ",
	 = "m",  = "n",  = "nʲ",  = "ŋ",
	 = "f",  = "s",  = "ɕ",  = "x",
	 = "v",  = "z",
	 = "",
}

-- note that 'ir' is for internal use by the code and not used in actual sichuanese pinyin
local finalConv = {
	 = "z̩",  = "ɚ",

	 = "a",  = "o",  = "ɛ",
	 = "ai",  = "ei",  = "au",  = "əu",
	 = "an",  = "ən",  = "aŋ",  = "oŋ",

	 = "i",  = "ia",  = "iɛ",
	 = "iɛi",  = "iau",  = "iəu",
	 = "iɛn",  = "in",  = "iaŋ",

	 = "u",  = "ua",  = "uɛ",
	 = "uai",  = "uei",
	 = "uan",  = "uən",  = "uaŋ",

	 = "y",  = "yo",  = "ye",
	 = "yan",  = "yn",  = "yoŋ",
}

local toneConv = {
	 = "⁵⁵",  = "²¹",  = "⁵³",  = "²¹³",  = "⁻",
}

local initialConv_swz = {
	 = "g",  = "k",  = "l",  = "n",  = "x",  = "",  = "rh",
}
local finalConv_swz = {
	 = "",  = "r",  = "ung",  = "uong",  = "y",  = "iuo",  = "ye",  = "uan",  = "un",  = "yng",
}

local function fix(initial, final)
	-- ju /tɕy/
	if find(initial, '^$') and find(final, '^u') then
		final = gsub(final, '^u', 'ü')
	end
	
	if initial == 'y' then
		initial = ''
		if final == 'ou' then
			final = 'iu'
		elseif not find(final, '^') then -- yin /in/, yuan /yan/, ya /ia/
			final = 'i' .. final
		end
	end
	
	-- wei /uei/ (/-uei/ is usually spelled <-ui> but /uei/ is not <wui>)
	-- wu /vu/
	-- wai /uai/
	-- wen /uən/
	if initial == 'w' then
		initial = (final == 'u') and 'w' or ''
		if final == 'ei' then
			final = 'ui'
		elseif final == 'en' then
			final = 'un'
		elseif final ~= 'u' then
			final = 'u' .. final
		end
	end

	-- distinguish the two 'i's
	if find(initial, '^$') and final == 'i' then
		final = 'ir'
	end

	return initial, final
end

local function warn(initial, final, tone)
	if initial == "" and find(final, "^") then
		error("Syllables in Sichuanese Pinyin do not begin with i-/u-. Add y-/w-.")
	end
	
	if not initialConv and initial ~= "y" then
		error("Invalid initial: " .. initial)
	end

	if not finalConv and final ~= "uo" then
		error("Invalid final: " .. final)
	end

	if tone == "5" then
		error("Chengdu does not have the fifth tone anymore. Use 2.")
	end
end

function export.convert(text, scheme)
	if type(text) == "table" then
		text, scheme = text.args, text.args
	end

	local result = {}
	for word in gsplit(text, '/') do
		local converted = {}

		local extra2 = match(word, '^*')
		for syllable in gmatch(word, '++*') do
			local initial, final, erhua, tone, extra = match(syllable, '^(??)(+)(r?)(+)(*)$')
			local caps = false

			if find(initial .. final, '') then
				caps = true
				initial, final = lower(initial), lower(final)
			end

			warn(initial, final, tone)

			initial, final = fix(initial, final)
			if final == 'e' and erhua == 'r' then
				final, erhua = 'er', ''
			end

			if scheme == 'IPA' then
				initial = initialConv
				final = finalConv
				tone = gsub(tone, '.', function(char) return toneConv end)

				if erhua == 'r' then
					if find(final, '^y') then -- 撮口呼
						final = 'yɚ'
					elseif find(final, '^i') then -- 齊齒呼
						final = 'iɚ'
					elseif find(final, '^u') then -- 合口呼
						final = 'uɚ'
					elseif (final == 'o' or final == 'oŋ') and find(initial, '^') then
						final = 'ɚ'
					elseif final == 'o' or final == 'oŋ' then
						final = 'uɚ'
					else -- 開口呼
						final = 'ɚ'
					end
				end

				syllable = initial .. final .. tone

				table.insert(converted, syllable)
			elseif scheme == 'SWZ' then
				initial = initialConv_swz or initial
				final = finalConv_swz or final

				tone = gsub(tone, '(%d)%-(%d)', '%2')

				-- XXX: what happens with erhua? (disabled output for now)
				-- cf the given example 貓(mer)
				if erhua == 'r' then return false end

				if tone == '3' and (final == 'a' or final == 'ai') then
					final = 'a' .. final
				end

				syllable = initial .. final

				if caps then syllable = gsub(syllable, '^.', upper) end

				table.insert(converted, '@' .. syllable .. extra)
			else
				error('Convert to what representation?')
			end
		end

		if scheme == 'IPA' then
			local text = '/' .. table.concat(converted, ' ') .. '/'
			table.insert(result, text)
		elseif scheme == 'SWZ' then
			local text = table.concat(converted, '')
			text = gsub(text, '()@(u)', '%1w')
			text = gsub(text, '()@(i)', '%1j')
			text = gsub(text, '()@(y)', '%1j')
			text = gsub(text, '@un', 'wen')
			text = gsub(text, '@', '')
			table.insert(result, extra2 .. text)
		end
	end

	if scheme == 'IPA' then
		return table.concat(result, ', ')
	else
		return table.concat(result, ' / ')
	end
end

return export