Pergi ke kandungan

Modul:dng-pron

Daripada Wikikamus
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͡ʂ", 
	["ц"] = "t͡sʰ", ["ч"] = "t͡ʂʰ", 
	["м"] = "m", ["н"] = "n", ["ң"] = "ŋ",
	["ф"] = "f", ["с"] = "s", ["ш"] = "ʂ", ["щ"] = "ɕ", ["х"] = "x",
	["в"] = "v", ["л"] = "l", ["ж"] = "ʐ", ["й"] = "ʝ",
	[""] = "",
}

local finalConv = {
	["ы"] = "ɨː", ["эр"] = "əɻ",

	["а"] = "aː", ["о"] = "ɔː", ["ә"] = "ɤː",
	["э"] = "ɛː", ["ый"] = "ɨi", ["у"] = "ʊ",
	["ан"] = "æ̃", ["ын"] = "ə̃", ["он"] = "ɔ̃", ["ун"] = "uŋ",

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

	["ў"] = "uː", ["уа"] = "ua", ["уэ"] = "uɛ", ["уә"] = "uɤ",
	["уэй"] = "uɛi", ["уй"] = "uei",
	["уан"] = "uæ̃", ["уон"] = "uɔn",

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

local finalConv_fix = {
	["ia"] = "jaː", ["iə"] = "jəː", ["iɔː"] = "jɔː", ["iou"] = "jou", ["iæ̃"] = "jæ̃", ["iɔ̃"] = "jɔ̃", 
}

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
--]]
				if initial == '' then final = finalConv_fix[final] or final end
				syllable = initial .. final .. tone
				syllable = gsub(syllable, 'ʂ(ʰ?[iĩy])', 'ɕ%1')

				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