Levenshtein 거리 조합
-
05-07-2019 - |
문제
ld = Levenshtein 거리
종이에 몇 가지 예제를 수행하면 이것이 효과가있는 것 같습니다. 그러나 이것이 항상 사실인지 아는 사람이 있습니까?
내가 3 줄이 있다고 가정 해 봅시다
봇
단발
BOM
LD (봇, 밥) = 1
그리고
LD (Bob, Bom) = 1
그 다음에
ld (bot, bom) = max (ld (bot, bob), ld (bob, dom)) = 1
또는
바브
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 PST에 추가
나는 이것이 같은 길이의 단어에 대해 보유한다고 생각하기 시작했다. 그렇지 않으면 여전히 할 수 있지만 단어 길이의 차이의 절대 값을 추가해야한다.
본질적으로 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) <= lev (a, b) + lev (b, c) (일명 "삼각형 불평등)
... VP-Tree와 BK-Tree에 의해 휴리스틱으로 많이 사용됩니다.
메트릭이기 때문에 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];
}
이것은 정기적 인 동적 프로그래밍 문제입니다. 그만큼 Wikipedia 항목 정확성 증명 부분이 있습니다. 당신은 다른 것을 찾고 있습니까?
이 경우에도 적용되지 않았습니다
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];
}
}
}