模組:Ethi-sortkey
外观
本模块會對以吉茲字母書寫的文字生成排序鍵。
最好不要直接從模板或其他模块調用此模块。要從模板中使用它,請以 {{sortkey}} 做為替代;若要在模块中使用,則以 Module:languages#Language:makeSortKey 替代。
關於測試用例,請參閱Module:Ethi-sortkey/testcases。
函數
makeSortKey(text, lang, sc)- 對以代碼
sc指定的文字、代碼lang指定的語言所書寫的一段text生成排序鍵。 - 當生成排序鍵失敗時,回傳
nil。
local export = {}
local m_str_utils = require("Module:string utilities")
local codepoint = m_str_utils.codepoint
local rsplit = m_str_utils.split
local rsub = m_str_utils.gsub
local u = m_str_utils.char
local floor = math.floor
local toSet = require("Module:table").listToSet
-- apply rsub() repeatedly until no change
local function rsub_repeatedly(term, foo, bar)
while true do
local new_term = rsub(term, foo, bar)
if new_term == term then
return term
end
term = new_term
end
end
local digits = "123456789ABCDEFGHIJK" --The digits correspond to ä, u, i, a, e, ə, o, ʷä, ʷi, ʷa, ʷe, ʷə, ʸä, ʸu, ʸi, ʸa, ʸe, ʸə, ʸo, ɔ
local spaces = "፠፡።፣፤፥፦፧፨" --to be converted into spaces
local zero = u(0x135D) .. u(0x135E) .. u(0x135F) .. "᎐᎑᎒᎓᎔᎕᎖᎗᎘᎙" -- to be removed altogether
--Rows taken by a single consonant
local use_16_intervals = {
{0x1240, 0x125F},
{0x1280, 0x128F},
}
--Consonants in two consecutive rows
local subst_8_intervals = {
{0x12B0, 0x12B7},
{0x12C0, 0x12C7},
{0x1310, 0x1317},
}
--Consonants in which the 8th order isn't Cʷa, but Cɔ. Where is this used?
local oa_in_8 = toSet({0x1200, 0x1240, 0x1280, 0x12A8, 0x12C8, 0x12E8, 0x1308, 0x1340})
local y1 = 13 --Order of ʸä
local w1 = 8 --Order of ʷä
--
local manual_order = {
--Supplement
[0x1380] = {0x1218, 8}, [0x1381] = {0x1218, 9}, [0x1382] = {0x1218, 11}, [0x1383] = {0x1218, 12},
[0x1384] = {0x1260, 8}, [0x1385] = {0x1260, 9}, [0x1386] = {0x1260, 11}, [0x1387] = {0x1260, 12},
[0x1388] = {0x1348, 8}, [0x1389] = {0x1348, 9}, [0x138A] = {0x1348, 11}, [0x138B] = {0x1348, 12},
[0x138C] = {0x1350, 8}, [0x138D] = {0x1350, 9}, [0x138E] = {0x1350, 11}, [0x138F] = {0x1350, 12},
--Extended
[0x2D80] = {0x1208, 20}, [0x2D81] = {0x1218, 20}, [0x2D82] = {0x1228, 20}, [0x2D83] = {0x1230, 20}, [0x2D84] = {0x1238, 20}, [0x2D85] = {0x1260, 20},
[0x2D86] = {0x1270, 20}, [0x2D87] = {0x1278, 20}, [0x2D88] = {0x1290, 20}, [0x2D89] = {0x1298, 20}, [0x2D8A] = {0x12A0, 20}, [0x2D8B] = {0x12D8, 20},
[0x2D8C] = {0x12F0, 20}, [0x2D8D] = {0x12F8, 20}, [0x2D8E] = {0x1300, 20}, [0x2D8F] = {0x1320, 20}, [0x2D90] = {0x1328, 20}, [0x2D91] = {0x1330, 20},
[0x2D92] = {0x1350, 20},
--Extended-B
--𞟭, 𞟮
[0x1E7ED] = {0x1218, w1 + 1}, [0x1E7EE] = {0x1218, w1 + 3},
--𞟰, 𞟱, 𞟲
[0x1E7F0] = {0x1240, w1 + 1}, [0x1E7F1] = {0x1240, w1 + 3}, [0x1E7F2] = {0x1240, w1 + 4},
--𞟳, 𞟴
[0x1E7F3] = {0x1260, w1 + 1}, [0x1E7F4] = {0x1260, w1 + 3},
--𞟵, 𞟶, 𞟷
[0x1E7F5] = {0x12A8, w1 + 1}, [0x1E7F6] = {0x12A8, w1 + 3}, [0x1E7F7] = {0x12A8, w1 + 4},
--𞟸, 𞟹, 𞟺
[0x1E7F8] = {0x1308, w1 + 1}, [0x1E7F9] = {0x1308, w1 + 3}, [0x1E7FA] = {0x1308, w1 + 4},
--𞟻, 𞟼
[0x1E7FB] = {0x1348, w1 + 1}, [0x1E7FC] = {0x1348, w1 + 3},
--𞟽, 𞟾
[0x1E7FD] = {0x1350, w1 + 1}, [0x1E7FE] = {0x1350, w1 + 3},
--Other (ፘ, ፙ, ፚ)
[0x1358] = {0x1228, y1}, [0x1359] = {0x1218, y1}, [0x135A] = {0x1348, y1}
}
---Start, end, base, offset
local order_tables = {
--Extended
{0x2DA0, 0x2DA6, 0x2DA0, - 0x2DA0 + 1},
{0x2DA8, 0x2DAE, 0x2DA8, - 0x2DA8 + 1},
{0x2DB0, 0x2DB6, 0x2DB0, - 0x2DB0 + 1},
{0x2DB8, 0x2DBE, 0x2DB8, - 0x2DB8 + 1},
{0x2DC0, 0x2DC6, 0x2DC0, - 0x2DC0 + 1},
{0x2DC8, 0x2DCE, 0x2DC8, - 0x2DC8 + 1},
{0x2DD0, 0x2DD6, 0x2DD0, - 0x2DD0 + 1},
{0x2DD8, 0x2DDE, 0x2DD8, - 0x2DD8 + 1},
--Extended-A
{0xAB01, 0xAB06, 0xAB01, - 0xAB01 + 2},
{0xAB09, 0xAB0E, 0xAB09, - 0xAB09 + 2},
{0xAB11, 0xAB16, 0xAB11, - 0xAB11 + 2},
{0xAB20, 0xAB26, 0xAB20, - 0xAB20 + 1},
{0xAB28, 0xAB2E, 0xAB28, - 0xAB28 + 1},
--Extended-B
{0x1E7E0, 0x1E7E7, 0x1210, - 0x1E7E0 + y1},
{0x1E7E8, 0x1E7EB, 0x1210, - 0x1E7E8 + w1}
}
--Convert ints to strings, but make 10->A, 11->B...
local function order_letter(o)
if o < 10 then
return tostring(o)
end
return u(55 + o) --"A" is at codepoint 65
end
--Find the codepoint of the base letter (c+ä) of a given character
local function get_base_order(c)
local base_cp, order
--In some cases it's easier to get the basic letter and the order manually
local result = manual_order[c]
if result then
return result[1], result[2]
end
--In some cases, related letters can be handled together
for _, t in ipairs(order_tables) do
if t[1] <= c and c <= t[2] then
return t[3], c + t[4]
end
end
--Single consonant takes a whole row
for _, pair in ipairs(use_16_intervals) do
if pair[1] <= c and c <= pair[2] then
base_cp = floor(c/16)*16
order = c - base_cp + 1
if order == 8 then --OA syllables
order = 20
elseif order == 9 then --move one slot to the left
order = 8
elseif order > 10 then --move two slots to the left (there's no order==10)
order = order - 2
end
return base_cp, order
end
end
--Single consonant in two rows (we only need to handle the second part)
for _, pair in ipairs(subst_8_intervals) do
if pair[1] <= c and c <= pair[2] then
base_cp = floor(c/8)*8 - 8
order = c - base_cp
if order == 1 then --move one slot to the left
order = 8
else --move two slots to the left
order = order - 2
end
return base_cp, order
end
end
--Single consonant in half a row
base_cp = floor(c/8)*8
order = c - base_cp + 1
--If it's Cɔ, 20
if order == 8 and oa_in_8[base_cp] then
order = 20
--If it's Cʷa, move to order 10 (make room for ʷä and ʷi if they exist)
elseif order == 8 then
order = 10
end
return base_cp, order
end
--Process each individual character
local function generate_sortkey(letter)
local cp = codepoint(letter)
--Ethiopic & Ethiopic Supplement, Ethiopic Extended, Ethiopic Extended-A, Ethiopic Extended-B
if not ((0x1200 <= cp and cp <= 0x1399) or (0x2D80 <= cp and cp <= 0x2DDE) or (0xAB01 <= cp and cp <= 0xAB2E) or (0x1E7E0 <= cp and cp <= 0x1E7FE)) then
return letter --If the character isn't in any of the Ethiopic blocks, don't do anything
end
--If it's a number, don't change anything
if 0x1369 <= cp and cp <= 0x137C then
return letter
end
local base_cp, order = get_base_order(cp)
return u(base_cp) .. order_letter(order)
end
function export.makeSortKey(text, lang, sc)
--Remove punctuation and dicritics (unlikely, but just in case)
text = rsub_repeatedly(text, "[" .. spaces .."]", " ")
text = rsub_repeatedly(text, " ", " ")
text = rsub_repeatedly(text, "[" .. zero .."]", "")
--Character by character
local result = {}
for _, l in ipairs(rsplit(text, "")) do
table.insert(result, generate_sortkey(l))
end
result = table.concat(result, "")
--Move vowel signs to the end
result = rsub_repeatedly(result, "([" .. digits .. "])([^" .. digits .. "])", "%2%1")
--Add a space before the digits (otherwise ቤተ will get sorted before ቤተ፡ክርስቲያን)
result = rsub(result, "([^" .. digits .. "])([" .. digits .. "])", "%1 %2")
return result
end
return export