This module is not to be directly used. It is used by Template:sei-IPA, see there for usage.


-- Based on [[Module:es-pronunc]] by: Benwing
-- Adapted by Santi2222

local export = {}

local m_IPA = require("Module:IPA")

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

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 TIL = u(0x0303) --tilde
local MAC = u(0x0331) --macron
local CFLEX = u(0x0302) -- circumflex =  ̂
local DIA = u(0x0308) -- diaeresis =  ̈

local vowel = "aeioAEIO"
local V = "[" .. vowel .. "]"
local accent = AC .. CFLEX
local accent_c = "[" .. accent .. "]"
local ipa_stress = "ˈˌ"
local ipa_stress_c = "[" .. ipa_stress .. "]"
local separator = accent .. ipa_stress .. "# ."
local C = "[^" .. vowel .. separator .. "]" -- consonant

local unstressed_words = require("Module:table").listToSet({ }) --add here unstressed words

-- 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

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

	text = ulower(text or mw.title.getCurrentTitle().text)
	-- decompose everything but ö, ḻ and ü (in loanwords only)
	text = rsub(text, "áa", "Á")
	text = rsub(text, "ée", "É")
	text = rsub(text, "íi", "Í")
	text = rsub(text, "óo", "Ó")

	text = mw.ustring.toNFD(text)
	text = rsub(text, ".[" .. DIA .. MAC .. "]", {
		["o" .. DIA] = "ö",
		["u" .. DIA] = "ü",
		["l" .. MAC] = "ḻ",
	})
	-- 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 certain monosyllabic words unstressed (not implemented yet)
	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 CFLEX to the last vowel not the first one, or we will mess up 'que' by
			-- adding the CFLEX after the 'u'
			words[i] = rsub(word, "^(.*" .. V .. ")", "%1" .. CFLEX)
		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)

	--transcription (with some fake symbols for convenience)
	text = rsub(text, "m([cqg])", "ŋ%1")
	text = rsub(text, "cö", "ć")
	text = rsub(text, "jö", "ś")
	text = rsub(text, "xö", "ź")
	text = rsub(text, "gü", "ǵ")
	text = rsub(text, "qu([ie])", "k%1")
	text = rsub(text, "[chfryzgvlḻxj]",
			{ ["c"] = "k", ["h"] = "ʔ", ["f"] = "ɸ", ["r"] = "ɾ", ["y"] = "j", ["z"] = "ʃ", ["g"] = "ɡ", ["v"]= "β", ["l"]="ɬ", ["ḻ"]="l",["x"]="χ",["j"]="x"})
	text = rsub(text, "ćk", "Ć")

	text = rsub(text, "aa", "A")
	text = rsub(text, "ee", "E")	
	text = rsub(text, "ii", "I")
	text = rsub(text, "oo", "O")
	text = rsub(text, "kk", "K")
	
	table.insert(debug, text)

	--syllable division (rudimentary)
	text = rsub_repeatedly(text, "(" .. V .. accent_c .. "*)(" .. C .. "?" .. 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_repeatedly(text, "(" .. C .. ")%.s(" .. C .. ")", "%1s.%2")

	table.insert(debug, text)

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

	local function heavy(syllable) --determines whether the (final) syllable is heavy
		if rfind(syllable, "[AEIOćĆǵś]") then
			return 1
		elseif string.len(syllable) > 2 then
			local sylmod = rsub(syllable, "#", "")
			if string.len(rsub(sylmod,".*[aeio]","")) > 2 then
				return 1
			end
		end
		return 0
	end
	
	local function findheavy(syls) --function that finds the position of a heavy syllable consisting of a long vowel
		for s = 1, (#syls - 1) do
			if rfind(syls[s], "[AEIO]") then
				return s
			end
		end
		return 0
	end
	
	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
			-- Graphic stress marker rules given by R:sei:dict
			if #syllables > 1 then
				if findheavy(syllables) ~= 0 then
					syllables[findheavy(syllables)] = "ˈ" .. syllables[findheavy(syllables)]
				elseif heavy(syllables[#syllables]) == 1 then
					syllables[#syllables] = "ˈ" .. syllables[#syllables]
				else
					syllables[#syllables - 1] = "ˈ" .. syllables[#syllables - 1]
				end
			else
				syllables[#syllables ] = "ˈ" .. syllables[#syllables]
			end
		end
	end

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

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

	text = table.concat(words, " ")

	-- suppress syllable mark before IPA stress indicator
	text = rsub(text, "%.(" .. ipa_stress_c .. ")", "%1")

	--phonetic transcription
	if phonetic then
		text = rsub(text, "Km", "Kʍ")
		text = rsub(text, "km", "kʍ")
		text = rsub(text, "ćm", "ćʍ")
	end

	if not phonetic then
		text = rsub(text, "ŋ", "m")
	end
	
	table.insert(debug, text)

	-- convert fake symbols to the real ones
	local final_conversions = {
		["ć"] = "kʷ",
		["Ć"] = "kʷː",
		["ǵ"] = "ɡʷ",
		["ś"] = "xʷ",
		["ź"] = "χʷ",
		["K"] = "kː",
		["A"] = "ɑː",
		["E"] = "ɛː",
		["I"] = "iː",
		["O"] = "oː",
		["a"] = "ɑ",
		["e"] = "ɛ"
	}

	text = rsub(text, "[ćĆśźǵKAEIOae]", final_conversions)

	if phonetic then
		text = rsub(text, "ʍɑ", "w" .. TIL .. "ɑ" .. TIL)
		text = rsub(text, "ʍɛ", "w" .. TIL .. "ɛ" .. TIL)
		text = rsub(text, "ʍi", "w" .. TIL .. "i" .. TIL)
		text = rsub(text, "ʍo", "w" .. TIL .. "o" .. TIL)
	end

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

	return mw.ustring.toNFC(text)
end

function export.show(frame)
	local params = {
		[1] = {},
		["pre"] = {},
	}

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

	local results = {}

	local text = args[1] or mw.title.getCurrentTitle().text
	
	local phonemic = export.IPA(text, false)
	local phonetic = export.IPA(text, true)

	table.insert(results, { pron = "/" .. phonemic .. "/" })
	if phonemic ~= phonetic then --a phonetic transcription will be added only if it differs from the phonemic one
		table.insert(results, { pron = "[" .. phonetic .. "]" })
	end
	
	local pre = args.pre and args.pre .. " " or ""

	return "* " .. pre .. m_IPA.format_IPA_full(lang, results)
end

return export