栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何计算python-Levenshtein.ratio

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何计算python-Levenshtein.ratio

通过更仔细地查看C代码,我发现这种明显的矛盾是由于以下事实:

ratio
对“替换”编辑操作的处理与对其他操作的处理(即,成本为2)不同,而
distance
对它们的处理都相同。费用为1。

levenshtein_common
函数内对内部函数 的调用中可以看出
ratio_py


https://github.com/miohtama/python-
Levenshtein/blob/master/Levenshtein.c#L727

static PyObject*ratio_py(PyObject *self, PyObject *args){  size_t lensum;  long int ldist;  if ((ldist = levenshtein_common(args, "ratio", 1, &lensum)) < 0) //Call    return NULL;  if (lensum == 0)    return PyFloat_FromDouble(1.0);  return PyFloat_FromDouble((double)(lensum - ldist)/(lensum));}

并按

distance_py
功能:

https://github.com/miohtama/python-
Levenshtein/blob/master/Levenshtein.c#L715

static PyObject*distance_py(PyObject *self, PyObject *args){  size_t lensum;  long int ldist;  if ((ldist = levenshtein_common(args, "distance", 0, &lensum)) < 0)    return NULL;  return PyInt_FromLong((long)ldist);}

最终导致将不同的cost参数发送到另一个内部函数

lev_edit_distance
,该内部函数具有以下doc片段:

@xcost: If nonzero, the replace operation has weight 2, otherwise all        edit operations have equal weights of 1.

lev_edit_distance()的代码:

_LEV_STATIC_PY size_tlev_edit_distance(size_t len1, const lev_byte *string1,       size_t len2, const lev_byte *string2,       int xcost){  size_t i;

[回答]

所以在我的例子中

ratio('ab', 'ac')
表示在字符串(4)的整个长度上进行替换操作(成本为2),因此
2/4 = 0.5

这就解释了“如何”,我想剩下的唯一方面就是“为什么”,但目前我对这种理解感到满意。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625266.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号