字符串编辑距离(levenshtein
distace莱文史特距离)是一种字符串之间相似度算法。对于中文来说,很多时候都是将词作为一个基本单位,而不是字符。
算
法描述:(算法是由俄国科学家Levenshtein提出)
Step
Description
1 |
Set n to be the length of s.
Set m to be the length of t.
If n
= 0, return m and exit.
If m = 0, return n and exit.
Construct a
matrix containing 0..m rows and 0..n columns. |
2 |
Initialize the first row to 0..n.
Initialize the first column to
0..m. |
3 |
Examine each character of s (i from 1 to n). |
4 |
Examine each character of t (j from 1 to m). |
5 |
If s[i] equals t[j], the cost is 0.
If s[i] doesn't equal t[j],
the cost is 1. |
6 |
Set cell d[i,j] of the matrix equal to the minimum of:
a. The
cell immediately above plus 1: d[i-1,j] + 1.
b. The cell immediately
to the left plus 1: d[i,j-1] + 1.
c. The cell diagonally above and to
the left plus the cost: d[i-1,j-1] + cost. |
7 |
After the iteration steps (3, 4, 5, 6) are complete, the distance is
found in cell d[n,m]. |
java实现:
-
Java
-
-
public
class
Distance {
-
-
-
-
-
-
private
int
Minimum (
int
a,
int
b,
int
c) {
-
int
mi;
-
-
mi = a;
-
if
(b < mi) {
-
mi = b;
-
}
-
if
(c < mi) {
-
mi = c;
-
}
-
return
mi;
-
-
}
-
-
-
-
-
-
public
int
LD (String s, String t) {
-
int
d[][];
-
int
n;
-
int
m;
-
int
i;
-
int
j;
-
char
s_i;
-
char
t_j;
-
int
cost;
-
-
-
-
n = s.length ();
-
m = t.length ();
-
if
(n ==
0
) {
-
return
m;
-
}
-
if
(m ==
0
) {
-
return
n;
-
}
-
d =
new
int
[n+
1
][m+
1
];
-
-
-
-
for
(i =
0
; i <= n; i++) {
-
d[i][
0
] = i;
-
}
-
-
for
(j =
0
; j <= m; j++) {
-
d[
0
][j] = j;
-
}
-
-
-
-
for
(i =
1
; i <= n; i++) {
-
-
s_i = s.charAt (i -
1
);
-
-
-
-
for
(j =
1
; j <= m; j++) {
-
-
t_j = t.charAt (j -
1
);
-
-
-
-
if
(s_i == t_j) {
-
cost =
0
;
-
}
-
else
{
-
cost =
1
;
-
}
-
-
-
-
d[i][j] = Minimum (d[i-
1
][j]+
1
, d[i][j-
1
]+
1
, d[i-
1
][j-
1
] + cost);
-
-
}
-
-
}
-
-
-
-
return
d[n][m];
-
-
}
-
-
}
分享到:
相关推荐
jacob LevenshteinDistance.rar
C#,字符串相似度的莱文斯坦距离(Levenshtein Distance)算法与源代码 莱文斯坦距离(Levenshtein Distance)用于衡量两个字符串之间的相似度。 莱文斯坦距离以俄国科学家(Vladimir I. Levenshtein)命名,他于...
来源:http://nayruden.com/?p=115
NULL 博文链接:https://biansutao.iteye.com/blog/326008
C /Cython实现的快速编辑距离(Levenshtein distance)
LevenshteinDistance(编辑距离)算法详解[借鉴].pdf
Levenshtein Distance--求字符串的相似程度的算法,文件用IE6可以打开。
Levenshtein distance is commonly used in many areas especially in speech recognition. It is very useful in finding the edit distance between two words or two sequences of vectors.
标签:airavata-levenshtein-distance-service-0.9.jar,airavata,levenshtein,distance,service,0.9,jar包下载,依赖包
两个字符串的相似度算法实现——编辑距离之Levenshtein距离
Levenshtein:快速计算编辑距离以及字符串的相似度
levenshtein - 这是一个Go实现计算Levenshtein距离算法
Metaphone-拼写检查 拼写检查程序使用 Levenshtein Distance 和 Metaphone 算法的组合为用户提供 5 种可能的更正
是用来度量两个序列相似程度的指标。通俗地来讲,编辑距离指的是在两个单词w1,w2之间,由其中一个单词w1变为w2所需要的最少单字符编辑操作次数。 当两个字符串都为空串,那么编辑距离为0; 当其中一个字符串为空串...
标签:airavata-levenshtein-distance-service-0.5.jar,airavata,levenshtein,distance,service,0.5,jar包下载,依赖包
Levenshtein Distance算法可以看作动态规划 它的思路就是从两个字符串的左边开始比较 记录已经比较过的子串相似度 实际上叫做距离 然后进一步得到下一个字符位置时的相似度 用下面的例子: GUMBO和GAMBOL 当算到矩阵D...
最小编辑距离,计算字符串的相似度,最小编辑距离,计算字符串的相似度最小编辑距离,计算字符串的相似度最小编辑距离,计算字符串的相似度最小编辑距离,计算字符串的相似度最小编辑距离,计算字符串的相似度
编辑距离 这是用于计算和可视化两个字符串之间的 Levenshtein 距离的简单实现。 截屏:
用于快速计算两个字符串之间的 Levenshtein 距离的 C++ mex 代码。 您需要使用“mex -setup”和“men LevenDistance.cpp”编译它。 这是基于损坏的源代码: ...