LD = Levenshtein距离

只是在纸上做一些例子,这似乎有效,但有人知道这是否总是如此?

假设我有3个字符串

BOT

BOB

BOM

LD(BOT,BOB)= 1

LD(BOB,BOM)= 1

然后

LD(BOT,BOM)= max(LD(BOT,BOB),LD(BOB,DOM))= 1

OR

BAAB

BBAB

BCCD

LD(BBAB,BAAB)= 1

LD(BBAB,BCCD)= 3

然后

LD(BAAB,BCCD)= max(LD(BBAB,BAAB),LD(BBAB,BCCD))= 3

我想知道这是否一直适用。

即,

LD(B,C)= max(LD(A,C),LD(A,B))


编辑 - 于太平洋标准时间2009年10月22日下午7:08添加

我开始认为这适用于相同长度的单词,否则你仍然可以这样做,但你必须添加单词长度差异的绝对值。

本质上 LD(B,C)= max(LD(A,C),LD(A,B))+ abs(长度(B) - 长度(C))

有帮助吗?

解决方案

不起作用。

LD("BOB", "BOT") == 1
LD("BOT", "BOB") == 1

LD("BOB", "BOB") == 0
max(LD("BOB", "BOT"), LD("BOT", "BOB")) == 1

0 != 1

也可能有更难的例子......

其他提示

不,但确实如此:

lev(a,c)<!> lt; = lev(a,b)+ lev(b,c)(a.k.a <!>“三角不等式)

...并且被VP-Trees和BK-Trees用作启发式。

作为度量,levenshtein距离遵循三角不等式:
http://en.wikipedia.org/wiki/Triangle_inequality

没有比测试更好的了。如果你知道C#运行它。

public Int32 CalculateDistance(String x, String y)
{
    Int32 xl = x.Length;
    Int32 yl = y.Length;
    Int32[,] matrix = new Int32[xl + 1, yl + 1];

    for (Int32 i = 0; i <= xl; i++)
    {
        matrix[i, 0] = i;
    }

    for (Int32 i = 0; i <= yl; i++)
    {
        matrix[0, i] = i;
    }

    for (Int32 j = 1; j <= yl; j++)
    {
        for (Int32 i = 1; i <= xl; i++)
        {                   
            if (x[i - 1] == y[j - 1])
            {
                matrix[i, j] = matrix[i - 1, j - 1];
            }
            else                    
            {
                matrix[i, j] = Min((matrix[i - 1, j] + 1), (matrix[i, j - 1] + 1), (matrix[i - 1, j - 1] + 1));
            }
        }
    }   

    return matrix[xl, yl];
}

这是一个常规的动态编程问题。 维基百科条目具有正确性证明部分。你在找别的吗?

对于此案例不适用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LevenshteinDistance
{
class Program
{
    static void Main(string[] args)
    {
        LevenshteinDistance ld = new LevenshteinDistance();
        string a="B";
        string b="Book";
        string c = "Sick";

        Console.WriteLine("{0} = Max( {1}, {2} )", ld.Compute(b, c), ld.Compute(a, c), ld.Compute(a, b)); 
        if (ld.Compute(b, c) == Math.Max(ld.Compute(a, c), ld.Compute(a, b)))
            Console.WriteLine("Equal");
        else
            Console.WriteLine("Not Equal");
        Console.ReadKey();

    }

}

class LevenshteinDistance
{
    //****************************
    // Get minimum of three values
    //****************************

    private int Minimum(int a, int b, int c)
    {
        int min;

        min = a;
        if (b < min)
        {
            min = b;
        }
        if (c < min)
        {
            min = c;
        }
        return min;

    }

    //*****************************
    // Compute Levenshtein distance
    //*****************************

    public int Compute(string s, string t)
    {
        int[,] matrix; // matrix
        int n; // length of s
        int m; // length of t
        int i; // iterates through s
        int j; // iterates through t
        char s_i; // ith character of s
        char t_j; // jth character of t
        int cost; // cost

        // Step 1
        n = s.Length;
        m = t.Length;
        if (n == 0)
        {
            return m;
        }
        if (m == 0)
        {
            return n;
        }
        matrix = new int[n + 1, m + 1];

        // Step 2

        for (i = 0; i <= n; i++)
        {
            matrix[i, 0] = i;
        }

        for (j = 0; j <= m; j++)
        {
            matrix[0, j] = j;
        }

        // Step 3

        for (i = 1; i <= n; i++)
        {

            s_i = s[(i - 1)];

            // Step 4

            for (j = 1; j <= m; j++)
            {

                t_j = t[(j - 1)];

                // Step 5

                if (s_i == t_j)
                {
                    cost = 0;
                }
                else
                {
                    cost = 1;
                }

                // Step 6

                matrix[i, j] = Minimum(matrix[i - 1, j] + 1, matrix[i, j - 1] + 1, matrix[i - 1, j - 1] + cost);

            }

        }

        // Step 7

        return matrix[n, m];

    }

}

}

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top