• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Delphi中比较两个字符串相似性的百分比算法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

用百分比比较两个字符串(彼此之间有多少相似度)
返回 byte 类型,从 0 到 100%

function  CompareStringsInPercent(Str1, Str2: string ): Byte;
type
  
 TLink = array [0..1] of  Byte;
var
  
 tmpPattern: TLink;
  PatternA, PatternB: array of  TLink;
  IndexA, IndexB, LengthStr: Integer;
begin
  
 Result := 100;
  // Building pattern tables
  
 
LengthStr := Max(Length(Str1), Length(Str2));
  for  IndexA := 1 to  LengthStr do 
  begin
    if 
 Length(Str1) >= IndexA then 
    begin
      
 SetLength(PatternA, (Length(PatternA) + 1));
      PatternA[Length(PatternA) - 1][0] := Byte(Str1[IndexA]);
      PatternA[Length(PatternA) - 1][1] := IndexA;
    end ;
    if  Length(Str2) >= IndexA then 
    begin
      
 SetLength(PatternB, (Length(PatternB) + 1));
      PatternB[Length(PatternB) - 1][0] := Byte(Str2[IndexA]);
      PatternB[Length(PatternB) - 1][1] := IndexA;
    end ;
  end ;
  // Quick Sort of pattern tables
  
 
IndexA := 0;
  IndexB := 0;
  while  ((IndexA < (Length(PatternA) - 1)) and  (IndexB < (Length(PatternB) - 1))) do 
  begin
    if 
 Length(PatternA) > IndexA then 
    begin
      if 
 PatternA[IndexA][0] < PatternA[IndexA + 1][0] then 
      begin
        
 tmpPattern[0]           := PatternA[IndexA][0];
        tmpPattern[1]           := PatternA[IndexA][1];
        PatternA[IndexA][0]     := PatternA[IndexA + 1][0];
        PatternA[IndexA][1]     := PatternA[IndexA + 1][1];
        PatternA[IndexA + 1][0] := tmpPattern[0];
        PatternA[IndexA + 1][1] := tmpPattern[1];
        if  IndexA > 0 then  Dec(IndexA);
      end
      else 
        
 Inc(IndexA);
    end ;
    if  Length(PatternB) > IndexB then 
    begin
      if 
 PatternB[IndexB][0] < PatternB[IndexB + 1][0] then 
      begin
        
 tmpPattern[0]           := PatternB[IndexB][0];
        tmpPattern[1]           := PatternB[IndexB][1];
        PatternB[IndexB][0]     := PatternB[IndexB + 1][0];
        PatternB[IndexB][1]     := PatternB[IndexB + 1][1];
        PatternB[IndexB + 1][0] := tmpPattern[0];
        PatternB[IndexB + 1][1] := tmpPattern[1];
        if  IndexB > 0 then  Dec(IndexB);
      end
      else 
        
 Inc(IndexB);
    end ;
  end ;
  // Calculating simularity percentage
  
 
LengthStr := Min(Length(PatternA), Length(PatternB));
  for  IndexA := 0 to  (LengthStr - 1) do 
  begin
    if 
 PatternA[IndexA][0] = PatternB[IndexA][0] then 
    begin
      if 
 Max(PatternA[IndexA][1], PatternB[IndexA][1]) - Min(PatternA[IndexA][1],
        PatternB[IndexA][1]) > 0 then  Dec(Result,
        ((100 div  LengthStr) div  (Max(PatternA[IndexA][1], PatternB[IndexA][1]) -
          Min(PatternA[IndexA][1], PatternB[IndexA][1]))))
      else if  Result < 100 then  Inc(Result);
    end
    else 
      
 Dec(Result, (100 div  LengthStr))
  end ;
  SetLength(PatternA, 0);
  SetLength(PatternB, 0);
end ;


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab笔记四(对向量进行数组运算、获取函数的多个输出)发布时间:2022-07-18
下一篇:
MATLAB 矩阵操作(三)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap