gpt4 book ai didi

utf-8 - 计算混合语言文档中的单词

转载 作者:行者123 更新时间:2023-12-04 04:33:15 25 4
gpt4 key购买 nike

给定一组包含汉字、基于拉丁字母的单词或两者混合的行,我想获得字数。

即:

this is just an example
这只是个例子

理想情况下应该给出10个单词;但是当然,如​​果没有字典,例子最好被视为两个独立的字符。因此,11 个单词/字符的计数在这里也是可以接受的结果。

显然, wc -w不会工作。它将6个汉字/5个单词视为1个“单词”,一共返回6个。

我该如何进行?我愿意尝试不同的语言,尽管现在 bash 和 python 对我来说是最快的。

最佳答案

您应该在 Unicode word boundaries 上拆分文本,然后计算包含字母或表意文字的元素。如果你使用 Python,你可以使用 unisegnltk包,例如。另一种方法是简单地使用 Unicode-aware regexes但这些只会在 simple word boundaries 上中断.另见问题Split unicode string on word boundaries .

请注意,对于某些语言,您将需要更复杂的基于字典的解决方案。 UAX #29 状态:

For Thai, Lao, Khmer, Myanmar, and other scripts that do not typically use spaces between words, a good implementation should not depend on the default word boundary specification. It should use a more sophisticated mechanism, as is also required for line breaking. Ideographic scripts such as Japanese and Chinese are even more complex. Where Hangul text is written without spaces, the same applies. However, in the absence of a more sophisticated mechanism, the rules specified in this annex supply a well-defined default.

关于utf-8 - 计算混合语言文档中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20215196/

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