我对Damerau-Levenshtein编辑距离有问题(操作:删除,卖淫,插入,交换)
我需要使用Damerau-Levenshtein矩阵来具体改变第一个带有已使用操作列表的String
示例:
str1: pair
str2: pari
----
distance: 1
SWAP [2] "i": pair -> pari
我的代码可以正确使用矩阵生成
public int[][] matrixgen(String str1, String str2, int rows, int cols)
return LevenshteinDistanceMatrix
我在第二部分遇到了麻烦,事实上在某些情况下这个算法并没有 以正确的顺序执行操作
public int editDistDP(String str1, String str2, int rows, int cols) {
SString sstr_1 = new SString(str1);
SString sstr_2 = new SString(str2);
int i = rows;
int j = cols;
int[][] dp = matrixgen(sstr_1.to_String(), sstr_2.to_String(), rows, cols);
while (i > 0 && j > 0) {
int min;
System.out.println(sstr_1.to_String() + " -> " + sstr_2.to_String());
if (sstr_1.equals_To(sstr_2)) break;
if (i > 1 && j > 1) {
min = min_v2(dp[i][j - 1],
dp[i - 1][j],
dp[i - 1][j - 1],
dp[i - 2][j - 2]); //minumum as 4
} else {
min = min(dp[i][j - 1],
dp[i - 1][j],
dp[i - 1][j - 1]); //minimun as 3
}
if (min == dp[i][j]) {
i--;
j--;
}
else if (min == dp[i - 1][j]) {
System.out.print("DELETE [" + i + "]: " + sstr_1.to_String().charAt(i - 1));
sstr_1.delete(i); //edit sstr_1 and delete in posix i
i--;
}
else if (min == dp[i][j - 1]) {
System.out.print("INSERT [" + i + "]: " + sstr_2.to_String().charAt(j - 1));
sstr_1.insert(sstr_2.to_String().charAt(j - 1), i); //edit sstr_1 and insert sstr_2.to_String().charAt(j - 1) in sstr_1 into posix i
j--;
}
else if (min == dp[i - 1][j - 1]) {
System.out.print("REPLACE [" + i + "]: " + sstr_2.to_String().charAt(j - 1));
sstr_1.replace(i, sstr_2.to_String().charAt(j - 1)); //edit sstr_1 and replace sstr_2.to_String().charAt(j - 1) in sstr_1's posix i
i--;
j--;
}
else if (min == dp[i - 2][j - 2]) {
System.out.print("SWAP [" + (i - 2) + "]: " + sstr_1.to_String().charAt(i - 2));
sstr_1.swap(i - 2); //edit sstr_1 and swap sstr_1's char in posix i-2 with sstr_1's char in posix (i-2)+1
i -= 1;
j -= 1;
}
}
System.out.println(sstr_1.to_String() + " == " + sstr_2.to_String());
return dp[rows][cols];
}
在“对”的情况下 - > “pari”,此算法有效
在“volubile”的情况下 - > “olubiel”,距离为2,我们有交换+删除这是输出:
START: volubile -> olubiel
SWAP [6]: "l" >> volubiel | olubiel //correct
volubiel -> olubiel
REPLACE [7]: "e" >> volubiee | olubiel //ERROR: it has to delete "v" not replace
volubiee -> olubiel
然后返回0