Module:User:AmazingJus/turkish

From blackwiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:User:AmazingJus/turkish/doc

local export = {}
-- local m_IPA = require("Module:IPA")
local U = mw.ustring.char
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local gmatch = mw.ustring.gmatch
local function lc(str)
	return mw.getLanguage("tr"):lc(str)
end

-- create lookup table
local function stringToLookup(str)
	local lookup = {}
	
	for char in gmatch(str, ".") do
		lookup[char] = true
	end
	
	return lookup
end

local breve = U(0x361)
local stress = "ˈ"


local cons = {
	["c"] = "ʤ", ["ç"] = "ʧ", ["g"] = "ɡ", ["ğ"] = "ɰ", ["j"] = "ʒ", ["r"] = "ɾ", ["ş"] = "ʃ", ["y"] = "j"
}

local vowels = {
	["â"] = "aː", ["ı"] = "ɯ", ["î"] = "iː", ["ö"] = "ø", ["û"] = "uː", ["ü"] = "y"
}

local devoicing = {
	["b"] = "p", ["c"] = "ç", ["d"] = "t", ["g"] = "k",
}

local b = "aıou"
local c = "bcçdfgğhjklmnprsştvyz"
local f = "âeiîöûü"
local v = "aâeıiîoöuûü"

-- create tables to determine whether something is a consonant or a vowel
-- format: { a = true, â = true, e = true, ... }
local vowelTable = stringToLookup(v)
local consonantTable = stringToLookup(c)

local function insert(str, i, thing)
	if i > #str then
		error("Index " .. i .. " is greater than the length of the string.")
	end
	return mw.ustring.sub(str, 1, i - 1) .. thing .. mw.ustring.sub(str, i, #str)
end

--[[
	Operates on a word in Turkish orthography, not IPA.
	Gets list of last two or three vowels (in the order ultima, penult,
	antepenult), and the following consonants of the penult and antepenult (in
	that order), in order to apply the Sezer stress rule.
	
	Places stress before vowel. A later rule will have to move it to the correct
	position.
--]]

function export.sezer(text)
	text = lc(text)
	
	-- syllables: actually vowels
	local syllables = {}
	-- consonants: consonants following antepenult and penult vowels
	local consonants = {}
	
	-- get first three syllables
	for i = mw.ustring.len(text), 1, -1 do
		local char = mw.ustring.sub(text, i, i)
		
		-- create an entry for the first three vowels in the word.
		if vowelTable[char] then
			table.insert(syllables, { pos = i })
			if #syllables == 3 then
				break
			end
		
		-- log each non-final consonant in a table at the same index as the
		-- vowel that precedes it (ignore final consonants, which aren't
		-- relevant for the rule)
		elseif next(syllables) and consonantTable[char] then
			local consI = #syllables + 1
			if not consonants[consI] then
				consonants[consI] = {}
			end
			table.insert(consonants[consI], char)
		end
	end
	
	-- rule only applies in words of two or more syllables
	if #syllables < 2 then
		return nil
	end
	
	if #syllables == 2 then
		return insert(text, syllables[2].pos, stress)
	end
	
	if #syllables >= 3 then
		if consonants[3] and consonants[2] and #consonants[3] >= 2 and #consonants[2] <= 1 then
			return insert(text, syllables[3].pos, stress)
		else
			return insert(text, syllables[2].pos, stress)
		end
	end
	
	error("The function Sezer failed to add a stress mark.")
end

function export.phonemic(text)
	if type(text) == "table" then
		text = text.args[1]
	end

	text = lc(text)
	text = " " .. text .. " " -- make all word borders have a space

	text = gsub(text, "([bcdg]+)([çfhkpsşt])", function(prev_cons, next_cons)
		return devoicing[prev_cons] .. next_cons
	end)

	text = gsub(text, ".", cons)

	text = gsub(text, "([" .. f .. "])ɡ", "%1ɟ")
	text = gsub(text, "([" .. f .. "])k", "%1c")
	text = gsub(text, "([" .. b .. "])l([^" .. f .. "])", "%1ɫ%2")

	text = gsub(text, "ɡ([" .. f .. "])", "ɟ%1")
	text = gsub(text, "k([" .. f .. "])", "c%1")
	text = gsub(text, "([^" .. f .. "])l([" .. b .. "])", "%1ɫ%2")

	text = gsub(text, "ck", "cc")
	text = gsub(text, "kc", "cc")
	text = gsub(text, "ɡɟ", "ɟɟ")
	text = gsub(text, "ɟɡ", "ɟɟ")
	text = gsub(text, "lɫ", "ll")
	text = gsub(text, "ɫl", "ll")

--[[
	if match(text, "[" .. v .. "]") then
		text = gsub(text, "([" .. v .. "])", "%1.")
		text = gsub(text, "([" .. v .. "]).([^" .. v .. "]*) ", "%1%2 ")
		text = gsub(text, "([" .. v .. "])\.([^" .. v .. "]+)([^" .. v .. "])", "%1%2.%3")
	end
--]]

	text = gsub(text, "\.ˈ", "ˈ")

	text = gsub(text, "ʤ", "d" .. breve .. "ʒ")
	text = gsub(text, "ʧ", "t" .. breve .. "ʃ")


	text = gsub(text, ".", vowels)
	
	return mw.text.trim(text)
end

return export