我是靠谱客的博主 耍酷大碗,这篇文章主要介绍java字符串相似度算法,现在分享给大家,希望可以做个参考。

按顺序匹配的,代码如下:

复制代码
1
复制代码
1
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/** * 判断两个字符串相似度,可设置level * @param strSource 原字符串 * @param strCompared 比较字符串 * @param level 评分阀值 * @param moreCount 比较字符串比原字符串多多少个限制 * @return */ public static Boolean isSimilar(String strSource,String strCompared,int level,int moreCount){ if(strCompared.length()-strSource.length()>moreCount){ return false; } int count=strSource.length(); int maxSameCount=0; //遍历count次 for(int i=0;i<count;i++){ int nowSameCount=0; int c=0; int lastIndex=0;//记录上一次匹配的目标索引 //遍历每一次的原字符串所有字段 for(int j=i;j<strSource.length();j++){ char charSource=strSource.charAt(j); for(;c<strCompared.length();c++){ char charCompare=strCompared.charAt(c); if(charSource==charCompare){ nowSameCount++; lastIndex=++c;//如果匹配,手动加1 break; } } c=lastIndex;//遍历完目标字符串,记录当前匹配索引 } if(nowSameCount>maxSameCount){ maxSameCount=nowSameCount; } } //大于原字符串数量的情况 if(maxSameCount>count){ maxSameCount=count-(maxSameCount-count); } double dLv= (double)100*maxSameCount/count; int iLv=10*maxSameCount/count*10; int cha=(int)dLv-iLv; int yu=cha>5?1:0; iLv+=yu*10; if(iLv/10>=level){ return true; }else{ return false; } }

最后

以上就是耍酷大碗最近收集整理的关于java字符串相似度算法的全部内容,更多相关java字符串相似度算法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部