Немає перевірених версій цієї сторінки; ймовірно, її ще не перевіряли на відповідність правилам проекту.

Документацію для цього модуля можна створити у Модуль:be-pron/документація

local export = {}

local m_IPA = require("Модуль:IPA")

local uk = require("Модуль:languages-data").getByCode("uk")

local gsub = mw.ustring.gsub
local U = mw.ustring.char
local acute = U(0x301)
local grave = U(0x300)

function export.ipa(word, accent, output)
	if type(word) == "table" then
		word, accent, output =
			word.args[1] or word:getParent().args[1],
			word.args.accent or word:getParent().args.accent,
			word.args.output or word:getParent().args.output
	end
	
	if not word or (word == "") then
		word = mw.title.getCurrentTitle().text
	end
	
	--	Возвращает ошибку, если слово содержит некириллические символы.
--	require("Модуль:script utilities").checkScript(word, "Cyrl")
	
	word = mw.ustring.lower(word)
	
	local needsAccent = false
	
	if accent ~= "off" and not mw.ustring.find(word, "[" .. acute .. grave .. "]") then
		if require("Модуль:string").count(word, "[аеєиіїоуюя]") > 1 then
			needsAccent = true
		end
	end

	
	local phonetic_chars_map = {
	
		-- отдельные символы, которые переводятся в звуки МФА; они обрабатываются последними
		[3] = {
			["а"] = "a",	["б"] = "b",	["в"] = "v",	["г"] = "ɣ",	["ґ"] = "ɡ", 
			["д"] = "d",	["е"] = "ɛ", ["ё"] = "ʲo",	["ж"] = "ʐ",	["з"] = "z", 
			["і"] = "ʲi",	["й"] = "j",	["к"] = "k", 
			["л"] = "l",	["м"] = "m",	["н"] = "n",	["о"] = "o",	["п"] = "p", 
			["р"] = "r",	["с"] = "s",	["т"] = "t",	["у"] = "u", ["ў"] = "w",	["ф"] = "f", 
			["х"] = "x",	["ц"] = "t͡s",	["ч"] = "t͡ʂ",	["ш"] = "ʂ",["ы"] = "ɨ ", ["э"] = "e", 
			["ь"] = "ʲ",	["ю"] = "ʲu",	["я"] = "ʲa",	["’"] = "j",
			-- ударные гласные
			[acute] =  "ˈ", [grave] =  "ˈ",
		},
	
		-- последовательность двух символов, которая переводится в звуки МФА
		[2] = {
			["дж"] = "d͡ʐ",	["дз"] = "d͡z",
		-- Dental plosives assimilate to following hissing/hushing consonants, which is not noted in the spelling.
			["дс"] = "d͡zs",   ["тц"] = "t͡sː", 
		},
	
		-- последовательность трёх символов, которая переводится в звуки МФА
		[1] = {
			["дзь"] = "d͡zʲ", 
		-- Dental plosives assimilate to following hissing/hushing consonants, which is not noted in the spelling.
			["тьс"] = "t͡sʲː"
		},
	}
	
	local phonetic = word
	local orthographic_replacements = {
		-- first apply consonant cluster simplifications that always occur orthographically
		["нтськ"	] = "ньськ",
		["стськ"	] = "ськ",
		["нтст"		] = "нст",
		["стч"		] = "шч",
		["стд"		] = "зд",
		["стс"		] = "сː",
		["стськ"	] = "ськ",
		["^зш"		] = "шː",
		["зш"		] = "жш",
		["^зч"		] = "шч",
		["зч"		] = "жч",
	
		-- then long consonants that are orthographically geminated.
		["([бвгґд])%1"			] = "%1ː",
		["([^д]+)жж"			] = "%1жː", -- джж последовательность кодирует дифонический дж
		["([^д]+)зз"			] = "%1зː", -- дзз последовательность кодирует дифонический дз
		["([йклмнпрстфхцчшщ])%1"] = "%1ː",
		["дждж"					] = "джː",
		["дздз"					] = "дзː",
	}
	
	for regex, replacement in pairs(orthographic_replacements) do
		phonetic = gsub(phonetic, regex, replacement)
	end
	
	-- переназначение апострофа на '!', чтобы не противоречило ударной отметке МФА
	phonetic =  gsub(phonetic, "'", "!")
	
	-- replace multiple letter sequences
	for _, replacements in ipairs(phonetic_chars_map) do
		for key, replacement in pairs(replacements) do
			phonetic = gsub(phonetic, key, replacement)
		end
	end

	-- move stress mark, added by phonetic_chars_map, before vowel
	phonetic = gsub(phonetic, "([ɑɛiɪuɔ])ˈ", "ˈ%1")
	
	-- добавление ударения, если слово с одним слогом и без "|ударение=откл"
	local _, numberOfVowels  = gsub(phonetic, "[ɑɛiɪuɔ]", "")
	if (numberOfVowels == 1) and  (accent ~= "откл") then
		phonetic = gsub(phonetic, "([ɑɛiɪuɔ])", "ˈ%1")
	end
	
	-- palatalizable consonants before /i/ or /j/ become palatalized
	local palatalizable = "[tdsznlrbpʋfɡmkɦxʃʒ]"
	phonetic = gsub(phonetic, "(" .. palatalizable .. ")([ː]?)([ˈ]?)i", "%1ʲ%2%3i")
	phonetic = gsub(phonetic, "(" .. palatalizable .. ")([ː]?)j", "%1ʲ%2")

	-- eliminate garbage sequences of [ʲːj] resulting from -тьс- cluster followed by [j]
	phonetic = gsub(phonetic, "ʲːj", "ʲː")

	-- упрощение согласных: ст + ц' → [с'ц']. Мы создаём это из-за палатализации.
	-- Так как т +ц → [ц:] присутствует правило о долготе. Согласно «Орфоэпическому словарю» p. 13,
	-- обе формы правильные, без долготы в обычной (разговорной) речи и с долготой
	-- в медленной речи, поэтому мы заключаем в скобки долготу как опциональную.
	phonetic =  gsub(phonetic, "st͡sʲ([ː]?)", "sʲt͡sʲ(%1)")
	
	-- ассимиляция: глухой + звонкий = звонкий + звонкий
	-- должна ли /ʋ/ включаться как звонкая? «Орфоепічний словник» не озвучивает начальный кластер шв (p. 116)
	local voiced_obstruent = "[bdzʒɡɦ]"
	local voicing = {
		["p"] = "b",
		["f"] = "v",
		["t"] = "d",
		["tʲ"] = "dʲ",
		["s"] = "z",
		["sʲ"] = "zʲ",
		["ʂ"] = "ʐ",
		["k"] = "ɡ",
		["x"] = "ɣ",
		["t͡s"] = "d͡z",
		["t͡sʲ"] = "d͡zʲ",
		["t͡ʂ"] = "d͡ʐ",
	
	}
	for voiceless, voiced in pairs(voicing) do
		phonetic = gsub(phonetic, voiceless .. "(" .. voiced_obstruent .. "+)", voiced .. "%1")
	end

	-- In the sequence of two consonants, of which the second is soft, the first is pronounced soft too
	-- unless the first consonant is a labial, namely б, п, в, ф, м.
	phonetic = gsub(phonetic, "([tdsznl])(.)ʲ", "%1ʲ%2ʲ")
	phonetic = gsub(phonetic, "([tdsznl])t͡sʲ", "%1ʲt͡sʲ")
	phonetic = gsub(phonetic, "([tdsznl])d͡zʲ", "%1ʲd͡zʲ")
	phonetic = gsub(phonetic, "t͡s(.)ʲ", "t͡sʲ%1ʲ")
	phonetic = gsub(phonetic, "d͡z(.)ʲ", "d͡zʲ%1ʲ")
	phonetic = gsub(phonetic, "d͡zt͡sʲ", "d͡zʲt͡sʲ")
	phonetic = gsub(phonetic, "t͡sd͡zʲ", "t͡sʲd͡zʲ")

	-- Hushing consonants ж, ч, ш assimilate to the following hissing consonants, giving a long hissing consonant:
	-- [ʒ] + [t͡sʲ] → [zʲt͡sʲ], [t͡ʃ] + [t͡sʲ] → [t͡sʲː], [ʃ] + [t͡sʲ] → [sʲt͡sʲ], [ʃ] + [sʲ] → [sʲː]
	phonetic = gsub(phonetic, "ʒt͡sʲ", "zʲt͡sʲ")
	phonetic = gsub(phonetic, "t͡ʃt͡sʲ", "t͡sʲː")
	phonetic = gsub(phonetic, "ʃt͡sʲ", "sʲt͡sʲ")
	phonetic = gsub(phonetic, "ʃsʲ", "sʲː")

	-- Hissing consonants before hushing consonants within a word assimilate - on зш and зч word-initially and 
	-- word-medially see above.
	-- [s] + [ʃ] → [ʃː],  [z] + [ʃ] → [ʒʃ], [z] + [t͡s] → [ʒt͡s]
	-- [z] + [d͡ʒ] → [ʒd͡ʒ]
	phonetic = gsub(phonetic, "zʒ", "ʒː")
	phonetic = gsub(phonetic, "sʃ", "ʃː")
	phonetic = gsub(phonetic, "zt͡s", "ʒt͡s")
	phonetic = gsub(phonetic, "zd͡ʒ", "ʒd͡ʒ")
	
	-- cleanup: excessive palatalization: CʲCʲCʲ → CCʲCʲ
	phonetic = gsub(phonetic, "([^ɑɛiɪuɔ]+)ʲ([^ɑɛiɪuɔ]+)ʲ([^ɑɛiɪuɔ]+)ʲ", "%1%2ʲ%3ʲ")

	-- unstressed /ɑ/ has an allophone [ɐ]
	phonetic = gsub(phonetic, "([^ˈ])ɑ", "%1ɐ")
	phonetic = gsub(phonetic, "^ɑ", "ɐ")
	-- unstressed /u/ has an allophone [ʊ]
	phonetic = gsub(phonetic, "([^ˈ])u", "%1ʊ")
	phonetic = gsub(phonetic, "^u", "ʊ")
	-- unstressed /ɔ/ has by assimilation an allophone [o] before a stressed syllable with /u/ or /i/
	phonetic = gsub(phonetic, "ɔ([bdzʒɡɦmnlrpftskxʲʃ͡]+)ˈ([uiʊ]+)", "o%1ˈ%2")
	-- one allophone [e] covers unstressed /ɛ/ and /ɪ/
	phonetic = gsub(phonetic, "([^ˈ])ɛ", "%1e")
	phonetic = gsub(phonetic, "^ɛ", "e")
	phonetic = gsub(phonetic, "([^ˈ])ɪ", "%1e")
	phonetic = gsub(phonetic, "^ɪ", "e")   

	-- /ʋ/ has an allophone [u̯] in a syllable coda
	local vowel = "[ɑɛiɪuɔɐoʊe]"
	phonetic = gsub(phonetic, "(" .. vowel .. "+)ʋ", "%1u̯")
	-- /ʋ/ has an allophone [w] before /ɔ, u/and voiced consonants (not after a vowel)
	phonetic = gsub(phonetic, "ʋ([ˈ]?)([ɔuoʊbdzʒɡɦmnlr]+)", "w%1%2")
	-- /ʋ/ has an allophone [ʍ] before before voiceless consonants (not after a vowel)
	phonetic = gsub(phonetic, "ʋ([pftskxʃ]+)", "ʍ%1")

	-- in a syllable-final position (i.e. the first position of a syllable coda) /j/ has an allophone [i̯]:
	local consonant = "[bdzʒɡɦmnlrpftskxʃʋ]"
	phonetic = gsub(phonetic, "(" .. vowel .. "+)j([ˈ]?)(" .. gsub(consonant, "ʋ", "") .. "+)", "%1i̯%2%3")
	phonetic = gsub(phonetic, "(" .. vowel .. "+)j$", "%1i̯")
	-- also at the beginning of a word before a consonant
	phonetic = gsub(phonetic, "^j(" .. gsub(consonant, "ʋ", "") .. "+)", "i̯%1")
 
	-- remove old orthographic apostrophe
	phonetic =  gsub(phonetic, "!", "")
	-- stress mark in correct place
	phonetic = gsub(phonetic, "([bdzʒɡɦjʲmnlrpftskxʃʋwʍː͡]+)ˈ", "ˈ%1")
	phonetic = gsub(phonetic, "([ui]̯)ˈ([ʲ]?" .. vowel .. ")", "ˈ%1%2")
	phonetic = gsub(phonetic, "ˈ(l[ʲ]?[ː]?)(" .. gsub(consonant, "l", "") .. ")", "%1ˈ%2")
	phonetic = gsub(phonetic, "ˈ(r[ʲ]?[ː]?)(" .. gsub(consonant, "r", "") .. ")", "%1ˈ%2")
	phonetic = gsub(phonetic, "ˈ(m[ʲ]?[ː]?)([bpfɦszʃʋʒ])", "%1ˈ%2")
	phonetic = gsub(phonetic, "ˈ(n[ʲ]?[ː]?)([dtfkɡɦlxszʃʋʒ])", "%1ˈ%2")
	phonetic = gsub(phonetic, "ʲ?ːʲ", "ʲː")
	
	if output == "template" then
		return m_IPA.format_IPA_full(uk, { { pron = "[" .. phonetic .. "]" } } ) ..
			( needsAccent and
				'<span class="error previewonly"><br>Слово ' .. word .. ' содержит несколько гласных, но не имеет знака ударения, поэтому некоторые из гласных могут быть транскрибированы неправильно. Пожалуйста, добавьте знаки ударения, чтобы отметить ударные слоги.</span>' ..
				require("Модуль:utilities").format_categories( { "Украинские термины с неполным произношением" }, lang)
			or "" )
	else
		return (phonetic)
	end
end

return export