Module:cbk-pronunc

-- Based on module:tl-pron by TagaSanPedroAko, in turn based on module:es-pronunc by Benwing2. Some code based on module:ilo-pron -- Adaptation by TagaSanPedroAko.

local export = {}

local m_IPA = require("Module:IPA")

local lang = require("Module:languages").getByCode("cbk")

local u = mw.ustring.char local rfind = mw.ustring.find local rsubn = mw.ustring.gsub local rsplit = mw.text.split local ulower = mw.ustring.lower

local AC = u(0x0301) -- acute = ́ local GR = u(0x0300) -- grave = ̀ local CFLEX = u(0x0302) -- circumflex = ̂ local TILDE = u(0x0303) -- tilde = ̃ local DIA = u(0x0308) -- diaeresis = ̈ local MACRON = u(0x0304) -- macron

local vowel = "aeiouàèìòù" -- vowel local V = "[" .. vowel .. "]" local W = "[jw]" -- glide local accent = AC .. GR .. CFLEX .. MACRON local accent_c = "[" .. accent .. "]" local stress_c = "[" .. AC .. GR .. "]" local ipa_stress = "ˈˌ" local ipa_stress_c = "[" .. ipa_stress .. "]" local separator = accent .. ipa_stress .. "# ." local separator_c = "[" .. separator .. "]" local C = "[^" .. vowel .. separator .. "]" -- consonant local T = "[^" .. vowel .. "lrɾjw" .. separator .. "]" -- obstruent or nasal

local unstressed_words = require("Module:table").listToSet({ --feel free to add more unstressed words	"a", "el", "la", "los", "de", "del", "na", -- articles and prepositions	"y", "o", "si", "que", "ni", -- conjunctions   "no", -- negation word })

-- version of rsubn that discards all but the first return value local function rsub(term, foo, bar) local retval = rsubn(term, foo, bar) return retval end

-- version of rsubn that returns a 2nd argument boolean indicating whether -- a substitution was made. local function rsubb(term, foo, bar) local retval, nsubs = rsubn(term, foo, bar) return retval, nsubs > 0 end

-- apply rsub repeatedly until no change local function rsub_repeatedly(term, foo, bar) while true do		local new_term = rsub(term, foo, bar) if new_term == term then return term end term = new_term end end

-- ĵ, ɟ and ć are used internally to represent [d͡ʒ], [ʝ~ɟ͡ʝ] and [t͡ʃ]

function export.IPA(text, phonetic) local debug = {}

text = ulower(text or mw.title.getCurrentTitle.text) -- decompose everything but ë, ñ and ü

text = mw.ustring.toNFD(text) text = rsub(text, "." .. "[" .. TILDE .. DIA .. GR .."]", {		["a" .. GR] = "à",		["e" .. GR] = "è",		["i" .. GR] = "ì",		["o" .. GR] = "ò",		["u" .. GR] = "ù",		["n" .. TILDE] = "ñ",		["u" .. DIA] = "ü",	}) -- convert commas and en/en dashes to IPA foot boundaries text = rsub(text, "%s*[,–—]%s*", " | ") -- question mark or exclamation point in the middle of a sentence -> IPA foot boundary text = rsub(text, "([^%s])%s*[!?]%s*([^%s])", "%1 | %2")

-- canonicalize multiple spaces and remove leading and trailing spaces local function canon_spaces(text) text = rsub(text, "%s+", " ") text = rsub(text, "^ ", "") text = rsub(text, " $", "") return text end

text = canon_spaces(text)

-- Make prefixes unstressed unless they have an explicit stress marker; also make certain -- monosyllabic words (e.g. la, del, si, que, etc.) without stress marks be -- unstressed. local words = rsplit(text, " ") for i, word in ipairs(words) do		if rfind(word, "%-$") and not rfind(word, accent_c) or unstressed_words[word] then -- add MACRON to the last vowel not the first one -- adding the MACRON after the 'u' words[i] = rsub(word, "^(.*" .. V .. ")", "%1" .. MACRON) end end text = table.concat(words, " ") -- Convert hyphens to spaces text = rsub(text, "%-", " ") -- canonicalize multiple spaces again, which may have been introduced by hyphens text = canon_spaces(text) -- now eliminate punctuation text = rsub(text, "[!?']", "") -- put # at word beginning and end and double ## at text/foot boundary beginning/end text = rsub(text, " | ", "# | #") text = "##" .. rsub(text, " ", "# #") .. "##"

