Die Dokumentation für dieses Modul kann unter Modul:eo-pron/Doku erstellt werden

-- importiert aus dem Englischen Wiktionary, erstellt vom Nutzer DTLHS
-- https://en.wiktionary.org/wiki/User:DTLHS
-- an das deutsche Wiktionary angepasst vom Nutzer Formatierer
-- https://de.wiktionary.org/wiki/User:Formatierer

local export = {}

local consonants = {
	["b"] = "b",
	["c"] = "t͡s",
	["ĉ"] = "t͡ʃ",
	["d"] = "d",
	["f"] = "f",
	["g"] = "ɡ",
	["ĝ"] = "d͡ʒ",
	["h"] = "h",
	["ĥ"] = "x",
	["j"] = "j",
	["ĵ"] = "ʒ",
	["k"] = "k",
	["l"] = "l",
	["m"] = "m",
	["n"] = "n",
	["p"] = "p",
	["r"] = "r",
	["s"] = "s",
	["ŝ"] = "ʃ",
	["t"] = "t",
	["v"] = "v",
	["z"] = "z",
	['ŭ'] = "w"}

local vowels = {
	["a"] = "a",
	["e"] = "e",
	["i"] = "i",
	["o"] = "o",
	["u"] = "u",

}

local letters_phonemes = {}

-- combine into single table
for k,v in pairs(vowels) do letters_phonemes[k] = v end
for k,v in pairs(consonants) do letters_phonemes[k] = v end

function term_to_words(term)
	-- split by spaces, hyphens, or periods
	local words = mw.text.split(term, '[%s|%-|%.]')
	return words
end
function string_to_letters(term)
	return mw.text.split(term, "")
end

function letters_to_syllables(letters)

	if table.getn(letters) == 1 then
		return {[1] = letters[1]}
	end
	local l_r_exceptions = {["m"] = true, ["n"] = true, ["ŭ"] = true, ["j"] = true}

	local result = {[1] = ""}
	local j = 1
	for i=1, (table.getn(letters) - 2) do
		result[j] = result[j] .. letters[i]
		local letter = letters[i]
		local letter1 = letters[i+1]
		local letter2 = letters[i+2]
		
		if (vowels[letter] ~= nil) then
			if (consonants[letter1] ~= nil) and (vowels[letter2] ~= nil) then
				-- single consonant goes with following vowel
				if has_vowel(result[j])==true and (letter1 ~= 'ŭ') then
					j = j + 1
					result[j] = ""
				end
				
			elseif (consonants[letter1] ~= nil) and (l_r_exceptions[letter1] == nil) and (letter2 == 'l' or letter2 == 'r') and (letter1 ~= 'l' and letter1 ~= 'r') then
				-- consonant followed by l or r goes with l or r
				if has_vowel(result[j])==true then
					j = j + 1
					result[j] = ""
				end

			elseif (vowels[letter1] ~= nil) then
				-- two vowels
				if has_vowel(result[j])==true then
					j = j + 1
					result[j] = ""
				end
			end
		elseif (consonants[letter] ~= nil) then
			if (consonants[letter1] ~= nil) and (vowels[letter2] ~= nil) then
				if (mw.ustring.len(result[j]) ~= 1) then
					-- single consonant goes with following vowel
					if has_vowel(result[j])==true then
						j = j + 1
						result[j] = ""
					end
				end
			elseif (consonants[letter1] ~= nil) and (l_r_exceptions[letter1] == nil) and (letter2 == 'l' or letter2 == 'r') and (letter1 ~= 'l' and letter1 ~= 'r') then
				-- consonant followed by l or r goes with l or r
				if has_vowel(result[j])==true then
					j = j + 1
					result[j] = ""
				end

			elseif (vowels[letter1] ~= nil) then
				-- two vowels
				if has_vowel(result[j])==true then
					j = j + 1
					result[j] = ""
				end
			end
		end
	end
	
	-- add last two letters
	if table.getn(letters) >= 2 then
		local c1 = letters[table.getn(letters)-1]
		local c2 = letters[table.getn(letters)]
			
		if c1 ~= 'ŭ' then
            if (vowels[c1] ~= nil) and (vowels[c2] ~= nil) then
                result[j] = result[j] .. c1
                j = j + 1
                result[j] = c2
            else
                if has_vowel(result[j]) then
                	if has_vowel(c1 .. c2) then
	                	j = j + 1
    	            	result[j] = c1 .. c2
	            	else
	            		result[j] = result[j] .. c1 .. c2
            		end
            		
                else
                	result[j] = result[j] .. c1 .. c2
            	end
        	end
        	
        else
            if (vowels[letters[table.getn(letters)-2]] ~= nil) and (vowels[c2] ~= nil) then
            	result[j] = result[j] .. c1
                j = j + 1
                result[j] = c2
            else
                if has_vowel(result[j]) then
                	if has_vowel(c1 .. c2) then
	                	j = j + 1
    	            	result[j] = c1 .. c2
	            	else
	            		result[j] = result[j] .. c1 .. c2
            		end
            		
                else
                    result[j] = result[j] .. c1 .. c2   
                end
            end
        end
    end
    

	result2 = {}
	for i, j in pairs(result) do
		if j ~= "" and j ~= nil then
			table.insert(result2, j)
		end
	end
	return result2
end
function string_to_syllables(term)
	-- split if given artificial syllable breaks
	local split_input = mw.text.split(term, '·', true)
	local result = {}
	for i, split in pairs(split_input) do
		for j, syllable in pairs(letters_to_syllables(string_to_letters(split))) do
			table.insert(result, syllable)
		end
	end
	
	return result
	
	end
