Module:Sandbox/Santi2222/eu-pronunc

Definition from Wiktionary, the free dictionary
Jump to navigation Jump to search

local export = {}

function export.IPA(word,phonetic,do_debug)
	local debug = {}
	
	if type(word) == 'table' then
		do_debug = word.args[4]
		word = word.args[1]
	end
	local orig_word = word
	word = mw.ustring.lower(word or mw.title.getCurrentTitle().text)
	word = mw.ustring.gsub(word,"[^abdefghiíjklmnopqrstuúxzüñ.]","")
	
	table.insert(debug,word)
	
	local V = "[aeiíouüú]" -- vowel
	local C = "[^aeiíouüú.]" -- consonant
	
	--palatals
	word = mw.ustring.gsub(word,"ll","ʎ")

    --vocales hiato / diptongo
    word = mw.ustring.gsub(word,"í","i.")
    word = mw.ustring.gsub(word,"ú","u.")
	table.insert(debug,word)
	
	local match_count = 0
	word = mw.ustring.gsub(
		word,
		'(.?)h(.?)',
		function (before, after)
			match_count = match_count + 1
			-- mw.log(word, before, after)
			if match_count == 1 and ('aeiouü'):match(before) and ('aeiouü'):match(after) and before  ~= "" and after  ~= "" then
				return before .. '.'.. after
			end
		end)
	table.insert(debug,word)
	
	--alphabet-to-phoneme
	word = mw.ustring.gsub(word,"tt","c")
    word = mw.ustring.gsub(word,"tt","ɟ")
	word = mw.ustring.gsub(word,"tx","č") -- "t͡ʃ"
    word = mw.ustring.gsub(word,"ts","ć") -- "t͡s"
	word = mw.ustring.gsub(word,"tz","đ") -- "t͡s̺
	word = mw.ustring.gsub(word,"x","ʃ")
	word = mw.ustring.gsub(word,'[gjñr]',
		--['g']='ɡ':  U+0067 LATIN SMALL LETTER G → U+0261 LATIN SMALL LETTER SCRIPT G
		{['g']='ɡ', ['j']='x', ['r']='ɾ'})
	
	--trill in #r, lr, nr, rr
	local match_count = 0
	word = mw.ustring.gsub(
		word,
		'(.?)ɾ(.?)',
		function (before, after)
			match_count = match_count + 1
			-- mw.log(word, before, after)
			if match_count == 1 and before == '' or before == 'l' or before == 'n'
					or after ~= '' and ('bdfɡklʎmnɲpstxzčćđcɟ'):match(after) then
				return before .. 'r' .. after
			elseif before == 'ɾ' then
				return 'r' .. after
			elseif after == 'ɾ' then
				return before .. 'r'
			end
		end)
	
	word = mw.ustring.gsub(word,'n([bm])','m%1')
	
	table.insert(debug,word)
	
	--iɲ sound

	local match_count = 0
	word = mw.ustring.gsub(
		word,
		'in(.?)',
		function (after)
			match_count = match_count + 1
			-- mw.log(word, before, after)
			if match_count == 1 and ('aeiouü'):match(after) then
				return 'iɲ' .. after
			end
		end)
	local match_count = 0
	word = mw.ustring.gsub(
		word,
		'(.?)i(.?)',
		function (before, after)
			match_count = match_count + 1
			-- mw.log(word, before, after)
			if match_count == 1 and ('aeiouü'):match(before) and after == "ɲ" and before  ~= "" then
				return before .. after
			end
		end)
	
	table.insert(debug,word)
	
	word = mw.ustring.gsub(word,"ñ","ɲ")

	--iʎ sound
	local match_count = 0
	word = mw.ustring.gsub(
		word,
		'il(.?)',
		function (after)
			match_count = match_count + 1
			-- mw.log(word, before, after)
			if match_count == 1 and ('aeiouü'):match(after) then
				return 'iʎ' .. after
			end
		end)
	
	local match_count = 0
	word = mw.ustring.gsub(
		word,
		'(.?)il(.?)',
		function (before, after)
			match_count = match_count + 1
			-- mw.log(word, before, after)
			if match_count == 1 and ('aeiouü'):match(after) and ('aeiouü'):match(before) then
				return before .. 'ʎ' .. after
			elseif ('aeiouü'):match(after) then
				return before .. 'iʎ' .. after
			end
		end)
	
	table.insert(debug,word)
	
	--syllable division
	for _ = 1, 2 do
		word = mw.ustring.gsub(word,"(" .. V .. ")(" .. C .. ")(" .. V .. ")","%1.%2%3")
	end
	for _ = 1, 2 do
		word = mw.ustring.gsub(word,"(" .. V .. ")(" .. C .. ")(" .. C .. ")(" .. V .. ")","%1%2.%3%4")
	end
	for _ = 1, 2 do
		word = mw.ustring.gsub(word,"(" .. V .. ")(" .. C .. ")(" .. C .. ")(" .. C .. ")(" .. V .. ")","%1%2.%3%4%5")
	end
	word = mw.ustring.gsub(word,"([pbktdɡcɟ])%.([lɾ])",".%1%2")
	word = mw.ustring.gsub(word,"(" .. C .. ")%.s(" .. C .. ")","%1s.%2")
	word = mw.ustring.gsub(word,"([aeoáéíóú])([aeoáéíóú])","%1.%2")
	word = mw.ustring.gsub(word,"([ií])([ií])","%1.%2")
	word = mw.ustring.gsub(word,"([uú])([uú])","%1.%2")

	table.insert(debug,word)
	
	--diphthongs
	word = mw.ustring.gsub(word,'ih?([aeouáéóú])','j%1')
	word = mw.ustring.gsub(word,'uh?([aeioáéíó])','w%1')
	
	table.insert(debug,word)
	
	--accentuation
	local syllables = mw.text.split(word,"%.")

	table.insert(debug,word)
	
	--syllables are shown
	word = table.concat(syllables,'.')
	
	--allophones of b, d, g
	if phonetic then
		local match_count = 0
		word = mw.ustring.gsub(
			word,
			'(.?).b(.?)',
			function (before, after)
				match_count = match_count + 1
				-- mw.log(word, before, after)
				if match_count == 1 and before ~= "" and before ~= "." and before ~= "n" and before ~= "m" and before ~= "ɲ" then
					return before .. '.β̞' .. after
				end
			end)
		table.insert(debug,word)
		local match_count = 0
		word = mw.ustring.gsub(
			word,
			'(.?).d(.?)',
			function (before, after)
				match_count = match_count + 1
				-- mw.log(word, before, after)
				if match_count == 1 and before ~= "" and before ~= "n" and before ~= "m" and before ~= "ɲ" and before ~= "ʎ" and before ~= "l" then
					return before .. '.ð̞' .. after
				end
			end)
		table.insert(debug,word)
		local match_count = 0
		word = mw.ustring.gsub(
			word,
			'(.?).g(.?)',
			function (before, after)
				match_count = match_count + 1
				-- mw.log(word, before, after)
				if match_count == 1 and before ~= "" and before ~= "n" and before ~= "m" and before ~= "ɲ" then
					return before .. '.ɣ˕' .. after
				end
			end)
		table.insert(debug,word)
	end
	
	--allophones of n, t, l and notation of vowels
	if phonetic then
		word = mw.ustring.gsub(word,"n.k","ŋ.k")
		word = mw.ustring.gsub(word,"n.g","ŋ.g")
		word = mw.ustring.gsub(word,"n.x","ŋ.x")	
		word = mw.ustring.gsub(word,"n.f","ɱ.f")
		word = mw.ustring.gsub(word,"n.ʎ","ɲ.ʎ")
		word = mw.ustring.gsub(word,"n.c","ɲ.c")	
		word = mw.ustring.gsub(word,"n.ɟ","ɲ.ɟ")	
		word = mw.ustring.gsub(word,"n.t","n̪.t")	
		word = mw.ustring.gsub(word,"n.d","n̪.d")
		word = mw.ustring.gsub(word,"l.t","l̪.t")	
		word = mw.ustring.gsub(word,"l.d","l̪.d")
		word = mw.ustring.gsub(word,"l.č","lʲ.č")
		word = mw.ustring.gsub(word,"n.č","nʲ.č")
		word = mw.ustring.gsub(word,"t","t̪")	
		word = mw.ustring.gsub(word, 'a', 'ä')
		word = mw.ustring.gsub(word, 'e', 'ɛ')
		word = mw.ustring.gsub(word, 'i', 'ɪ')
		word = mw.ustring.gsub(word, 'o', 'ɔ')
		word = mw.ustring.gsub(word, 'u', 'ʊ')
	end
	
	--notation
	word = mw.ustring.gsub(word,'h','')
	word = mw.ustring.gsub(word,'č','t͡ʃ')
	word = mw.ustring.gsub(word,'ć','t͡s')
	word = mw.ustring.gsub(word,'đ','t͡z')
	word = mw.ustring.gsub(word,'ü','y')

	--correction of allophonic vowels (if phonetic)
	local match_count = 0
	word = mw.ustring.gsub(
		word,
		'(.?)ɔ.ä(.?)',
		function (before, after)
			match_count = match_count + 1
			-- mw.log(word, before, after)
			if match_count == 1 and after  == "" then
				return before .. 'o.ä' .. after
			end
		end)
	table.insert(debug,word)
	
	local match_count = 0
	word = mw.ustring.gsub(
		word,
		'(.?)ɛ.ä(.?)',
		function (before, after)
			match_count = match_count + 1
			-- mw.log(word, before, after)
			if match_count == 1 and after  == "" then
				return before .. 'e.ä' .. after
			end
		end)
	
	table.insert(debug,word)
	
	--dipthongs
	word = mw.ustring.gsub(word,"äʊ","äʊ̯")
	word = mw.ustring.gsub(word,"äɪ","äɪ̯")
	word = mw.ustring.gsub(word,"ɛʊ","ɛʊ̯")
	word = mw.ustring.gsub(word,"ɛɪ","ɛɪ̯")
	word = mw.ustring.gsub(word,"ɔʊ","ɔʊ̯")
	word = mw.ustring.gsub(word,"ɔɪ","ɔɪ̯")

	local match_count = 0
	word = mw.ustring.gsub(
		word,
		'(.?)j(.?)',
		function (before, after)
			match_count = match_count + 1
			-- mw.log(word, before, after)
			if match_count == 1 and ('äaɛeɪiɔoʊuy'):match(before) and ('äaɛeɪiɔoʊuy'):match(after) and before  ~= "" and after  ~= "" then
				return before .. '.j'.. after
			end
		end)
	table.insert(debug,word)
	
	local match_count = 0
	word = mw.ustring.gsub(
		word,
		'(.?)w(.?)',
		function (before, after)
			match_count = match_count + 1
			-- mw.log(word, before, after)
			if match_count == 1 and ('äaɛeɪiɔoʊuy'):match(before) and ('äaɛeɪiɔoʊuy'):match(after) and before  ~= "" and after  ~= "" then
				return before .. '.w'.. after
			end
		end)
	table.insert(debug,word)
	
	--z and s
	if phonetic then
		word = mw.ustring.gsub(word,'s','s̺')
	end
	word = mw.ustring.gsub(word,'z','s̪')

	--
	if do_debug == 'yes' then
		return word .. table.concat(debug,"")
	else
		return word
	end
end

function export.phonetic(frame)
	return export.IPA(frame,true)
end

return export