Module:ka-IPA

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This module implements the {{ka-IPA}} template.


local export = {}

local ipa = require("Module:IPA")
local lang = require("Module:languages").getByCode("ka")

local gsub = mw.ustring.gsub
local lower = mw.ustring.lower

local mapping = {
	["ა"] = "a", ["ბ"] = "b", ["გ"] = "ɡ", ["დ"] = "d",
	["ე"] = "e", ["ვ"] = "v", ["ზ"] = "z", ["თ"] = "tʰ",
	["ი"] = "i", ["კ"] = "kʼ", ["ლ"] = "l", ["მ"] = "m",
	["ნ"] = "n", ["ო"] = "o", ["პ"] = "pʼ", ["ჟ"] = "ʒ",
	["რ"] = "r", ["ს"] = "s", ["ტ"] = "tʼ", ["უ"] = "u",
	["ფ"] = "pʰ", ["ქ"] = "kʰ", ["ღ"] = "ɣ", ["ყ"] = "qʼ",
	["შ"] = "ʃ", ["ჩ"] = "t͡ʃ", ["ც"] = "t͡s", ["ძ"] = "d͡z",
	["წ"] = "t͡sʼ", ["ჭ"] = "t͡ʃʼ", ["ხ"] = "x", ["ჯ"] = "d͡ʒ",
	["ჰ"] = "h",
	["ჶ"] = "f" -- special character
}
local narrow = {
	["რ"] = "ɾ", ["ღ"] = "ʁ", ["ყ"] = "χʼ", ["ჩ"] = "t͡ʃʰ",
	["ც"] = "t͡sʰ", ["ხ"] = "χ"
}

function export.pronunciation(word, is_phonetic)
	-- make text lowercase
	word = lower(word)

	require("Module:script utilities").checkScript(word, "Geor")

	if is_phonetic then
		-- /v/ is labialised to the previous consonant
		word = gsub(word, "([ბგდვზთკლმნპჟრსტფქღყშჩცძწჭხჯჰ])ვ", "%1ʷ")

		-- /v/ is devoiced before a devoiced consonant
		word = gsub(word, "ვ([თკპსტფქშჩცწჭხჰ])", "f%1")

		-- /l/ is velarised before a back vowel
		word = gsub(word, "ლ([აოუ])", "ɫ%1")

		-- /n/ is velarised before a velar consonant
		word = gsub(word, "ნ([ქკგ])", "ŋ%1")

		-- word-initial /b, d, ɡ/ are devoiced
		local bdg_initial = { ["ბ"] = "b̥", ["დ"] = "d̥", ["გ"] = "ɡ̊" }
		word = gsub(word, "^([ბდგ])", bdg_initial)
		word = gsub(word, "(%s)([ბდგ])", function(s, c) return s .. bdg_initial[c] end)

		-- word-final /b, d, ɡ/ are devoiced and aspirated
		local bdg_final = { ["ბ"] = "ფ", ["დ"] = "თ", ["გ"] = "ქ" }
		word = gsub(word, "([ბდგ])$", bdg_final)
		word = gsub(word, "([ბდგ])(%s)", function(c, s) return bdg_final[c] .. s end)

		word = gsub(word, ".", narrow)
	end

	word = gsub(word, "%p", "")
	word = gsub(word, ".", mapping)

	return word
end

function export.show(frame)
	local args = frame:getParent().args
	local pagetitle = mw.title.getCurrentTitle().text

	local p, results = {}, {}

	if args[1] then
		for _, v in ipairs(args) do
			if v == "+" then v = pagetitle end
			table.insert(p, (v ~= "") and v or nil)
		end
	else
		if mw.title.getCurrentTitle().nsText == "Template" then
			p = {"ქართული ენა"}
		else p = { pagetitle } end
	end

	local has_f = false

	for input_index, word in ipairs(p) do
		-- check for character ჶ /f/
		if mw.ustring.find(word, "ჶ") then
			has_f = true
		end

		local phonemic = export.pronunciation(word, false)
		local phonetic = export.pronunciation(word, true)

		table.insert(results, { pron = "/" .. phonemic .. "/" })

		local qual = args["q"..input_index]
		if qual then
			results[#results].qualifiers = { qual }
		end

		if phonemic ~= phonetic then table.insert(results, { pron = "[" .. phonetic .. "]" }) end
	end

	local ret = ipa.format_IPA_full { lang = lang, items = results }

	if has_f then
		ret = ret .. require("Module:utilities").format_categories({"Georgian terms with /f/"}, lang)
	end

	return ret
end

return export