This module will sort Coptic language text. It is also used to sort Old Nubian.
The module should preferably not be called directly from templates or other modules.
To use it from a template, use {{sortkey}}
.
Within a module, use Module:languages#Language:makeSortKey.
For testcases, see Module:cop-sortkey/testcases.
makeSortKey(text, lang, sc)
text
written in the script specified by the code sc
, and language specified by the code lang
.nil
.ΑΛΚ1Α
)ΑΛΚ1Ο
)ΑΛΚ2Ε
)ΑΛΚ2ΙΙ
)ΑΛΚ2Υ
)ΥΡ1
)ΥΡ1Η
)ΥΡ1Ω
)ΥΡ2ΙΕ
)ΥΡ2ΟΕ
)ΥΡ2Ω
)ΑΛ2Υ
)ΑΛ2Ω
)ΑΛΤΣ1ΗΑ
)ΑΛΥ1Α
)ΑΛΥ1Η
)ΑΛΥ2ΙΙ
)ΑΛΥ2ΩΕ
)ΑΛϢΥ1Ω
)ΜΤ2ΑΥ
)ΜΥ1Α
)ΜΥ1ΑΑ
)ΜΥ1Ο
)ΜΥ2ΑΑΑ
)ΜΥ2ΕΕΕ
)ΜΥΣ2ΑΕ
)ΜΥΤ1Υ
)ΜΦ2Η
)ΤΛ1Α
)ΤΛ1Η
)ΤΛ2ΑΟ
)ΤΛ2ΙΙ
)ΤΛ2ΩΙΙ
)Κ2Ω
)Κ2Ω ΕΒΛ1Ο
)ϢΛΛ1Η
)export = {}
local match = mw.ustring.match
local function ugsub(text, regex, replacement)
local out = mw.ustring.gsub(text, regex, replacement)
return out
end
local alphabet = "ⲁⲃⲅⲇⲉⲍⲏⲑⲓⲕⲗⲙⲛⲝⲟⲡⲣⲥⲧⲩⲫⲭⲯⲱϣϥⳉϧϩϫϭw"
local vowels = "ⲁⲉⲏⲓⲟⲩⲱ"
local vowel = ""
local consonants = ugsub(alphabet, vowel, "")
local consonant = ""
local replacements = {
= "ⲩ",
= "ⲕⲉ",
= "ⲓ",
= "ⲧⲓ",
= "",
= "",
= "",
}
local CopticToGreek = {
= "α",
= "β",
= "γ",
= "δ",
= "ε",
= "ζ",
= "η",
= "θ",
= "ι",
= "κ",
= "λ",
= "μ",
= "ν",
= "ξ",
= "ο",
= "π",
= "ρ",
= "σ",
= "τ",
= "υ",
= "φ",
= "χ",
= "ψ",
= "ω",
}
function export.makeSortKey(text, lang, sc)
if not text then
return nil
elseif sc and sc ~= "Copt" then
return mw.ustring.upper(text)
end
local str_gsub = string.gsub
text = mw.ustring.lower(text)
for letter, replacement in pairs(replacements) do
text = str_gsub(text, letter, replacement)
end
local origText = text
text = ugsub(text, "ⲩ(" .. vowel .. ")", "w%1")
text = ugsub(text, "(" .. vowel .. ")ⲩ", "%1w")
-- mw.log(origText, text)
local sort = {}
for word in mw.ustring.gmatch(text, "%S+") do
-- Add initial vowel (if any).
table.insert(sort, match(word, "^" .. vowel) )
-- Add consonants (in order).
table.insert(sort, ugsub(word, vowel .. "+", ""))
--[[
Add the number "1" if word ends in consonant.
"1" sorts before Greek–Coptic and Coptic Unicode blocks.
]]
if mw.ustring.match(word, consonant .. "$") then
table.insert(sort, "1")
elseif mw.ustring.match(word, vowel .. "$") then
table.insert(sort, "2")
end
-- Get non-initial vowels (in order) by removing initial vowel and all consonants.
table.insert(sort, ugsub(ugsub(word, "^" .. vowel, ""), consonant, ""))
table.insert(sort, " ")
end
sort = table.concat(sort)
sort = str_gsub(sort, "w", "ⲩ")
--[[
Convert Greek-derived Coptic characters to Greek ones.
Otherwise, the uniquely Coptic letters would sort first, because
they were added to Unicode earlier.
ϣϥⳉϧϩϫϭ ⲁⲃⲅⲇⲉⲍⲏⲑⲓⲕⲗⲙⲛⲝⲟⲡⲣⲥⲧⲩⲫⲭⲯⲱ
⇓
αβγδεζηθικλμνξοπρστυφχψω ϣϥⳉϧϩϫϭ
]]
sort = str_gsub(sort, "+", CopticToGreek)
return mw.ustring.upper(sort)
end
local lang = require("Module:languages").getByCode("cop")
local sc = require("Module:scripts").getByCode("Copt")
local function tag(text)
return require("Module:script utilities").tag_text(text, lang, sc)
end
function export.showSorting(frame)
local terms = {}
for i, term in ipairs(frame.args) do
table.insert(terms, term)
end
local function comp(term1, term2)
return export.makeSortKey(term1) < export.makeSortKey(term2)
end
table.sort(terms, comp)
for i, term in pairs(terms) do
terms = "\n* " .. tag(term) .. " (<code>" .. export.makeSortKey(term) .. "</code>)"
end
return table.concat(terms)
end
return export