Module:pa-IPA/sandbox

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


local export = {}

local lang = require("Module:languages").getByCode("pa")
local sc = require("Module:scripts").getByCode("Gurm")
local m_IPA = require("Module:IPA")
local m_a = require("Module:accent qualifier")

local gsub = mw.ustring.gsub
local gmatch = mw.ustring.gmatch
local find = mw.ustring.find

local correspondences = {
	["ṅ"] = "ŋ", ["ਗ"] = "ɡ", 
	["ਚ"] = "t͡ʃ", ["ਜ"] = "d͡ʒ", 
	["ਟ"] = "ʈ", ["ਡ"] = "ɖ", ["ਣ"] = "ɳ",
	["ਤ"] = "t̪", ["ਦ"] = "d̪",
	["ਯ"] = "j", ["ਰ"] = "ɾ", ["ਵ"] = "ʋ", ["ਲ"] = "l̪",
	["ਸ਼"] = "ʃ", ["ਹ"] = "ɦ",
	["ੜ"] = "ɽ", ["ž"] = "ʒ", ["ਲ਼"] = "ɭ", ["ਗ਼"] = "ɣ", ["ਕ਼"] = "q", ["ਖ਼"] = "x",

	["ਅ"] = "ə", ["ਆ"] = "ɑː", ["ਾ"] = "ɑː", ["ਿ"] = "ɪ",
	["ੀ"] = "iː", ["ਈ"] = "iː", ["ਓ"] = "oː", ["ੋ"] = "oː", ["ਏ"] = "eː", ["ੇ"] = "eː",
	["ਉ"] = "ʊ", ["ਊ"] = "uː", ["ੁ"] = "ʊ", ["ੂ"] = "uː",
 
    ["ਹ"] = "(ɦ)", ["'"] = "(ʔ)",
}

local perso_arabic = {
	["x"] = "kh", ["ġ"] = "g", ["q"] = "k", ["ž"] = "z", ["z"] = "j", ["f"] = "ph", ["'"] = "", 
}

local lengthen = {
	["a"] = "ā", ["i"] = "ī", ["u"] = "ū",
}

local vowels = "aāiīuūoʊɪɔɔ̃ɛeãā̃ẽĩī̃õũū̃ː"
local vowel = "[aāiīuūoʊɪɔɔ̃ɛeãā̃ẽĩī̃õũū̃]ː?"
local weak_h = "([gjdḍbṛnm])h"
local aspirate = "([kctṭp])"
local syllabify_pattern = "([" .. vowels .. "]̃?)([^" .. vowels .. "%.%-]+)([" .. vowels .. "]̃?)"

local function find_consonants(text)
	local current = ""
	local cons = {}
	for cc in mw.ustring.gcodepoint(text .. " ") do
		local ch = mw.ustring.char(cc)
		if find(current .. ch, "^[kgṅcjñṭḍṇtdnpbmyrlvśṣshqxġzžḻṛṟfθṉḥ]$") or find(current .. ch, "^[kgcjṭḍṇtdpbṛ]h$") then
			current = current .. ch
		else
			table.insert(cons, current)
			current = ch
		end
	end
	return cons
end

