Module:palindromes: difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
No edit summary |
A bit of moving things around. We want to remove irrelevant characters before we check for length, otherwise something like "a.a" would be considered a palindrome. |
||
Line 31: | Line 31: | ||
} |
} |
||
function ignoreCharacters(term, lang) |
local function ignoreCharacters(term, lang) |
||
⚫ | |||
if not lang then return term end |
|||
term = mw.ustring.gsub(term, "[ ,%.%?!%%%-'\"]", "") |
|||
if data[lang:getCode()] then |
if data[lang:getCode()] then |
||
Line 43: | Line 44: | ||
end |
end |
||
function export.is_palindrome(term, |
function export.is_palindrome(term, lang) |
||
⚫ | |||
⚫ | |||
⚫ | |||
-- Ignore terms consisting of less than 3 characters |
|||
local n = mw.ustring.len(term) |
local n = mw.ustring.len(term) |
||
Line 49: | Line 54: | ||
return false |
return false |
||
end |
end |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- Ignore terms that consist of just one character repeated |
-- Ignore terms that consist of just one character repeated |
Revision as of 00:06, 27 August 2016
- The following documentation is located at Module:palindromes/documentation. [edit]
- Useful links: subpage list • links • transclusions • testcases • sandbox
local u = mw.ustring.char
local export = {}
local data = {
["he"] = {
from = {
"[ם]",
"[ן]",
"[ך]",
"[ף]",
"[ץ]",
"[" .. u(0x0591) .. "-" .. u(0x05BD) .. u(0x05BF) .. "-" .. u(0x05C5) .. u(0x05C7) .. "]"
},
to = {
"מ",
"נ",
"כ",
"פ",
"צ"
}
},
["grc"] = {
from = {"[ᾳάᾴὰᾲᾶᾷἀᾀἄᾄἂᾂἆᾆἁᾁἅᾅἃᾃἇᾇᾱᾰ]", "[ἈᾈἌᾌἊᾊἎᾎἉᾉἍᾍἋᾋἏᾏᾹᾸ]", "[έὲἐἔἒἑἕἓ]",
"[ἘἜἚἙἝἛ]", "[ῃήῄὴῂῆῇἠᾐἤᾔἢᾒἦᾖἡᾑἥᾕἣᾓἧᾗ]", "[ἨᾘἬᾜἪᾚἮᾞἩᾙἭᾝἫᾛἯᾟ]",
"[ίὶῖἰἴἲἶἱἵἳἷϊΐῒῗῑῐ]", "[ἸἼἺἾἹἽἻἿῙῘ]", "[όὸὀὄὂὁὅὃ]", "[ὈὌὊὉὍὋ]",
"[ύὺῦὐὔὒὖὑὕὓὗϋΰῢῧῡῠ]", "[ὙὝὛὟῩῨ]", "[ῳώῴὼῲῶῷὠᾠὤᾤὢᾢὦᾦὡᾡὥᾥὣᾣὧᾧ]",
"[ὨᾨὬᾬὪᾪὮᾮὩᾩὭᾭὫᾫὯᾯ]", "[ῥῤ]", "[Ῥ]", "[ς]", "[́͂]"},
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)
-- 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
local reversed = {}
for c in mw.ustring.gmatch(term, ".") do
table.insert(reversed, 1, c)
end
reversed = table.concat(reversed)
if term == reversed then
return true
else
return false
end
end
return export