python - 将 Levenshtein 比率转换为 C++

标签 python c++ c

<分区>

是否有用于在 C 或 C++ 中执行以下操作的库?我指的不是使用 C 或 C++ 的 Python 库,而是实际的 C/C++ 库:

>>> import Levenshtein
>>> ratio = Levenshtein.ratio('StackOver', 'Stackoverflow')
0.7272727272727273

最佳答案

我想指出,虽然这个问题一个纯资源问题,但_levenshtein.cpython-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 is wchar_t, Python extension uses Py_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/

相关文章:

python - 使用pymongo在mongodb中按时间范围查询

C++ istream_iterator 不是 std 的成员

c - 带有 if 条件的未使用变量

c - 读取 GPIOB_IDR 寄存器时的值不正确

python - 从字符串中提取数值并保存到数据框时出现问题

python - 循环只取最后一个值

python - 如何检查列表中是否存在 np.NaN 和/或 None

c++ - 避免包装器 DLL 中的堆栈溢出

c++ - CARLA RGB相机传感器的输出格式是什么

c - gcc 是否自动将静态变量初始化为零?