定义
Levenshtein Distance 是一个用来计算两个序列差异性的字符串矩阵
其中 tail(x) 是 x 除去第一个字符的子字符串
两个单词之间的 LD 就是单个字符编辑的最小次数(插入、删除、替代)
LD 也可以称之为 编辑距离
上下界
如果字符串为相同长度,汉明距离是 LD 的上界
【汉明距离是两个字符串中相应符号不同的位置数】
双字符串的 LD 不大于 三字符串 LD
计算 LD 距离
public int getEditDistance(String input, String target){
int m = input.length();
int n = target.length();
if(m == 0) return n;
if(n == 0) return m;
int[][] matrix = new int[m][n];
for(int i = 0; i < m; i++){
matrix[i][0] = i;
}
for(int j = 0; j < n; j++){
matrix[0][j] = j;
}
for(int i = 1; i < m; i++){
for(int j = 1; j < n; j++){
int t = input.charAt(i) == target.charAt(j) ? 0 : 1;
matrix[i][j] = Math.min(matrix[i - 1][j -1] + t,
Math.min(matrix[i - 1][j] + 1, matrix[i][j - 1] + 1))
}
}
return matrix[m - 1][n -1];
}
参考链接
墙裂推荐 * https://zhuanlan.zhihu.com/p/80682302
https://en.wikipedia.org/wiki/Levenshtein_distance
http://www.voidcn.com/article/p-xvekuwwa-xs.html
http://spark-public.s3.amazonaws.com/nlp/slides/med.pdf
https://zhuanlan.zhihu.com/p/104190037
https://www.cnblogs.com/biyeymyhjob/archive/2012/09/28/2707343.html
https://www.cnblogs.com/ivanyb/archive/2011/11/25/2263356.html
https://blog.csdn.net/chichoxian/article/details/53944188
https://blog.csdn.net/baodream/article/details/80417695