模組:Urj-fin-common



local export = {}

-- Split the word into syllables of C(C)V(V) shape
function export.split_syllables(word)
	local syllables = {}
	local remainder = word
	
	while remainder ~= "" do
		local syll = ""
		
		if mw.ustring.find(remainder, "^([^aeiouäöü%-]+)") then
			syll = syll .. mw.ustring.match(remainder, "^([^aeiouäöü%-]+)")
			remainder = mw.ustring.gsub(remainder, "^([^aeiouäöü%-]+)", "")
		end
		
		if mw.ustring.find(remainder, "^([aeiouäöü%-]+)") then
			syll = syll .. mw.ustring.match(remainder, "^([aeiouäöü%-]+)")
			remainder = mw.ustring.gsub(remainder, "^([aeiouäöü%-]+)", "")
		end
		
		table.insert(syllables, syll)
	end
	
	-- Assume that suffixes are attached to words of at least two syllables.
	if mw.ustring.find(word, "^%-") then
		table.insert(syllables, 1, "")
	end
	
	return syllables
end

-- Apply gradation to a word
function export.apply_gradation(word)
	local syllables = export.split_syllables(word)
	
	for i, syll in ipairs(syllables) do
		-- The first and last consonant do not gradate
		if i > 1 then
			-- Apply suffixal gradation to single consonants at the beginning of odd-numbered syllables
			if i % 2 == 1 and mw.ustring.find(syll, "^[ptks][aeiouäöü]") then
				syll = mw.ustring.gsub(syll, "^p", "b")
				syll = mw.ustring.gsub(syll, "^t", "d")
				syll = mw.ustring.gsub(syll, "^k", "g")
				syll = mw.ustring.gsub(syll, "^s", "h")
			end
			
			-- Apply radical gradation
			-- Does the next syllable begin with more than one consonant, or does it contain no vowels (final consonant)?
			if i < #syllables and (mw.ustring.find(syllables[i+1], "^[^aeiouäöü][^aeiouäöü]") or not mw.ustring.find(syllables[i+1], "[aeiouäöü]")) then
				syll = mw.ustring.gsub(syll, "^([lrmn]?)pp([aeiouäöü])", "%1p'%2")
				syll = mw.ustring.gsub(syll, "^([lrmn]?)tt([aeiouäöü])", "%1t'%2")
				syll = mw.ustring.gsub(syll, "^([lrmn]?)kk([aeiouäöü])", "%1k'%2")
				syll = mw.ustring.gsub(syll, "^([lrmn]?)cc([aeiouäöü])", "%1c'%2")
				syll = mw.ustring.gsub(syll, "^([lrmn]?)p([aeiouäöü])", "%1b%2")
				syll = mw.ustring.gsub(syll, "^([lrmn]?)t([aeiouäöü])", "%1d%2")
				syll = mw.ustring.gsub(syll, "^([lrmn]?)k([aeiouäöü])", "%1g%2")
			end
		end
		
		syllables[i] = syll
	end
	
	return table.concat(syllables, "")
end

