java字符串相似度算法

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

/**
 * 判断两个字符串相似度,可设置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;
   }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值