function string_to_ipa(syllable)

	local syllable_letters = string_to_letters(syllable)
	local syllable_ipa = ""
	
	for k, letter in pairs(syllable_letters) do
		if letters_phonemes[letter] ~= nil then
			syllable_ipa = syllable_ipa .. letters_phonemes[letter]
		end

	end
	return syllable_ipa
end

function has_vowel(term)
	local letters = string_to_letters(term)
	
	for i,j in pairs(letters) do
		if vowels[j] ~= nil then
			return true
		end
	end
	return false
end
function strip_initial_consonants(term)
	local letters = string_to_letters(term)
	local result = {}
	
	local gate = false
	for i,j in pairs(letters) do
		if vowels[j] ~= nil then
			gate = true
		end
		
		if gate == true then
			table.insert(result, j)
		end
	end
	
	return table.concat(result)
end
	

function term_to_IPA_and_rhyme(term)
	
	local words = term_to_words(term)
	local result = {}

	for i,word in pairs(words) do
		if word ~= "" then
			-- add /o/ if word is a single character and a consonant
			if mw.ustring.len(word) == 1 then
				if (consonants[word] ~= nil) then
					word = word .. 'o'
				end
			end
			
			-- break into syllables and make each into IPA
			local hyphenated = string_to_syllables(word)
			local word_result = {}
			for j, syllable in pairs(hyphenated) do
				local syllable_ipa = string_to_ipa(syllable)
				word_result[j] = syllable_ipa
			end

			-- add stress to penultimate syllable, and set rhyme to last two syllables
			if (table.getn(word_result) > 1) then
				rhyme_letters = strip_initial_consonants(hyphenated[table.getn(hyphenated) - 1] .. hyphenated[table.getn(hyphenated)])
				
				word_result[table.getn(word_result) - 1] = "ˈ" .. word_result[table.getn(word_result) - 1]
				
			end
			
			result[i] = table.concat(word_result)
		end
	end

	-- rhyme to ipa
	local rhyme = nil
	if rhyme_letters ~= nil then
		rhyme = string_to_ipa(rhyme_letters)
	end
	
	return {['IPA'] = result, ['rhyme'] = rhyme}

end

function parse_input(input)
	
	-- no input -> use page title
	if not input then
		output = mw.title.getCurrentTitle().text
		return mw.ustring.lower(output)
	end
	
	return mw.ustring.lower(input)

end

function export.IPA(IPA_input)
	IPA_input = parse_input(IPA_input)
	
	return  table.concat(term_to_IPA_and_rhyme(IPA_input).IPA, " ")
end

function export.rhyme(rhyme_input)
	rhyme_input = parse_input(rhyme_input)
	
	return term_to_IPA_and_rhyme(rhyme_input).rhyme
end

function export.hyphenation(hyphenation_input)
	hyphenation_input = parse_input(hyphenation_input)
	
	local words = term_to_words(hyphenation_input)
	local result = {}
	
	for i, word in pairs(words) do
		hyphenated = string_to_syllables(word)
		table.insert(result, table.concat(hyphenated, "·"))
	end
	
	return table.concat(result, ' ')
end

function export.letters(letters_input)
	letters_input = parse_input(letters_input)
	
	return table.concat(string_to_letters(letters_input), '-')
end

function get_artificial_breaks(p_input)
	-- override for syllable breaks
	if not p_input:getParent().args[1] then
		return nil
	end
	
	param = 1
	local result = {}
	while true do
		if not (p_input:getParent().args[param]) then
			if not result then
				return nil
			end
			
			return table.concat(result, "·")

		end
		table.insert(result, p_input:getParent().args[param])
		param = param + 1
	end
	
end

function export.pronunciation_section(p_input)
	
	local artificial_breaks = get_artificial_breaks(p_input)
	
	IPA_override = p_input:getParent().args["i"]
	if not IPA_override then
		IPA_override = export.IPA(artificial_breaks)
	end

	hyphenation_override = p_input:getParent().args["h"]
	if not hyphenation_override then
		hyphenation_override = export.hyphenation(artificial_breaks)
	end
	
	rhyme_override = p_input:getParent().args["r"]
	if not rhyme_override then
		rhyme_override = export.rhyme(artificial_breaks)
	end
		
	audio = p_input:getParent().args["a"]

	if rhyme_override ~= nil then
		rhyme_override = p_input:expandTemplate{ title = "Reim", args = {[1] = rhyme_override, [2] = "Esperanto"}}
	end
	IPA_override = p_input:expandTemplate{ title = "Lautschrift", args = {[1] = IPA_override, ["spr"] = "eo"}}

	local result = ""
	result = result .. p_input:expandTemplate{ title = "Worttrennung" } .. "<dl><dd>" .. hyphenation_override .. "</dd></dl>"
	result = result .. p_input:expandTemplate{ title = "Aussprache" } .. "<dl>"
	result = result .. "<dd>" .. p_input:expandTemplate{ title = "IPA" } .. " " .. IPA_override .. "</dd>"

	--if audio ~= nil then
		audio = p_input:expandTemplate{ title = "Audio", args = {[1] = audio, ["spr"] = "eo"}}
		result = result .. "<dd>" .. p_input:expandTemplate{ title = "Hörbeispiele"} .. " " .. audio .. "</dd>"
	--end
	
	if rhyme_override ~= nil then
		result = result .. "<dd>" .. p_input:expandTemplate{ title = "Reime"} .. " " .. rhyme_override .. "</dd>"
	end
	
	result = result .. "</dl>"
	
	return result
end

return export