跳转到内容

模組:Ar-stripdiacritics

維基詞典,自由的多語言詞典

本模块會去除阿拉伯語的文字的變音符號。它也被用於去除塔吉克阿拉伯語、巴林阿拉伯語、美索不達米亞阿拉伯語、漢志阿拉伯語、阿曼阿拉伯語、多法爾阿拉伯語、突尼斯阿拉伯語、海灣阿拉伯語、南黎凡特阿拉伯語、北黎凡特阿拉伯語、蘇丹阿拉伯語、阿爾及利亞阿拉伯語、內志阿拉伯語、摩洛哥阿拉伯語、埃及阿拉伯語、烏茲別克阿拉伯語、利比亞阿拉伯語、也門阿拉伯語、北美索不達米亞阿拉伯語、內富沙語、蒙戈勒語、莫札拉布語、施盧赫語、森哈加語、早期古烏古斯語、古安納托利亞土耳其語、烏茲別克語、保加爾語、喀喇汗語、古盧森尼亞語的變音符號。 最好不要直接從模板或其他模块調用此模块。要從模板中使用它,請以 {{strip diacritics}} 做為替代;若要在模块中使用,則以 Module:languages#Language:stripDiacritics 替代。

關於測試用例,請參閱Module:Ar-stripdiacritics/testcases。

函數

stripDiacritics(text, lang, sc)
對以代碼 sc 指定的文字、代碼 lang 指定的語言所書寫的一段 text 去除變音符號。
當去除變音符號失敗時,回傳 nil。

local m_str_utils = require("Module:string utilities")

local find = m_str_utils.find
local gsub = m_str_utils.gsub
local U = m_str_utils.char

local taTwiil = U(0x640)
local waSla = U(0x671)
-- diacritics ordinarily removed by entry_name replacements
local Arabic_diacritics = U(0x64B, 0x64C, 0x64D, 0x64E, 0x64F, 0x650, 0x651, 0x652, 0x670)

-- replace alif waṣl with alif
-- remove tatweel and diacritics: fathatan, dammatan, kasratan, fatha,
-- damma, kasra, shadda, sukun, superscript (dagger) alef
local replacements = {
	from = {U(0x0671), "[" .. U(0x0640, 0x064B) .. "-" .. U(0x0652, 0x0670, 0x0656) .. "]"},
	to   = {U(0x0627)},
}

local export = {}

function export.stripDiacritics(text, lang, sc)
	if text == waSla or find(text, "^" .. taTwiil .. "?[" .. Arabic_diacritics .. "]" .. "$") then
		return text
	end
	
	for i, from in ipairs(replacements.from) do
		local to = replacements.to[i] or ""
		text = gsub(text, from, to)
	end
	
	return text
end

return export