<分区>
是否有用于在 C 或 C++ 中执行以下操作的库?我指的不是使用 C 或 C++ 的 Python 库,而是实际的 C/C++ 库:
>>> import Levenshtein
>>> ratio = Levenshtein.ratio('StackOver', 'Stackoverflow')
0.7272727272727273
<分区>
是否有用于在 C 或 C++ 中执行以下操作的库?我指的不是使用 C 或 C++ 的 Python 库,而是实际的 C/C++ 库:
>>> import Levenshtein
>>> ratio = Levenshtein.ratio('StackOver', 'Stackoverflow')
0.7272727272727273
最佳答案
我想指出,虽然这个问题是一个纯资源问题,但_levenshtein.c
在 python-Levenshtein
的源代码分发中,它本身可以用作 C-only 库,前提是它是用 -DNO_PYTHON
编译的:
Levenshtein.c can be used as a pure C library, too. You only have to define
NO_PYTHON
preprocessor symbol (-DNO_PYTHON
) when compiling it. The functionality is similar to that of the Python extension. No separate docs are provided yet, RTFS. But they are not interchangeable:C functions exported when compiling with
-DNO_PYTHON
(see_levenshtein.h
) are not exported when compiling as a Python extension (and vice versa) Unicode character type used with-DNO_PYTHON
iswchar_t
, Python extension usesPy_UNICODE
, they may be the same but don't count on it
一个例子:
#define NO_PYTHON
#include <stdio.h>
#include <string.h>
#include "_levenshtein.h"
double ratio(char *s1, char *s2) {
size_t l1 = strlen(s1);
size_t l2 = strlen(s2);
size_t lsum = l1 + l2;
if (lsum == 0) {
return 1;
}
size_t distance = lev_edit_distance(l1, s1, l2, s2, 1);
return ((double)lsum - distance) / (lsum);
}
int main() {
char *str1 = "StackOver";
char *str2 = "Stackoverflow";
printf("%.16f\n", ratio(str1, str2));
}
用-DNO_PYTHON
编译_levenshtein.c
并链接在一起,运行;输出
0.7272727272727273
关于python - 将 Levenshtein 比率转换为 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29185720/