function export.make_stems(stem, contract_ctt)
	local stems = {normal = stem}
	
	stems.final = stems.normal
	
	if mw.ustring.find(stems.normal, "[^aeiouäöü]$") then
		stems.normal = stems.normal .. "e"
	end
	
	stems.i = stems.normal .. "i"
	stems.k = stems.normal .. "k"
	stems.n = stems.normal .. "n"
	stems.t = stems.normal .. "t"
	stems.types = {"unknown"}
	
	local syllables = export.split_syllables(stems.normal)
	
	
	-- Stems ending in a long vowel replace the second vowel with a diphthong
	if mw.ustring.find(stems.normal, "([aeiouäöü])%1$") then
		stems.types = {"long vowel"}
		stems.i = mw.ustring.gsub(stems.i, "[aeiouäöü]i$", "i")
	-- Stems ending in a u-diphthong replace u with v
	elseif mw.ustring.find(stems.normal, "([aeioäö])[uü]$") then
		stems.types = {"u-diphthong"}
		stems.i = mw.ustring.gsub(stems.i, "[uü]i$", "vi")
	-- Stems ending in a i-diphthong have don't get an extra -i
	elseif mw.ustring.find(stems.normal, "([aeouäöü])i$") then
		stems.types = {"i-diphthong"}
		stems.i = mw.ustring.gsub(stems.i, "ii$", "i")
	elseif #syllables == 1 then
		stems.types = {"short monosyllable"}
	else
		-- Stems ending in a simple -i get -eji- in the plural
		if mw.ustring.find(stems.normal, "i$") then
			stems.types = {"i"}
			stems.i = mw.ustring.gsub(stems.i, "ii$", "eji")
		elseif mw.ustring.find(stems.normal, "([ouöü])$") then
			stems.types = {"rounded"}
		-- Stems ending in -ä replace the vowel with -ei-.
		elseif mw.ustring.find(stems.normal, "([^aeiouäöü])ä$") then
			if #syllables > 1 then
				stems.types = {"a-ei"}
				stems.i = mw.ustring.gsub(stems.i, "äi$", "ei")
			end
		-- Stems ending in -a replace the vowel with -oi- in the past stem,
		-- but only if the preceding syllable does not contain rounded vowels.
		-- Otherwise, replace with -ei- as above.
		elseif mw.ustring.find(stems.normal, "([^aeiouäöü])a$") then
			if #syllables > 1 then
				if not mw.ustring.find(syllables[#syllables-1], "[ou]") then
					stems.types = {"a-oi"}
					stems.i = mw.ustring.gsub(stems.i, "ai$", "oi")
				else
					stems.types = {"a-ei"}
					stems.i = mw.ustring.gsub(stems.i, "ai$", "ei")
				end
			end
		-- Stems ending in -e may drop the -e in some forms, resulting in contraction
		elseif mw.ustring.find(stems.normal, "[^aeiouäöü]e$") then
			stems.types = {"e"}
			
			stems.final = mw.ustring.gsub(stems.final, "e$", "i")
			stems.final = mw.ustring.gsub(stems.final, "ji$", "i")
			
			stems.i = mw.ustring.gsub(stems.i, "ei$", "i")
			
			if mw.ustring.find(stems.normal, "cce$") then
				stems.types = {"cce"}
				stems.t = mw.ustring.gsub(stems.t, "ccet$", "ct")
			elseif mw.ustring.find(stems.normal, "[pk][cs]e$") then
				stems.types = {"kce, pce, kse, pse"}
				stems.k = mw.ustring.gsub(stems.k, "[kp]([cs])ek$", "%1k")
				stems.n = mw.ustring.gsub(stems.n, "[kp]sen$", "ss")
				stems.t = mw.ustring.gsub(stems.t, "[kp]([cs])et$", "%1t")
			elseif mw.ustring.find(stems.normal, "[aeiouäöü%-]ce$") then
				stems.types = {"Vce"}
				stems.n = mw.ustring.gsub(stems.n, "cen$", "nn")
				stems.t = mw.ustring.gsub(stems.t, "cet$", "ct")
			elseif mw.ustring.find(stems.normal, "[aeiouäöü%-]he$") then
				stems.types = {"Vhe"}
				stems.t = mw.ustring.gsub(stems.t, "het$", "ht")
			elseif mw.ustring.find(stems.normal, "[aeiouäöü%-]ke$") then
				stems.types = {"Vke"}
				stems.k = mw.ustring.gsub(stems.k, "kek$", "kk")
				
				if stems.normal == "näke" or stems.normal == "teke" or #syllables > 2 then
					stems.t = mw.ustring.gsub(stems.t, "ket$", "kt")
				end
			elseif mw.ustring.find(stems.normal, "[aeiouäöü%-][lnrs]e$") then
				stems.types = {"Vle, Vne, Vre, Vse"}
				stems.k = mw.ustring.gsub(stems.k, "([lnrs])ek$", "%1k")
				stems.n = mw.ustring.gsub(stems.n, "([lnrs])en$", "%1n")
				stems.n = mw.ustring.gsub(stems.n, "([lns])n$", "%1%1")
				stems.t = mw.ustring.gsub(stems.t, "([lnrs])et$", "%1t")
			elseif mw.ustring.find(stems.normal, "[aeiouäöü%-]me$") then
				stems.types = {"Vme"}
				stems.t = mw.ustring.gsub(stems.t, "met$", "nt")
			elseif mw.ustring.find(stems.normal, "[chst]te$") then
				stems.types = {"cte, hte, ste, tte"}
			elseif mw.ustring.find(stems.normal, "kte$") then
				stems.types = {"kte"}
				
				stems.final = mw.ustring.gsub(stems.final, "ti$", "ci")
				stems.i = mw.ustring.gsub(stems.i, "ti$", "ci")
				
				if contract_ctt then
					stems.t = mw.ustring.gsub(stems.t, "ktet$", "kt")
				end
			elseif mw.ustring.find(stems.normal, "nte$") then
				stems.types = {"nte"}
				
				stems.final = mw.ustring.gsub(stems.final, "ti$", "ci")
				stems.i = mw.ustring.gsub(stems.i, "ti$", "ci")
				stems.n = mw.ustring.gsub(stems.n, "nten$", "nn")
				
				if contract_ctt then
					stems.t = mw.ustring.gsub(stems.t, "ntet$", "tt")
				end
			elseif mw.ustring.find(stems.normal, "[aeiouäöü%-]te$") then
				stems.types = {"Vte"}
				
				stems.final = mw.ustring.gsub(stems.final, "ti$", "ci")
				stems.i = mw.ustring.gsub(stems.i, "ti$", "ci")
				
				stems.k = mw.ustring.gsub(stems.k, "tek$", "tk")
				stems.n = mw.ustring.gsub(stems.n, "ten$", "nn")
				stems.t = mw.ustring.gsub(stems.t, "tet$", "tt")
			elseif mw.ustring.find(stems.normal, "te$") then
				stems.types = {"te"}
				
				stems.final = mw.ustring.gsub(stems.final, "ti$", "ci")
				stems.i = mw.ustring.gsub(stems.i, "ti$", "ci")
			end
			
			if #syllables >= 3 then
				stems.final = mw.ustring.gsub(stems.final, "i$", "")
			end
			
			-- Simplify final consonant clusters
			stems.final = mw.ustring.gsub(stems.final, "[^aeiouäöü%-]+([^aeiouäöü])$", "%1")
			stems.final = mw.ustring.gsub(stems.final, "m$", "n")
		end
	end
	
	return stems
end

function export.detect_harmony(stem)
	local vowels = {}
	vowels.a = "ä"
	vowels.u = "ü"
	
	if mw.ustring.find(stem, "[aou]") then
		vowels.a = "a"
		vowels.u = "u"
	end
	
	return vowels
end

return export