2016-09-05 2 views
0

Левенштейн можно вычислить итеративно с помощью двух рядов таким образом:Итерационная версии Damerau-Левенштейн расстояние

https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows

я наткнулся на Optimal String alignment distance, что принимает во внимание транспозиции. Википедия говорит, что она может быть вычислена с помощью простого расширения регулярного алгоритма Левенштейна:

if i > 1 and j > 1 and a[i-1] = b[j-2] and a[i-2] = b[j-1] then 
    d[i, j] := minimum(d[i, j], 
         d[i-2, j-2] + cost) // transposition 

Однако, я не в состоянии порта расширения алгоритма псевдокода на этой странице, чтобы код итеративной версии. Любая помощь приветствуется.

ответ

1

Вам нужно три строки, чтобы вычислить эту новую версию, я не могу проверить код, но я совершенно уверен в этом:

int DamerauLevenshteinDistance(string s, string t) 
{ 
// degenerate cases 
if (s == t) return 0; 
if (s.Length == 0) return t.Length; 
if (t.Length == 0) return s.Length; 

// create two work vectors of integer distances 
int[] v0 = new int[t.Length + 1]; 
int[] v1 = new int[t.Length + 1]; 
int[] v2 = new int[t.Length + 1]; 

// initialize v0 (the previous row of distances) 
// this row is A[0][i]: edit distance for an empty s 
// the distance is just the number of characters to delete from t 
for (int i = 0; i < v0.Length; i++) 
    v0[i] = i; 

    // compute v1 

    v1[0] = 0; 

    // use formula to fill in the rest of the row 
    for (int j = 0; j < t.Length; j++) 
    { 
     var cost = (s[0] == t[j]) ? 0 : 1; 
     v1[j + 1] = Minimum(v1[j] + 1, v0[j + 1] + 1, v0[j] + cost); 
    } 

if (s.Length == 1) { 
    return v1[t.Length]; 
} 

for (int i = 1; i < s.Length; i++) 
{ 
    // calculate v2 (current row distances) from the previous rows v0 and v1 

    // first element of v2 is A[i+1][0] 
    // edit distance is delete (i+1) chars from s to match empty t 
    v2[0] = i + 1; 

    // use formula to fill in the rest of the row 
    for (int j = 0; j < t.Length; j++) 
    { 
     var cost = (s[i] == t[j]) ? 0 : 1; 
     v2[j + 1] = Minimum(v2[j] + 1, v1[j + 1] + 1, v1[j] + cost); 
     if (j > 0 && s[i] = t[j-1] && s[i-1] = t[j]) 
      v2[j + 1] = Minimum(v2[j+1], 
        v0[j-1] + cost); 
    } 

    // copy v2 (current row) to v1 (previous row) and v1 to v0 for next iteration 
    for (int j = 0; j < v0.Length; j++) 
     v0[j] = v1[j]; 
     v1[j] = v2[j]; 
} 

return v2[t.Length]; 
} 

Исходный код исходит от реализации википедии, упомянутой выше.

Смежные вопросы