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


本文整理自网络,侵删。

 
用百分比比较两个字符串(彼此之间有多少相似度)
返回 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 ;

来源:https://www.cnblogs.com/ljl_falcon/archive/2012/03/19/2405968.html

相关阅读 >>

Delphi xe 打开andorid gps设置

Delphi字符串隐藏

Delphi 获取文件的最新修改时间

Delphi研究之驱动开发篇(七)--利用共享内存与用户模式

Delphi 16进制字符串转原字符串

Delphi 10分钟10行代码开发app(Delphi 应用案例)

Delphi json 转换成 tfdmemtable

Delphi & c++ 安卓使用权限

Delphi生成guid的两种方法

Delphi中的布尔类型

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...