local function syllabify(text)
	for count = 1, 2 do
		text = gsub(text, syllabify_pattern, function(a, b, c)
			b_set = find_consonants(b)
			table.insert(b_set, #b_set > 1 and 2 or 1, ".")
			return a .. table.concat(b_set) .. c
			end)
		text = gsub(text, "(" .. vowel .. ")(?=" .. vowel .. ")", "%1.")
	end
	for count = 1, 2 do
		text = gsub(text, "(" .. vowel .. ")(" .. vowel .. ")", "%1.%2")
	end
	return text
end

local identical = "knlsfzθ"
for character in gmatch(identical, ".") do
	correspondences[character] = character
end

local function transliterate(text)
	return lang:transliterate(text)
end

function export.link(term)
	return require("Module:links").full_link{ term = term, lang = lang, sc = sc }
end

function export.narrow_IPA(ipa)
	-- what /ɑ/ really is
	ipa = gsub(ipa, 'ɑ', 'ä')
	-- dentals
	ipa = gsub(ipa, '([snl])', '%1̪')
	-- nasals induce nasalization
	ipa = gsub(ipa, '([əäɪiʊueɛoɔæ])(ː?)([nɳŋm])', '%1̃%2%3')
	-- cc, jj
	ipa = gsub(ipa, 't͡ʃ(%.?)t͡ʃ', 't̚%1t͡ʃ')
	ipa = gsub(ipa, 'd͡ʒ(%.?)d͡ʒ', 'd̚%1d͡ʒ')
	-- syllable boundary consonants
	ipa = gsub(ipa, '([kg])%.([kg])', '%1̚.%2')
	ipa = gsub(ipa, '([ʈɖ])%.([ʈɖ])', '%1̚.%2')
	ipa = gsub(ipa, '([td]̪?)%.([tdn])', '%1̚.%2')
	ipa = gsub(ipa, '([pb])%.([pb])', '%1̚.%2')
	-- aspiration rules
	ipa = gsub(ipa, 'əɦ%.([kgŋtdɲʈɖɳnpbmɾlzqfʂʃsʒɭɣɹʋj])', 'ɛɦ.%1')
	ipa = gsub(ipa, 'ʊɦ%.([kgŋtdɲʈɖɳnpbmɾlzqfʂʃsʒɭɣɹʋj])', 'ɔɦ.%1')
	ipa = gsub(ipa, 'ə%.ɦə', 'ɛ.ɦɛ')
	ipa = gsub(ipa, 'ʊ%.ɦə', 'ɔ.ɦɔ')
	ipa = gsub(ipa, 'ə%.ɦʊ', 'ɔ.ɦɔ')
	-- v/w
	ipa = gsub(ipa, '([kgŋtdɲʈɖɳnpbm]̪?%.?)ʋ', '%1w')
	-- retroflex s rules
	ipa = gsub(ipa, 'ʂ(%.?[^ʈɖ])', 'ʃ%1')
        ipa = gsub(ipa, '([ŋn])%.([q])', 'ɴ.%2')
	ipa = gsub(ipa, 'ʂ$', 'ʃ')
	ipa = gsub(ipa, "ɪ%.j", "i.j")
	return ipa
end

function export.toIPA(text, style)
	text = gsub(text, '॰', '-')
	local translit = transliterate(text)
	if not translit then
		error('The term "' .. Punjabi .. '" could not be transliterated.')
	end
	
	if style == "nonpersianized" then
		translit = gsub(translit, "[xġqžzf']", perso_arabic)
	end
	
	-- force final schwa
	translit = gsub(translit, "a~$", "ə")
	
	-- vowels
	translit = gsub(translit, "͠", "̃")
	translit = gsub(translit, 'a(̃?)i', 'ɛ%1ː')
	translit = gsub(translit, 'a(̃?)u', 'ɔ%1ː')
	translit = gsub(translit, "%-$", "")
	translit = gsub(translit, "^%-", "")
	translit = gsub(translit, "ŕ$", "r")
	translit = gsub(translit, "ŕ", "ri")
	translit = gsub(translit, ",", "")
	translit = gsub(translit, " ", "..")
	translit = syllabify(translit)
	translit = gsub(translit, "%.ː", "ː.")
	translit = gsub(translit, "%.̃", "̃")
	
	-- gy
	translit = gsub(translit, 'jñ', 'gy')

	translit = gsub(translit, aspirate .. "h", '%1ʰ')
	translit = gsub(translit, weak_h, '%1ʱ')
	
	local result = gsub(translit, ".", correspondences)

	-- remove final schwa (Pandey, 2014)
	-- actually weaken
	result = gsub(result, "(...)ə$", "%1ᵊ")
	result = gsub(result, "(...)ə ", "%1ᵊ ")
	result = gsub(result, "(...)ə%.?%-", "%1ᵊ-")
	result = gsub(result, "%.?%-", ".")

	result = gsub(result, "%.%.", "‿")
	
	-- formatting
	result = gsub(result, "ː̃", "̃ː")
	result = gsub(result, "ː%.̃", "̃ː.")
	result = gsub(result, "%.$", "")
       -- ñ
        result = gsub(result, "ñ", "n")

	-- i and u lengthening
	result = gsub(result, "ʊ(̃?)(ʱ?)$", "u%1ː%2")
	result = gsub(result, "ɪ(̃?)(ʱ?)$", "i%1ː%2")
	
	result = gsub(result, "ɛː(%.?)j", function(a)
		local res = "ə̯i"
		res = res .. a .. "j"
		return res
	end)
	result = gsub(result, "ɔː(%.?)ʋ", function(a)
		local res = "ə̯u"
		res = res .. a .. "ʋ"
		return res
	end)
	
	return result
end

function export.make(frame)
	local args = frame:getParent().args
	local pagetitle = mw.title.getCurrentTitle().text
	
	local p, results = {}, {}, {}
	
	if args[1] then
		for index, item in ipairs(args) do
			table.insert(p, (item ~= "") and item or nil)
		end
	else
		p = { pagetitle }
	end
	
	for _, Punjabi in ipairs(p) do
		local nonpersianized = export.toIPA(Punjabi, "nonpersianized")
		local persianized = export.toIPA(Punjabi, "persianized")
		table.insert(results, { pron = "/" .. nonpersianized .. "/" })
		local narrow = export.narrow_IPA(nonpersianized)
		if narrow ~= nonpersianized then table.insert(results, { pron = "[" .. narrow .. "]" }) end
		if nonpersianized ~= persianized then
			table.insert(results, { pron = "/" .. persianized .. "/" })
			local narrow = export.narrow_IPA(persianized)
			if narrow ~= persianized then table.insert(results, { pron = "[" .. narrow .. "]" }) end
		end
	end
	
	return m_a.format_qualifiers(lang, {"Standard Punjabi"}) .. " " .. m_IPA.format_IPA_full { lang = lang, items = results }
end

return export