org.springframework.beans.PropertyMatches 的
calculateStringDistance 方法
/**
* Calculate the distance between the given two Strings
* according to the Levenshtein algorithm.
* @param s1 the first String
* @param s2 the second String
* @return the distance value
*/
private static int calculateStringDistance(String s1, String s2) {
if (s1.isEmpty()) {
return s2.length();
}
if (s2.isEmpty()) {
return s1.length();
}
int d[][] = new int[s1.length() + 1][s2.length() + 1];
for (int i = 0; i <= s1.length(); i++) {
d[i][0] = i;
}
for (int j = 0; j <= s2.length(); j++) {
d[0][j] = j;
}
for (int i = 1; i <= s1.length(); i++) {
char s_i = s1.charAt(i - 1);
for (int j = 1; j <= s2.length(); j++) {
int cost;
char t_j = s2.charAt(j - 1);
if (s_i == t_j) {
cost = 0;
}
else {
cost = 1;
}
d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1),
d[i - 1][j - 1] + cost);
}
}
return d[s1.length()][s2.length()];
}
对于两个字符串、,长度分别为、,它们的Levenshtein Distance 为:
其中当时,为0,否则为1。就是的前个字符与的前个字符的编辑距离。
、的相似度为。
float similarity =1-calculateStringDistance(s1,s2)/Math.max(s1.length,s2.length());
相似度为similarity。