A modult a Modul:ne-IPA/doc lapon tudod dokumentálni

local export = {}

local lang = require("Module:languages").getByCode("ne")
local sc = require("Module:scripts").getByCode("Deva")
local m_IPA = require("Module:IPA")

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

local correspondences = {
	["ṅ"] = "ŋ", ["g"] = "ɡ", 
    ["c"] = "t͡s", ["j"] = "d͡z", ["ċ"] = "t͡s", ["j̈"] = "d͡z", ["ñ"] = "ɲ",
	["ṭ"] = "ʈ", ["ḍ"] = "ɖ", ["ṇ"] = "ɳ",
	["t"] = "t̪", ["d"] = "d̪",
	["y"] = "j", ["w"] = "w", ["v"] = "v", ["l"] = "l",
	["ś"] = "ɕ", ["ṣ"] = "ʂ", ["h"] = "ɦ",
	["ṛ"] = "ɽ", ["n"] = "n", ["ž"] = "ʒ", ["z"] = "z", ["ṟ"] = "ɹ", ["r̥"] = "ri", ["ṃ"] = "̃", ["ॽ"] = "", [","] = ",", ["r"] = "r", ["ỹ"] = "j̃",

	["a"] = "ʌ", ["ā"] = "a", ["i"] = "i",
	["ī"] = "i", ["o"] = "o", ["e"] = "e",
	["u"] = "u", ["ū"] = "u", ["ai"] = "ʌi̯", ["au"] = "ʌu̯", ["ŏ"] = "ɒ", ["ĕ"] = "æ", ["āu"] = "āu̯",  ["āi"] = "āu̯",

	["ũ"] = "ũ", ["õ"] = "õ", ["ã"] = "ʌ̃", ["ā̃"] = "ã", ["ẽ"] = "ẽ",

	["ḥ"] = "ʰ",  [" "] = " ",
}

