>> import locale >>> locale.setlocale(locale.LC_ALL, "f-6ren">
gpt4 book ai didi

python - 为什么 locale.strxfrm ("Gè") locale.strxfrm ("Gène")) 的前缀不是 locale "fr_FR.UTF-8"?

转载 作者:行者123 更新时间:2023-12-03 15:14:09 29 4
gpt4 key购买 nike

这里的代码在 Python 中,但在使用语言环境的 C/C++ 中的行为应该是相同的。

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
>>> locale.strxfrm("Gène").startswith(locale.strxfrm("Gè"))
False

我知道它不应该那样使用,但我想知道发生了什么......

上下文:
我有一个 strxfrm 转换的字符串数组和一个普通的输入文本。我想知道哪些 strxfrm 转换的字符串在转换之前以文本开头。它完全可行吗?如何 ?

奖金问题:

我们能得到每个地区的等效字母列表吗?我们可以检查等效字符串吗?

我的意思是:
"de_DE.UTF8" ,我能得到类似的东西吗
locale.strxfrm("Wissen").startswith(locale.strxfrm("Wiß")) 

返回真?

由于 "ß" and "ss" are equivalent在排序中(除非它是唯一的区别):
> locale.strxfrm("Wiessen") < locale.strxfrm("Wießen") < locale.strxfrm("Wiessen0")
True

法语中的“œ”和“oe”也是如此。

编辑 : 关于奖金,我看到了 Python locale-aware string comparison但答案依赖于 3rd 方库,所以我提出了一个解决方法 hacked 函数:
def isEquivalent(str1, str2):
return ( locale.strxfrm(str2[:-1]) < locale.strxfrm(str1) <= locale.strxfrm(str2) < locale.strxfrm(str1+"0")
or
locale.strxfrm(str1[:-1]) < locale.strxfrm(str2) <= locale.strxfrm(str1) < locale.strxfrm(str2+"0") )

最佳答案

一个非常有趣的问题!
这个答案不规范,我认为glibc-dev将是最好的论坛。

TL;博士
strxfrm 的唯一要求这是:

strcmp(strxfrm(a), strxfrm(b)) == strcoll(a, b)

什么 strxfrm允许是将事物的相对顺序导出到另一个(dumber)系统,例如,在数据库表中维护二级索引。

让我们测试一下

让我们来看看 Python3 (Python3.9,OSX,组合范式):

>>> locale.strxfrm(unicodedata.normalize("NFC", "Gène"))
'Jëqh\x01Jëqh'
>>> locale.strxfrm(unicodedata.normalize("NFC", "Gè"))
'Jë\x01Jë'

如果您要通过 <SOH> 中断输出字节,你实际上会得到一个有效的子字符串。

我不知道输出在分隔符两侧基本上重复的意义。 🤔

python 3 NFD 似乎遵循相同的语义,但输出不同,我猜这只是强调了规范化文本的重要性😼

>>> locale.strxfrm(unicodedata.normalize("NFD", "Gène"))
'Jhăqh\x01JhЃqh'
>>> locale.strxfrm(unicodedata.normalize("NFD", "Gè"))
'Jhă\x01JhЃ'

其他脚本 有更时髦的输出,这里是日语语言环境中的日语:

>>> locale.strxfrm(unicodedata.normalize("NFC", "村上  春樹"))
'ăă#ăă\x01桔伍#木欼'
>>> locale.strxfrm(unicodedata.normalize("NFC", "村上春樹"))
'ăăăă\x01桔伍木欼'
>>> locale.strxfrm(unicodedata.normalize("NFC", "村上"))
'ăă\x01桔伍'
>>> 'ăăăă\x01桔伍木欼' > 'ăă#ăă\x01桔伍#木欼' > 'ăă\x01桔伍'
True

Python2 具有不同的格式,其中内容也重复,但不清楚如何检测分隔符。所以,我们不要使用 Python 2,它已经 EOL 😅

>>> locale.strxfrm(unicodedata.normalize("NFC", u"Gène").encode("utf-8"))
'0019003Z001`001W00000019003Z001`001W'
>>> locale.strxfrm(unicodedata.normalize("NFC", u"Gè").encode("utf-8"))
'0019003Z00000019003Z'

JavaScript Intl模块,通过 new Intl.Collator(...).compare() 提供整理(排序)但据我所知,并没有公开 strxfrm 的等价物.我想知道这是否存在一些根本性的困难。我希望这样的功能可以用来构建,例如自定义 IndexedDB 索引,但是唉! 🤷‍♂️

关于python - 为什么 locale.strxfrm ("Gè") locale.strxfrm ("Gène")) 的前缀不是 locale "fr_FR.UTF-8"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27916045/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com