မာတိကာသို့ ခုန်သွားရန်

မဝ်ဂျူ:dng-pron

နူ ဝိက်ရှေန်နရဳ

Documentation for this module may be created at မဝ်ဂျူ:dng-pron/doc

local export = {}
local find = mw.ustring.find
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local gmatch = mw.ustring.gmatch
local gsplit = mw.text.gsplit
local lower = mw.ustring.lower
local upper = mw.ustring.upper

local initialConv = {
	["б"] = "p", ["д"] = "t", ["г"] = "k",
	["п"] = "pʰ", ["т"] = "tʰ", ["к"] = "kʰ",
	["з"] = "t͡s", ["җ"] = "ʈ͡ʂ", 
	["ц"] = "t͡sʰ", ["ч"] = "ʈ͡ʂʰ", 
	["м"] = "m", ["н"] = "n", ["ң"] = "ŋ",
	["ф"] = "f", ["с"] = "s", ["ш"] = "ʂ", ["щ"] = "ɕ", ["х"] = "x",
	["в"] = "v", ["л"] = "l", ["ж"] = "ʐ", ["й"] = "", ["р"] = "l",
	[""] = "",
}

local finalConv = {
	["ы"] = "z̩", ["эр"] = "ɛɻ",

	["а"] = "a", ["о"] = "ɔ", ["ә"] = "ə",
	["э"] = "ɛ", ["ый"] = "ei", ["у"] = "ou",
	["ан"] = "æ̃", ["ын"] = "əŋ", ["он"] = "ɑŋ", ["ун"] = "uŋ",

	["и"] = "i", ["я"] = "ia", ["е"] = "iə",
	["ё"] = "iɔː", ["ю"] = "iou",
	["ян"] = "iæ̃", ["ин"] = "iŋ", ["ён"] = "iɑŋ",

	["ў"] = "u", ["уа"] = "ua", ["уэ"] = "uɛ", ["уә"] = "uə",
	["уэй"] = "uɛi", ["уй"] = "uei",
	["уан"] = "uæ̃", ["уон"] = "uɑŋ",

	["ү"] = "y", ["үә"] = "yə",
	["үан"] = "yæ̃", ["үн"] = "yŋ",
}

local toneConv = {
	["1"] = "²⁴", ["2"] = "⁵¹", ["3"] = "⁴⁴", ["0"] = "⁰",
}

local function fix(initial, final)

	return initial, final
end

local function warn(initial, final, tone)	
	if not initialConv[initial] then
		error("Invalid initial: " .. initial)
	end

	if not finalConv[final] then
		error("Invalid final: " .. final)
	end

	if tone == "4" then
		error("Tone 4 currently not supported")
	end
end

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

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

		local extra2 = match(word, '^[^бдзҗгптцчкмнңфсшщхвлжйБДЗҖГПТЦЧКМНҢФСШЩХВЛЖЙыаәэоуияеёюўүЫАӘЭОУИЯЕЁЮЎҮйнр]*')
		for syllable in gmatch(word, '[бдзҗгптцчкмнңфсшщхвлжйБДЗҖГПТЦЧКМНҢФСШЩХВЛЖЙыаәэоуияеёюўүЫАӘЭОУИЯЕЁЮЎҮйнр]+[%d][^бдзҗгптцчкмнңфсшщхвлжйБДЗҖГПТЦЧКМНҢФСШЩХВЛЖЙыаәэоуияеёюўүЫАӘЭОУИЯЕЁЮЎҮйнр]*') do
			local initial, final, erhua, tone, extra = match(syllable, '^([бдзҗгптцчкмнңфсшщхвлжйрБДЗҖГПТЦЧКМНҢФСШЩХВЛЖЙР]?)([ыаәэоуияеёюўүЫАӘЭОУИЯЕЁЮЎҮйн]+)(р?)([%d])([^бдзҗгптцчкмнңфсшщхвлжйБДЗҖГПТЦЧКМНҢФСШЩХВЛЖЙыаәэоуияеёюўүЫАӘЭОУИЯЕЁЮЎҮйнр]*)$')
			local caps = false
			initial = initial or ''
			
			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 == 'э' and erhua == 'р' then
				final, erhua = 'эр', ''
			end

			if scheme == 'IPA' then
				initial = initialConv[initial]
				final = finalConv[final]
				tone = toneConv[tone]
--[[
				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, '^[pmfv]') then
						final = 'ɚ'
					elseif final == 'o' or final == 'oŋ' then
						final = 'uɚ'
					else -- 開口呼
						final = 'ɚ'
					end
				end
--]]
				syllable = initial .. final .. tone
				syllable = gsub(syllable, 'ʈ͡ʂ(ʰ?[iĩy])', 't͡ɕ%1')
				syllable = gsub(syllable, 'ʂ(ʰ?[iĩy])', 'ɕ%1')
				syllable = gsub(syllable, '(ʂʰ?)z̩', '%1ʐ̩')
				syllable = gsub(syllable, 'ʐz̩', 'ʐ̩')

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

		if scheme == 'IPA' then
			local text = '/' .. table.concat(converted, ' ') .. '/'
			table.insert(result, text)
		end
	end

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

function export.process(text)
	local readings = {}
	for reading in gsplit(text, '/') do
		local tones = {}
		for tone in gmatch(reading, '%d+') do
			tone = gsub(tone, "([123])", {[1] = "I", [2] = "II", [3] = "III"})
			table.insert(tones, tone)
		end
		local cyr = gsub(reading, '%d', '')
		local lat = require('Module:dng-translit').tr(cyr, nil, 'Cyrl')
		table.insert(readings, string.format("[[%s#Dungan|%s]] (%s, %s)", cyr, cyr, lat, table.concat(tones, '-')))
	end
	
	return table.concat(readings, ' / ')
end

return export