local vowels = "aāiīuūeaioauŏĕ"
local weak_h_c = "gjdḍd̪ṇbṛrṟwy"
local weak_h = "([" .. weak_h_c .. "])h"
local aspirate = "([kctṭt̪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)
	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.toIPA(text, phonetic)
	local translit = transliterate(text)
	if not translit then
		error('The term "' .. Nepali .. '" could not be transliterated.')
	end

	if phonetic then
		translit = gsub(translit, 'ā([iuĩũīū])', 'ä%1̯') -- syllabicity
		translit = gsub(translit, 'ā', 'ä') -- narrower transcription of a
	        translit = gsub(translit, 'ś', 's') -- s usually 
               translit = gsub(translit, '([aäāiīuūeaioauŏĕ])([n])([td])', '%1%2̪%3') -- dental assimilation
               translit = gsub(translit, '([aäāiīuūeaioauŏĕ])ṇ([ṭḍ])', '%1ɳ%2') -- retroflex assimilation
               translit = gsub(translit, '([aäāiīuūeaioauŏĕ])n([ṭḍ])', '%1ɳ%2') -- retroflex assimilation 2nd
               translit = gsub(translit, 'ṇ', 'ɽ̃')
	        translit = gsub(translit, 'ॽ', 'ʔ') -- ɡlottal stop
	        translit = gsub(translit, 'ṣ', 's') -- s usually
                translit = gsub(translit, 'hr̥', 'ʱɾi') -- hr̥ ligature initially
                translit = gsub(translit, '([aäāiīuūr̥])ch̥', '%1k̚t͡sʰ') -- kṣ ligature
               translit = gsub(translit, '()ch̥', 't͡sʰ') -- kṣ initially
	        translit = gsub(translit, 'ñ', 'n') -- ñ usually
		translit = gsub(translit, 'w([iīewoyr̥])', 'b%1') -- w to b before certain vowels and liquids
							translit = gsub(translit, '([a])h([iīĩī̃])', '%1%2') -- h dropping
									translit = gsub(translit, '([a])h([āä])', '%2̆ɦ%2̤') -- breathy and vowel changes
											translit = gsub(translit, '([äe])h([iīuūĩũī̃ū̃])', '%1%2') -- h dropping 2nd case
													translit = gsub(translit, '([a])h([ae])', '%1ʔ%2') -- glottal stop
															translit = gsub(translit, '([äā])h([a])', '%1ɦ%1̆') -- vowel change 2nd
																	translit = gsub(translit, '([iī])h([äāe])', '(%1)y%2') -- vowel change 3rd
               translit = gsub(translit, '([aāäiīuū])ya', '%1e̯') -- ya to e before vowel
                translit = gsub(translit, '([e])ya', '%1yə') -- ya remains same
               translit = gsub(translit, '([aāiīuūeoä])([gbtṭdmhṇnśṣslkpḍ])wa', '%1%2ːo') -- gemination of preceding consonant
               translit = gsub(translit, '()([gbtṭdmhṇnśṣslkpḍ])wa', '%2o')
               translit = gsub(translit, '([raäāiīuūeo])([r])w', '%1rb') -- rw to rb
               translit = gsub(translit, '([aāiīuūeoä])([grbtṭdmhnṛṛ̃ṅslkpḍ])ya', '%1%2ːe') -- gemination of preceding consonant
               translit = gsub(translit, '()([grbtṭdmhnṛṛ̃ṅslkpḍ])ya', '%2e') -- no gemination
                translit = gsub(translit, '()ya', 'e') -- ya to e initially
                translit = gsub(translit, 'wai', 'bʌi̯') -- wai usually
               translit = gsub(translit, 'w$', 'b') -- final w to b
               translit = gsub(translit, '(...)w ', '%1b ') -- final w to b 2nd
               translit = gsub(translit, '([ʌäaāiīuūeo ̤])ḍ', '%1ṛ') -- postvocalic allophone of ḍ
               translit = gsub(translit, '([ʌäaāiīuūeo ̤])ḍh', '%1ɽ') -- postvocalic allophone of ḍh
		translit = gsub(translit, '([gdbḍ])h([ʌäaāiīuūeeoo])', '%1ʱ%2̤') -- breathy voice
	-- force final schwa
	translit = gsub(translit, "a~$", "ʌ")
               translit = gsub(translit, 'a$', 'ə') -- final schwa retention
               translit = gsub(translit, '(...)a ', '%1ə ') -- final schwa retenti
		translit = gsub(translit, '([ʌäaāiīuūəeeo ̤])r([ʌäaāiīəuūeeo ̤])', '%1ɾ%2') -- intervocalic allophone of r

	end
	local result = gsub(translit, ".", correspondences)
		
	
	-- vowels
	translit = gsub(translit, "͠", "̃")
	translit = gsub(translit, "pp", "pː")
	translit = gsub(translit, "om̐", "oːm")
	translit = gsub(translit, "ch̥", "kʂ")
	translit = gsub(translit, "tt", "tː")
	translit = gsub(translit, "kk", "kː")
	translit = gsub(translit, "bb", "bː")
	translit = gsub(translit, "ɡɡ", "ɡː")
	translit = gsub(translit, "ḍḍ", "ɖː")

	translit = gsub(translit, 'th(̃?)th', 'tʰː%1')
	translit = gsub(translit, 'dh(̃?)dh', 'dʱː%1')
	translit = gsub(translit, 'ḍh(̃?)ḍh', 'ɖʱː%1')
	translit = gsub(translit, 'ph(̃?)ph', 'pʰː%1')
	translit = gsub(translit, 'bh(̃?)bh', 'bʱː%1')
	translit = gsub(translit, "mm", "mː")
	translit = gsub(translit, "ss", "sː")
	translit = gsub(translit, "nn", "nː") 
        translit = gsub(translit, 'ṭh(̃?)ṭh', 'ʈʰː%1')
	translit = gsub(translit, "rr", "rː")
	translit = gsub(translit, "ll", "lː")
        translit = gsub(translit, '([' .. vowels .. '])([iīuū])', '%1%2̯') -- non syllabicity
	translit = gsub(translit, "%-", ".")
	translit = gsub(translit, "r̥", "ri")


	translit = syllabify(translit)
	
	-- clusters
	translit = gsub(translit, 'ndny', 'ndñ')
	translit = gsub(translit, 'dny', 'dñ')
	if phonetic then translit = gsub(translit, 'ts', 't͡sʰ') end
	
	-- aspiration rules
	translit = gsub(translit, aspirate .. "h", '%1ʰ')
	translit = gsub(translit, '([āauūiīeä])h([mnlpbgṭtdḍk])', '%1ʱ%2')
	translit = gsub(translit, weak_h, '%1ʱ')
	translit = gsub(translit, '([' .. weak_h_c .. '])%.h', '.%1ʱ')
	translit = gsub(translit, aspirate .. '%.h', '.%1ʰ')
        translit = gsub(translit, "%.ː", "ː.")

	local result = gsub(translit, ".", correspondences)
	
	-- formatting
	result = gsub(result, "ː̃", "̃ː")
	result = gsub(result, "ː.̃", "̃ː.")
	result = gsub(result, "%. ", " ")
    result = gsub(result, "%.$", " ")

	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 _, Nepali in ipairs(p) do
		table.insert(results, { pron = "/" .. export.toIPA(Nepali) .. "/" })
		if export.toIPA(Nepali, true) ~= export.toIPA(Nepali) then
			table.insert(results, { pron = "[" .. export.toIPA(Nepali, true) .. "]" })
		end
	end
	
	return m_IPA.format_IPA_full(lang, results)
end

return export