Məzmuna keç

Modul:palindromes

Wiktionary saytından

Bu modulun sənədləşdirmə səhifəsi Modul:palindromes/doc səhifəsində yaradıla bilər

local u = mw.ustring.char

local export = {}

local data = {
	["ca"] = {
		from = {"à", "[èé]", "[íï]", "[òó]", "[úü]", "ç", "l·l"},
		to   = {"a", "e",    "i",    "o",    "u",    "c", "ll"},
	},
	["fr"] = {
		from = {"[áàâä]", "[éèêë]", "[íìîï]", "[óòôö]", "[úùûü]", "[ýỳŷÿ]", "ç", "æ",  "œ", "'"},
		to   = {"a",       "e",     "i",      "o",      "u",      "y",      "c", "ae", "oe"},
	},
	["grc"] = {
		from = {
			"[ᾳάᾴὰᾲᾶᾷἀᾀἄᾄἂᾂἆᾆἁᾁἅᾅἃᾃἇᾇᾱᾰἈᾈἌᾌἊᾊἎᾎἉᾉἍᾍἋᾋἏᾏᾹᾸ]",
			"[έὲἐἔἒἑἕἓἘἜἚἙἝἛ]",
			"[ῃήῄὴῂῆῇἠᾐἤᾔἢᾒἦᾖἡᾑἥᾕἣᾓἧᾗἨᾘἬᾜἪᾚἮᾞἩᾙἭᾝἫᾛἯᾟ]",
			"[ίὶῖἰἴἲἶἱἵἳἷϊΐῒῗῑῐἸἼἺἾἹἽἻἿῙῘ]", 
			"[όὸὀὄὂὁὅὃὈὌὊὉὍὋ]",
			"[ύὺῦὐὔὒὖὑὕὓὗϋΰῢῧῡῠὙὝὛὟῩῨ]",
			"[ῳώῴὼῲῶῷὠᾠὤᾤὢᾢὦᾦὡᾡὥᾥὣᾣὧᾧὨᾨὬᾬὪᾪὮᾮὩᾩὭᾭὫᾫὯᾯ]",
			"[ῥῤῬ]",
			"[ς]",
			"[́͂]"
		},
		to = {
			"α",
			"ε",
			"η",
			"ι",
			"ο",
			"υ",
			"ω",
			"ρ",
			"σ"
		}
	},
	["he"] = {
		from = {
			"[ם]",
			"[ן]",
			"[ך]",
			"[ף]",
			"[ץ]",
			"[" .. u(0x0591) .. "-" .. u(0x05BD) .. u(0x05BF) .. "-" .. u(0x05C5) .. u(0x05C7) .. "]"
		},
		to = {
			"מ",
			"נ",
			"כ", 
			"פ",
			"צ"
		}
	},
}

local function ignoreCharacters(term, lang)
	term = mw.ustring.lower(term)
	term = mw.ustring.gsub(term, "[ ,%.%?!%%%-'\"]", "")
	
	if data[lang:getCode()] then
		for i, from in ipairs(data[lang:getCode()].from) do
			term = mw.ustring.gsub(term, from, data[lang:getCode()].to[i] or "")
		end
	end
	
	return term
end

function export.is_palindrome(term, lang)
	-- Affixes aren't palindromes
	if mw.ustring.find(term, "^%-") or mw.ustring.find(term, "%-$") then
		return false
	end
	
	-- Remove punctuation and casing
	term = ignoreCharacters(term, lang)
	
	-- Ignore terms consisting of less than 3 characters
	local n = mw.ustring.len(term)
	
	if (n <= 2) then
		return false
	end
	
	-- Ignore terms that consist of just one character repeated
	if term == mw.ustring.rep(mw.ustring.sub(term, 1, 1), n) then
		return false
	end
	
	for i = 1, math.floor(n / 2) do
        if mw.ustring.match(term, ".", i) ~= mw.ustring.match(term, ".", -i) then
        	return false
        end
    end
    
    return true
end

return export