table.insert(debug, text)

--determining whether "y" or "w" is a consonant or a Vowel text = rsub(text, "y(" .. V .. ")", "ɟ%1") -- not the real sound text = rsub(text, "y#", "i") text = rsub(text, "w(" .. V .. ")","w%1") text = rsub(text, "w#","u")

-- handle certain combinations; ch, ll, ng and sh handling needs to go first text = rsub(text, "ch", "ć") --not the real sound text = rsub(text, "ll", "ʎ") text = rsub(text, "ñg", "ŋ") --use this for NG in loanwords from Philippine languages. Otherwise will be handled as Spanish NG	text = rsub(text, "sh", "ʃ")

--x text = rsub(text, "x", "ks")

--c, g, q	text = rsub(text, "c([ie])", "s%1") text = rsub(text, "g([ie])", "x%1") text = rsub(text, "gu(" .. V .. ")", "ɡ%1") text = rsub(text, "gü(" .. V .. ")", "ɡw%1") text = rsub(text, "qu([ie])", "k%1") text = rsub(text, "ü", "u")

table.insert(debug, text)

--alphabet-to-phoneme text = rsub(text, "[cfghjñrvyz7]",	--["g"]="ɡ": U+0067 LATIN SMALL LETTER G → U+0261 LATIN SMALL LETTER SCRIPT G    --J is for Spanish J. To handle non-Spanish J, respell it to DI    --7 is for glottal stop in loanwords from Philippine languages.		{ ["c"] = "k", ["f"] = "p", ["g"] = "ɡ", ["j"] = "x", ["ñ"] = "ɲ", ["q"] = "k", ["r"] = "ɾ", ["v"] = "b", ["y"] = "ɟ", ["z"] = "s", ["7"] = "ʔ" })

-- trill in rr (as in serra, erre) text = rsub(text, "ɾɾ", "r") text = rsub(text, "([#lns])ɾ", "%1r")

-- reduce any remaining double consonants; -- do this before handling of -nm- e.g. in inmigracion, which generates a double consonant, and do this text = rsub(text, "(" .. C .. ")%1", "%1")

-- voiceless stop to voiced before obstruent or nasal; but intercept -ts-, -tz- local voice_stop = { ["p"] = "b", ["t"] = "d", ["k"] = "ɡ" } text = rsub(text, "t(" .. separator_c .. "*[s])", "!%1") -- temporary symbol text = rsub(text, "([ptk])(" .. separator_c .. "*" .. T .. ")",			function(stop, after)				return voice_stop[stop] .. after			end) text = rsub(text, "!", "t")

text = rsub(text, "n([# .]*[bpm])", "m%1")

-- remove silent h before syllable division text = rsub(text, "h", "")

table.insert(debug, text)

--syllable division local vowel_to_glide = { ["i"] = "j", ["o"] = "w", ["u"] = "w" } -- i, o and u between vowels -> j and w. text = rsub_repeatedly(text, "(" .. V .. accent_c .. "*)([iou])(" .. V .. ")",			function(v1, iou, v2)				return v1 .. vowel_to_glide[iou] .. v2			end	) text = rsub_repeatedly(text, "(" .. V .. accent_c .. "*)(" .. C .. W .. "?" .. V .. ")", "%1.%2")	text = rsub_repeatedly(text, "(" .. V .. accent_c .. "*" .. C .. ")(" .. C .. V .. ")", "%1.%2")	text = rsub_repeatedly(text, "(" .. V .. accent_c .. "*" .. C .. "+)(" .. C .. C .. V .. ")", "%1.%2")	text = rsub(text, "([pbktdɡ])%.([lɾ])", ".%1%2") text = rsub_repeatedly(text, "(" .. C .. ")%.s(" .. C .. ")", "%1s.%2") -- Any aeo, or stressed iu, should be syllabically divided from a following aeo or stressed iu. text = rsub_repeatedly(text, "([aeo]" .. accent_c .. "*)([aeo])", "%1.%2") text = rsub_repeatedly(text, "([aeo]" .. accent_c .. "*)(" .. V .. stress_c .. ")", "%1.%2")	text = rsub(text, "([iu]" .. stress_c .. ")([aeo])", "%1.%2") text = rsub_repeatedly(text, "([iu]" .. stress_c .. ")(" .. V .. stress_c .. ")", "%1.%2")	text = rsub_repeatedly(text, "i(" .. accent_c .. "*)i", "i%1.i") text = rsub_repeatedly(text, "u(" .. accent_c .. "*)u", "u%1.u")

