概述
Java获取两个字符串的相似度
public static double getStringSimilarity(String sourceString, String targetString) {
int[][] matrix;
int sourceStringLength = sourceString.length();
int targetStringLength = targetString.length();
int indexOfSourceString;
int indexOfTargetString;
char charOfSourceString;
char charOfTargetString;
int temp;
if (sourceStringLength == 0 || targetStringLength == 0) return 0;
matrix = new int[sourceStringLength + 1][targetStringLength + 1];
for (indexOfSourceString = 0; indexOfSourceString <= sourceStringLength; indexOfSourceString++) {
matrix[indexOfSourceString][0] = indexOfSourceString;
}
for (indexOfTargetString = 0; indexOfTargetString <= targetStringLength; indexOfTargetString++) {
matrix[0][indexOfTargetString] = indexOfTargetString;
}
for (indexOfSourceString = 1; indexOfSourceString <= sourceStringLength; indexOfSourceString++) {
charOfSourceString = sourceString.charAt(indexOfSourceString - 1);
for (indexOfTargetString = 1; indexOfTargetString <= targetStringLength; indexOfTargetString++) {
charOfTargetString = targetString.charAt(indexOfTargetString - 1);
if (charOfSourceString == charOfTargetString || charOfSourceString == charOfTargetString + 32 || charOfSourceString + 32 == charOfTargetString) {
temp = 0;
} else temp = 1;
matrix[indexOfSourceString][indexOfTargetString] = Math.min(Math.min(matrix[indexOfSourceString - 1][indexOfTargetString] + 1, matrix[indexOfSourceString][indexOfTargetString - 1] + 1), matrix[indexOfSourceString - 1][indexOfTargetString - 1] + temp);
}
}
return (1 - (double) matrix[sourceStringLength][targetStringLength] / Math.max(sourceString.length(), targetString.length())) * 100F;
}
最后
以上就是舒心丝袜为你收集整理的Java获取两个字符串的相似度Java获取两个字符串的相似度的全部内容,希望文章能够帮你解决Java获取两个字符串的相似度Java获取两个字符串的相似度所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复