找了不少方法也试了不少第三方接口,个人感觉这个对比纯汉字的相似度是最准的。比较数字和英文时有问题。可以试试SimHash的方式
public static String balance(String source, String target) {
if (source == null && target == null) {
return 1f * 100 + "%";
}
if (source == null || target == null) {
return 0f * 100 + "%";
}
Set<Integer> aChar = source.chars().boxed().collect(Collectors.toSet());
Set<Integer> bChar = target.chars().boxed().collect(Collectors.toSet());
// 交集数量
int intersection = SetUtils.intersection(aChar, bChar).size();
if (intersection == 0) {
return 0 * 100 + "%";
}
int union = SetUtils.union(aChar, bChar).size();
float f = ((float) intersection) / (float)union;
return f * 100 + "%";
}