table.insert(debug, text)

--diphthongs text = rsub(text, "ih?([aeou])", "j%1") text = rsub(text, "uh?([aeio])", "w%1")

table.insert(debug, text)

local accent_to_stress_mark = { [AC] = "ˈ", [CFLEX] = "ˈʔ", [MACRON] = "" }

local function accent_word(word, syllables) -- Now stress the word. If any accent exists in the word (including ^ indicating an unaccented word), -- put the stress mark(s) at the beginning of the indicated syllable(s). Otherwise, apply the default -- stress rule. if rfind(word, accent_c) then for i = 1, #syllables do syllables[i] = rsub(syllables[i], "^(.*)(" .. accent_c .. ")(.*)$",						function(pre, accent, post)							return accent_to_stress_mark[accent] .. pre .. post						end				) end else -- Default stress rule. Words without vowels (e.g. IPA foot boundaries) don't get stress. Follow Spanish rules for stress. if #syllables > 1 and rfind(word, "[^aeiouàèìòùnŋs#]#") or #syllables == 1 and rfind(word, "[aeiouàèìòù]") 	then syllables[#syllables] = "ˈ" .. syllables[#syllables] elseif #syllables >= 2 then syllables[#syllables - 1] = "ˈ" .. syllables[#syllables - 1] end end end

local words = rsplit(text, " ") for j, word in ipairs(words) do		-- accentuation local syllables = rsplit(word, "%.")

if rfind(word, "men%.te#") then local mente_syllables -- Words ends in -mente (converted above to ménte); add a stress to the preceding portion -- (e.g. agriamente -> 'ágriaménte') unless already stressed (e.g. rápidamente). -- It will be converted to secondary stress further below. Essentially, we rip the word apart -- into two words ('mente' and the preceding portion) and stress each one independently. mente_syllables = {} mente_syllables[2] = table.remove(syllables) mente_syllables[1] = table.remove(syllables) accent_word(table.concat(syllables, "."), syllables) accent_word(table.concat(mente_syllables, "."), mente_syllables) table.insert(syllables, mente_syllables[1]) table.insert(syllables, mente_syllables[2]) else accent_word(word, syllables) end

-- Vowels are nasalized if followed by nasal in same syllable. if phonetic then for i = 1, #syllables do				-- first check for two vowels (veinte) syllables[i] = rsub(syllables[i], "(" .. V .. ")(" .. V .. ")([mnɲ])",						"%1" .. TILDE .. "%2" .. TILDE .. "%3") -- then for one vowel syllables[i] = rsub(syllables[i], "(" .. V .. ")([mnɲ])", "%1" .. TILDE .. "%2") end end

-- Reconstruct the word. words[j] = table.concat(syllables, phonetic and "." or "") end

text = table.concat(words, " ")

-- suppress syllable mark before IPA stress indicator text = rsub(text, "%.(" .. ipa_stress_c .. ")", "%1")	--make all primary stresses but the last one be secondary text = rsub_repeatedly(text, "ˈ(.+)ˈ", "ˌ%1ˈ")

table.insert(debug, text)

table.insert(debug,text) --correct final glottal stop placement text = rsub(text,"([ˈˌ])ʔ([ʔbćdfɡjĵɟklmnŋɲpɾrstwx])([aeiou])","%1%2%3ʔ")

--vowels with grave to vowel+glottal stop text = rsub(text,"à","aʔ") text = rsub(text,"è","eʔ") text = rsub(text,"ì","iʔ") text = rsub(text,"ò","oʔ") text = rsub(text,"ù","uʔ")

table.insert(debug,text)

--phonetic transcription if phonetic then

--/j/-coalescence before D, S, and T

text = rsub(text,"([dd̪])([ˈˌ.])j","%2ĵ") -- /d/ before /j/ text = rsub(text,"s([ˈˌ.])j","%1ʃ") -- /s/ before /j/ text = rsub(text,"t([ˈˌ.])j","%1ć") -- /t/ before /j/ text = rsub(text,"([ˈˌ.#])d([j])([aeiou])","%1ĵ%3") -- /dj/ at syllable onset text = rsub(text,"([ˈˌ.#])s([j])([aeiou])","%1ʃ%3") -- /sj/ at syllable onset text = rsub(text,"([ˈˌ.#])t([j])([aeiou])","%1ć%3") -- /tj/ at syllable onset

