python - Levenshtein Distance 是如何计算简体中文字符的?

标签 python string unicode levenshtein-distance edit-distance

我有两个问题:

    query1:你好世界
    query2:你好

当我使用 python 库 Levenshtein 运行此代码时:

from Levenshtein import distance, hamming, median
lev_edit_dist = distance(query1,query2)
print lev_edit_dist

我的输出是 12。 现在的问题是值 12 是如何得出的?

因为笔画之差,肯定不止12个。

最佳答案

根据其documentation , 它支持 unicode:

It supports both normal and Unicode strings, but can't mix them, all arguments to a function (method) have to be of the same type (or its subclasses).

不过您需要确保汉字是 unicode 格式的:

In [1]: from Levenshtein import distance, hamming, median

In [2]: query1 = '你好世界'

In [3]: query2 = '你好'

In [4]: print distance(query1,query2)
6

In [5]: print distance(query1.decode('utf8'),query2.decode('utf8'))
2

关于python - Levenshtein Distance 是如何计算简体中文字符的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30928004/

相关文章:

python - 在 memcache.get() 之前测试字符串是否是有效键

Javascript:转换代码字符串并返回

python拆分字符串但保留分隔符

python - 使用 python 启动使用 chcp 65001 预激活的控制台窗口

go - 解码编码的 JSON unicode

python - 如何使用Python将unicode字符串转换为真正的字符串

python - Matplotlib 返回空图

python - 在 SQLAlchemy 中使用 PostgresQL INTERVAL,其中持续时间动态存储在数据库中并且不是参数

Python:来自 2 个数组的直方图/分箱数据。

Java - 检查字符串之间的空格