只有插入和删除才能找到编辑距离的变化?

2024-09-23 06:23:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要找到一个词和它的排序词(例如:apple和aelpp)之间的编辑距离,只使用递归的插入和删除。你知道吗

我发现了一些使用插入、删除和替换的源代码,但我不知道如何只使用插入和删除。你知道吗

这是我找到的代码:

def ld(s, t):
    if not s: return len(t)
    if not t: return len(s)
    if s[0] == t[0]: return ld(s[1:], t[1:])
    l1 = ld(s, t[1:])
    l2 = ld(s[1:], t)
    l3 = ld(s[1:], t[1:])
    return 1 + min(l1, l2, l3)

需要进行哪些编辑才能只找到插入和删除的数量?你知道吗


Tags: 代码编辑距离l1applelenreturnif
1条回答
网友
1楼 · 发布于 2024-09-23 06:23:00

删除l3,它会像这样计算替换

def ld2(s, t):
    if not s: return len(t)
    if not t: return len(s)
    if s[0] == t[0]: return ld2(s[1:], t[1:])
    l1 = ld2(s, t[1:])
    l2 = ld2(s[1:], t)
    return 1 + min(l1, l2)

可以看到ld('apple', 'applx')等于1,而具有相同参数的ld2的计算结果为2。你知道吗

相关问题 更多 >