Module:fa-common

From Wiktionary, the free dictionary
Jump to navigation Jump to search
This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local export = {}

-- Return a ZWNJ character, if the word needs it
function export.ZWNJ(word)
	if mw.ustring.match(word, "[بپتثجچحخسشصضطظعغفقکگلمنهی]", -1) then
		return "\226\128\140" -- U+200C ZERO WIDTH NON-JOINER
	end
	return "" -- empty string
end

-- Add stress mark to transliteration
function export.stress(tr)
	if not mw.ustring.gmatch(tr, "[áéíóúấếố]") then
		tr = mw.ustring.gsub(tr,
			"([aeiouâêô])([^aeiouâêô]*)$",
			function (m, cons)
				return ({
					["a"] = "á", ["e"] = "é", ["i"] = "í", ["o"] = "ó",
					["u"] = "ú", ["â"] = "ấ", ["ê"] = "ế", ["ô"] = "ố"
				})[m] .. cons
			end)
	end
	
	return tr
end

function export.ending(word, tr)
	if mw.ustring.sub(word, -1) == "ا" then
		return "ā"
	elseif mw.ustring.sub(word, -1) == "ه" and (not tr or mw.ustring.sub(tr, -1) == "e" or mw.ustring.sub(tr, -1) == "a") then
		return "a"
	else
		return "c"
	end
end

function export.pl_ending(pl)
	if mw.ustring.sub(pl, -2) == "ها" then
		return "ها"
	elseif mw.ustring.sub(pl, -2) == "ان" then
		return "ان"
	elseif mw.ustring.sub(pl, -2) == "ات" then
		return "ات"
	else
		return nil -- it is a mokassar plural
	end
end

function export.pluralize(word, tr, pl_ending)
	local pl, pltr = word, tr
		
	if pl_ending == "ها" then
		pl = word .. export.ZWNJ(word) .. "ها"
		if tr then pltr = tr .. "-hâ" end
	elseif pl_ending == "ان" then
		if export.ending(word, tr) == "a" then
			pl = mw.ustring.gsub(word, "(.-)ه",
								 "%1گان")
			if tr then pltr = tr .. "gân" end
		else
			pl = word .. "ان"
			if tr then pltr = tr .. "ân" end
		end
	elseif pl_ending == "ات" then
		if export.ending(word, tr) == "a" then
			pl = mw.ustring.gsub(word, "(.-)ه",
								 "%1ات")
			if tr then pltr = mw.ustring.gsub(tr, "(.-)[ea]$", '%1ât') end
		else
			pl = word .. "ات"
			if tr then pltr = tr .. "ât" end
		end
	end

	return pl, pltr
end

function plurals(word, tr, pls)
	local plurals = {}
	
	for i, pl in ipairs(pls) do
		plurals[i] = {export.pluralize(word, tr, export.pl_ending(pl[1]))}
	end

	return plurals
end

return export