Jump to content

Module:Es-pronunc

Frae Wikipedia, the free beuk o knawledge
local export = {}

function export.show(word,LatinAmerica,phonetic,do_debug)
	local debug = {}
	
	if type(word) == 'table' then
		do_debug = word.args[4]
		word = word.args[1]
	end
	word = (word or mw.title.getCurrentTitle().text):lower()
	word = mw.ustring.gsub(word,"[^abcdefghijklmnopqrstuvwxyzáéíóúüñ.]","")
	
	table.insert(debug,word)
	
	--determining whether "y" is a consonant or a vowel + diphthongs, "-mente" suffix
	word = mw.ustring.gsub(word,"y([^aeiouáéíóú])","i%1")
	word = mw.ustring.gsub(word,"y([aeiouáéíóú])","ɟ%1") -- not the real sound
	word = mw.ustring.gsub(word,"hi([aeiouáéíóú])","ɟ%1")
	word = mw.ustring.gsub(word,"y$","ï")
    word = mw.ustring.gsub(word,"mente$","ménte")
	
	--x
	word = mw.ustring.gsub(word,"x","ks")
	
	--"c" & "g" before "i" and "e" and all that stuff
	word = mw.ustring.gsub(word,"c([ieíé])",(LatinAmerica and 's' or 'θ') .. "%1")
	word = mw.ustring.gsub(word,"gü([ieíé])","ɡw%1")
	word = mw.ustring.gsub(word,"ü","")
	word = mw.ustring.gsub(word,"gu([ieíé])","ɡ%1")
	word = mw.ustring.gsub(word,"g([ieíé])","x%1")

	table.insert(debug,word)
	
	--alphabet-to-phoneme
	word = mw.ustring.gsub(word,"qu","c")
	word = mw.ustring.gsub(word,"v","b")
	word = mw.ustring.gsub(word,"ch","ʃ") --not the real sound
	word = mw.ustring.gsub(word,'[cgjñry]',{['c']='k', ['g']='ɡ', ['j']='x', ['ñ']='ɲ', ['r']='ɾ'})  --['g']='ɡ':  U+0067 LATIN SMALL LETTER G → U+0261 LATIN SMALL LETTER SCRIPT G
	word = mw.ustring.gsub(word,'^ɾ','r')
	word = mw.ustring.gsub(word,'ɾɾ','r')
	word = mw.ustring.gsub(word,'lɾ','lr')
	word = mw.ustring.gsub(word,'nɾ','nr')
	word = mw.ustring.gsub(word,'ɾ([bdfɡklʎmnɲpstxzʃɟ])','r%1')
	word = mw.ustring.gsub(word,'n([bm])','m%1')
	word = mw.ustring.gsub(word,'ll',(LatinAmerica and 'ɟ' or 'ʎ'))
	word = mw.ustring.gsub(word,'z',(LatinAmerica and 'z' or 'θ')) -- not the real LatAm sound
	
	table.insert(debug,word)
	
	--syllable division
	word = mw.ustring.gsub(word,"([aeiouáéíóú])([^aeiouáéíóú.])([aeiouáéíóú])","%1.%2%3")
	word = mw.ustring.gsub(word,"([aeiouáéíóú])([^aeiouáéíóú.])([aeiouáéíóú])","%1.%2%3")
	word = mw.ustring.gsub(word,"([aeiouáéíóú])([^aeiouáéíóú.])([^aeiouáéíóú.])([aeiouáéíóú])","%1%2.%3%4")
	word = mw.ustring.gsub(word,"([aeiouáéíóú])([^aeiouáéíóú.])([^aeiouáéíóú.])([aeiouáéíóú])","%1%2.%3%4")
	word = mw.ustring.gsub(word,"([aeiouáéíóú])([^aeiouáéíóú.])([^aeiouáéíóú.])([^aeiouáéíóú.])([aeiouáéíóú])","%1%2.%3%4%5")
	word = mw.ustring.gsub(word,"([aeiouáéíóú])([^aeiouáéíóú.])([^aeiouáéíóú.])([^aeiouáéíóú.])([aeiouáéíóú])","%1%2.%3%4%5")
	word = mw.ustring.gsub(word,"([pbktdɡ])%.([lɾ])",".%1%2")
	word = mw.ustring.gsub(word,"([^aeiouáéíóú.])%.s([^aeiouáéíóú.])","%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,"%.")
	if mw.ustring.find(word,"[áéíóú]") then
		for i=1,#syllables do
			if mw.ustring.find(syllables[i],"[áéíóú]") then syllables[i] = "ˈ"..syllables[i] end
		end
	else
		if mw.ustring.find(word,"[^aeiouns]$") then
			syllables[#syllables] = "ˈ"..syllables[#syllables]
		else
			if #syllables > 1 then syllables[#syllables-1] = "ˈ"..syllables[#syllables-1] end
		end
	end

	table.insert(debug,word)
	
	--syllables nasalized if ending with "n", voiceless consonants in syllable-final position to voiced
	for i=1,#syllables do
		syllables[i] = mw.ustring.gsub(syllables[i],'[áéíóú]',{['á']='a', ['é']='e', ['í']='i', ['ó']='o', ['ú']='u'})
		if phonetic and mw.ustring.find(syllables[i],'[mnɲ][^aeiou]?$') then
			syllables[i] = mw.ustring.gsub(syllables[i],'([aeiou])',{['a']='ã',['e']='ẽ',['i']='ĩ',['o']='õ',['u']='ũ'})
		end
		syllables[i] = mw.ustring.gsub(syllables[i],'[ptk]$',{['p']='b',['t']='d',['k']='ɡ'})
	end
	word = table.concat(syllables)
	
	--real sound of LatAm Z
	word = mw.ustring.gsub(word,'z','s')
	--secondary stress
	word = mw.ustring.gsub(word,'ˈ(.+)ˈ','ˌ%1ˈ')
	word = mw.ustring.gsub(word,'ˈ(.+)ˌ','ˌ%1ˌ')
	word = mw.ustring.gsub(word,'ˌ(.+)ˈ(.+)ˈ','ˌ%1ˌ%2ˈ')

	--phonetic transcription
	if phonetic then
		--θ, s, f before voiced consonants
		word = mw.ustring.gsub(word,'θ([ˈˌ]?[mnɲbdɟɡlʎɾrh])','θ̬%1')
		word = mw.ustring.gsub(word,'s([ˈˌ]?[mnɲbdɟɡlʎɾrh])','z%1')
		word = mw.ustring.gsub(word,'f([ˈˌ]?[mnɲbdɟɡlʎrh])','v%1')
		--lots of allophones going on
		word = mw.ustring.gsub(word,'[bdɟɡ]',{['b']='β',['d']='ð',['ɟ']='ʝ',['ɡ']='ɣ'})
		word = mw.ustring.gsub(word,'^[ˈˌ]?[βðɣʝ]',{['β']='b',['ð']='d',['ʝ']='ɟ',['ɣ']='ɡ',['ˈβ']='ˈb',['ˈð']='ˈd',['ˈʝ']='ˈɟ',['ˈɣ']='ˈɡ',['ˌβ']='ˌb',['ˌð']='ˌd',['ˌʝ']='ˌɟ',['ˌɣ']='ˌɡ'})
		word = mw.ustring.gsub(word,'([mnɲ][ˈˌ]?)β','%1b')
		word = mw.ustring.gsub(word,'([lʎmnɲ][ˈˌ]?)ð','%1d')
		word = mw.ustring.gsub(word,'([mnɲ][ˈˌ]?)ɣ','%1ɡ')
		word = mw.ustring.gsub(word,'([lʎmnɲ][ˈˌ]?)ʝ','%1ɟ')
		word = mw.ustring.gsub(word,'[td]',{['t']='t̪',['d']='d̪'})
		--nasal assimilation before consonants
		word = mw.ustring.gsub(word,'n([ˈˌ]?[f])','ɱ%1')
		word = mw.ustring.gsub(word,'n([ˈˌ]?[td])','n̪%1')
		word = mw.ustring.gsub(word,'n([ˈˌ]?[θ])','n̟%1')
		word = mw.ustring.gsub(word,'n([ˈˌ]?ʃ)','nʲ%1')
		word = mw.ustring.gsub(word,'n([ˈˌ]?[ɟʎ])','ɲ%1')
		word = mw.ustring.gsub(word,'n([ˈˌ]?[kxɡ])','ŋ%1')
		--lateral assimilation before consonants
		word = mw.ustring.gsub(word,'l([ˈˌ]?[td])','l̪%1')
		word = mw.ustring.gsub(word,'l([ˈˌ]?[θ])','l̟%1')
		--semivowels
		word = mw.ustring.gsub(word,'([aeouãẽõũ][iïĩ])','%1̯')
		word = mw.ustring.gsub(word,'([aeioãẽĩõ][uũ])','%1̯')
	end
	
	table.insert(debug,word)
	
	word = mw.ustring.gsub(word,'h','') --silent "h"
	word = mw.ustring.gsub(word,'ʃ','t͡ʃ') --fake "ch" to real "ch"
	word = mw.ustring.gsub(word,'ɟ','ɟ͡ʝ') --fake "y" to real "y"
	word = mw.ustring.gsub(word,'ï','i') --fake "y$" to real "y$"
	
	if do_debug == 'yes' then
		return word .. table.concat(debug,"")
	else
		return word
	end
end

function export.LatinAmerica(frame)
	return export.show(frame,true)
end

function export.phonetic(frame)
	return export.show(frame,false,true)
end

function export.phoneticLatinAmerica(frame)
	return export.show(frame,true,true)
end

return export