-- θ, s, f before voiced consonants local voiced = "mnɲbdɟɡʎl" local r = "ɾr" local tovoiced = { ["s"] = "z", }		local function voice(sound, following) return tovoiced[sound] .. following end text = rsub(text, "([s])(" .. separator_c .. "*[" .. voiced .. r .. "])", voice)

-- fricative vs. stop allophones; first convert stops to fricatives, then back to stops -- after nasals and sometimes after l		-- only affects Chavacano Y (/b/, /d/ /g/ are not affricates) local stop_to_fricative = { ["ɟ"] = "ʝ" } local fricative_to_stop = { ["ʝ"] = "ɟ" } text = rsub(text, "[ɟ]", stop_to_fricative) text = rsub(text, "([mnɲ]" .. separator_c .. "*)([ʝ])", function(nasal_l, fricative)					return nasal_l .. fricative_to_stop[fricative]				end		) text = rsub(text, "(##" .. ipa_stress_c .. "*)([ʝ])",				function(stress, fricative)					return stress .. fricative_to_stop[fricative]				end		) text = rsub(text, "[td]", { ["t"] = "t̪", ["d"] = "d̪" })

--dental s/z local dental = "dt" local todental = { ["s"] = "s̪", ["z"] = "z̪", }		local function dent(sound, following) return todental[sound] .. following end text = rsub(text, "([sz])(" .. separator_c .. "*[" .. dental .. "])", dent)

-- nasal assimilation before consonants local labiodental, dentialveolar, dental, palatal, velar, alveolar = "ɱ", "n̪", "n̟", "ɲ", "ŋ", "n" local nasal_assimilation = { ["t"] = dentialveolar, ["d"] = dentialveolar, ["ć"] = palatal, ["ɲ"] = palatal, ["ɟ"] = palatal, ["ʎ"] = palatal, ["k"] = velar, ["x"] = velar, ["ɡ"] = velar, ["s"] = alveolar, ["z"] = alveolar, ["l"] = alveolar, ["r"] = alveolar, ["ɾ"] = alveolar, ["n"] = alveolar, }		text = rsub(text, "n(" .. separator_c .. "*)(.)",				function(stress, following)					return (nasal_assimilation[following] or "n") .. stress .. following				end		)

-- lateral assimilation before consonants text = rsub(text, "l(" .. separator_c .. "*)(.)",				function(stress, following)					local l = "l"					if following == "t" or following == "d" then						-- dentialveolar						l = "l̪"					elseif following == "θ" then						-- dental						l = "l̟"					elseif following == "ć" or following == "ɲ" or following == "ɟ" or following == "ʎ" then						-- palatal						l = "ʎ"					end					return l .. stress .. following				end)

--semivowels text = rsub(text, "eu", "ju") text = rsub(text, "([aeouãẽõũ][iĩ])", "%1̯") text = rsub(text, "([aeioãẽĩõø][uũ])", "%1̯")

end

table.insert(debug, text)

-- convert fake symbols to real ones local final_conversions = { ["ć"] = "t͡ʃ", -- fake "ch" to real "ch" ["ɟ"] = "ʝ", -- fake "y" to real "y" }

local final_conversions_phonetic = { ["ć"] = "t͡ʃ", -- fake "ch" to real "ch" ["ɟ"] = "ɟ͡ʝ", -- fake "y" to real "y" ["ĵ"] = "d͡ʒ", -- fake "di+vowel" to real "di+vowel" }

if phonetic then text = rsub(text, "[ćɟĵ]", final_conversions_phonetic) end text = rsub(text, "[ćɟ]", final_conversions)

-- remove # symbols at word and text boundaries text = rsub(text, "#", "")

return mw.ustring.toNFC(text)

end

function export.show(frame) local params = { [1] = {},		["pre"] = {}, ["post"] = {}, ["bullets"] = { type = "number", default = 1 }, }

local parargs = frame:getParent.args local args = require("Module:parameters").process(parargs, params)

local results = {}

local text = args[1] or mw.title.getCurrentTitle.text

table.insert(results, { pron = "/" .. export.IPA(text, false) .. "/" }) table.insert(results, { pron = "[" .. export.IPA(text, true) .. "]" })

local bullet = string.rep("* ", args.bullets) .. " "	local pre = args.pre and args.pre .. " " or "" local post = args.post and " " or ""

return bullet .. pre .. m_IPA.format_IPA_full { lang = lang, items = results } .. post end

return export