sorting - 按字母顺序对包含 UTF-8 编码值的表进行排序

标签 sorting utf-8 lua locale alphabetical

我将字典条目存储在 Lua 表中,并将其用作数组。我想对 Lua 中的条目进行排序,这样我就可以添加新的条目,而不必自己移动到正确的位置(这很快就会变得非常乏味)。但是,我面临几个问题:

  1. 许多单词包含非 ASCII 字符,这使得字符串的内置比较运算符不适合该任务(例如,它使 amputar 出现在 ámbito 之前) .
  2. 有来自不同语言的单词(不过都是西方语言),即西类牙语、德语和英语。这里的问题是,不同的语言可能对字母顺序有不同的概念。由于主要语言是西类牙语,我想使用它的规则,尽管我不确定这是否适用于西类牙语字母表中未包含的字符。
  3. 有些单词包含大写字母,或者更糟糕的是,以大写字母开头。例如,所有德语名词都以大写字母开头。通过内置的比较运算符,大写字母出现在小写字母之前,这不是我想要的行为;我希望大写字母与小写字母完全一样。

以下表为例:

local entries =
{
    'amputar',
    'Volksgeist',
    'ámbito'
}

这些条目应按如下顺序排序:

ámbito
amputar
Volksgeist

但是,使用我当前的代码,输出是错误的:

local function compare_utf8_strings( o1 , o2 )
    -- Using the built-in non-UTF-8-aware non-locale-aware string comparison operator
    return o1 < o2
end

table.sort( entries , function ( a , b ) return compare_utf8_strings( a , b ) end )

for i, entry in ipairs(entries) do
    print( entry )
end

输出:

Volksgeist
amputar
ámbito

您能否使用以下代码并对其进行修改以满足我的要求?

local entries =
{
    'amputar',
    'Volksgeist',
    'ámbito'
}

local function compare_utf8_strings( o1 , o2 )
    -- Hack here, please, accomplishing my requirements
end

table.sort( entries , function ( a , b ) return compare_utf8_strings( a , b ) end )

for i, entry in ipairs(entries) do
    print( entry )
end

它应该输出:

ámbito
amputar
Volksgeist

作为附加要求,此 Lua 代码全部位于 LuaTeX 内部,目前支持该语言的 5.2 版本。至于外部库,我想可以使用它们。

我是Lua阵营的新手,所以,请原谅我所犯的任何错误,并随时通知它,以便我修复它。

最佳答案

经过一段时间的搜索没有结果,我找到了this article作者:约瑟夫·赖特。虽然它触及了我的问题,但它没有提供明确的解决方案。我问了他,结果发现目前没有直接的方法可以做到我想要的。不过,他指出,slnunicode 是 LuaTeX 内置的(尽管将来会被替换)。

我使用 LuaTeX 环境中提供的工具开发了一个“粗略”的解决方案。它并不优雅,但它可以工作,并且不会引入任何外部依赖项。关于其效率,我没有发现文档构建时间有任何差异。

-- Make the facilities available
unicode = require( 'unicode' )
utf8 = unicode.utf8

--[[
    Each character's position in this array-like table determines its 'priority'.
    Several characters in the same slot have the same 'priority'.
]]
local alphabet =
{
    -- The space is here because of other requirements of my project
    { ' ' },
    { 'a', 'á', 'à', 'ä' },
    { 'b' },
    { 'c' },
    { 'd' },
    { 'e', 'é', 'è', 'ë' },
    { 'f' },
    { 'g' },
    { 'h' },
    { 'i', 'í', 'ì', 'ï' },
    { 'j' },
    { 'k' },
    { 'l' },
    { 'm' },
    { 'n' },
    { 'ñ' },
    { 'o', 'ó', 'ò', 'ö' },
    { 'p' },
    { 'q' },
    { 'r' },
    { 's' },
    { 't' },
    { 'u', 'ú', 'ù', 'ü' },
    { 'v' },
    { 'w' },
    { 'x' },
    { 'y' },
    { 'z' }
}

-- Looks up the character `character´ in the alphabet and returns its 'priority'
local function get_pos_in_alphabet( character )
    for i, alphabet_entry in ipairs(alphabet) do
        for _, alphabet_char in ipairs(alphabet_entry) do
            if character == alphabet_char then
                return i
            end
        end
    end

    --[[
        If it isn't in the alphabet, abort: it's better than silently outputting some
        random garbage, and, thanks to the message, allows to add the character to
        the table.
    ]]
    assert( false , "'" .. character .. "' was not in alphabet" )
end

-- Returns the characters in the UTF-8-encoded string `s´ in an array-like table
local function get_utf8_string_characters( s )
    --[[
        I saw this variable being used in several code snippets around the Web, but
        it isn't provided in my LuaTeX environment; I use this form of initialization
        to be safe if it's defined in the future.
    ]]
    utf8.charpattern = utf8.charpattern or "([%z\1-\127\194-\244][\128-\191]*)"

    local characters = {}

    for character in s:gmatch(utf8.charpattern) do
        table.insert( characters , character )
    end

    return characters
end

local function compare_utf8_strings( _o1 , _o2 )
    --[[
        `o1_chars´ and `o2_chars´ are array-like tables containing all of the
        characters of each string, which are all made lower-case using the
        slnunicode facilities that come built-in with LuaTeX.
    ]]
    local o1_chars = get_utf8_string_characters( utf8.lower(_o1) )
    local o2_chars = get_utf8_string_characters( utf8.lower(_o2) )

    local o1_len = utf8.len(o1)
    local o2_len = utf8.len(o2)

    for i = 1, math.min( o1_len , o2_len ) do
        o1_pos = get_pos_in_alphabet( o1_chars[i] )
        o2_pos = get_pos_in_alphabet( o2_chars[i] )

        if o1_pos > o2_pos then
            return false
        elseif o1_pos < o2_pos then
            return true
        end
    end

    return o1_len < o2_len
end

我无法将此解决方案集成到问题的框架中,因为我的测试环境 ZeroBrane Studio Lua IDE 没有附带 slnunicode 并且我不知道如何添加它。

就是这样。如果有人有任何疑问或需要进一步解释,请使用评论。我希望它对其他人有用。

关于sorting - 按字母顺序对包含 UTF-8 编码值的表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29167886/

相关文章:

python - 定长数据域和变长utf-8编码

php - 使用php将阿拉伯语文本存储在mysql数据库中

sorting - table.sort 使用什么算法?

python-2.7 - 如何从 python 加载和使用 torch 深度学习模型?

c++ - 基于交换的排序算法的交换次数的奇偶性

algorithm - 将列表分层为无序分区

python - 按元素有效地分组数组

python - 如何从 stdin 读取输入并强制执行编码?

function - 获取函数的 AST

algorithm - 对多边形的点进行排序以进行绘制