Module:User:Theknightwho/lua-uca

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of Theknightwho, for their own experimentation. Items in this module may be added and removed at Theknightwho's discretion; do not rely on this module's stability.


local export = {}

function export.makeSortKey(text)
	local data = mw.loadData("Module:User:Theknightwho/lua-uca/lua-uca-ducet/Latn")
	local collator = require "Module:User:Theknightwho/lua-uca/lua-uca-collator"
	--local languages = require "Module:User:Theknightwho/lua-uca/lua-uca-languages"
	local collatorObj = collator.new(data[1])
	--collatorObj = languages.en(collatorObj)

	text = mw.ustring.toNFD(mw.ustring.upper(text))
	local char = mw.ustring.char
	local codepoints = data[2]
	local weightedSortkey = collatorObj:make_sort_key({mw.ustring.codepoint(text, 1, mw.ustring.len(text))}, 1)
	
	rawset(data, 1, nil)
	
	local sortkey = {}
	local j, tail
	for i, weight in ipairs(weightedSortkey) do
		if weight == 0 then tail = true end
		if codepoints[weight] and not tail then
			table.insert(sortkey, char(codepoints[weight]))
		elseif not tail then
			j = 0
			while not codepoints[weight-j] and weight-j > 0 do
				j = j + 1
				if codepoints[weight-j] then
					table.insert(sortkey, char(codepoints[weight-j]) .. char(0xF000+j-1))
				end
			end
		elseif weight > 0 then
			table.insert(sortkey, char(0xF000+weight))
		end
	end
	
	return table.concat(sortkey)
	--return weightedSortkey
end

function export.sort(terms)
	local data = mw.loadData("Module:User:Theknightwho/lua-uca/lua-uca-ducet/Latn")
	local collator = require "Module:User:Theknightwho/lua-uca/lua-uca-collator"
	--local languages = require "Module:User:Theknightwho/lua-uca/lua-uca-languages"
	local collatorObj = collator.new(data[1])
	--collatorObj = languages.en(collatorObj)
	
	table.sort(terms, function(a, b) 
		return collatorObj:compare_strings(a, b) 
	end)
	
	return terms
end

return export