字符相似度算法及应用

应用场景
配置映射时,每次均要都待配置的列表里面去搜索去对应,效率低下且非常麻烦,采用相似度,可以自动过滤相似的部分,快速实现映射配置。

举例:


算法代码:

  1. <P><STRONG>C#代码</STRONG></P>
  2. <P>using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;</P>
  6. <P>namespace Tech.Tools.Common
  7. {
  8.     /// <summary>
  9.     /// 计算两个字符串相似度管理类(算法是由俄国科学家Levenshtein提出)
  10.     /// </summary>
  11.   public class StrSimilarityDegreeParser
  12.     {
  13.         /// <summary>
  14.         /// 返回两个字符串相似度
  15.         /// </summary>
  16.         /// <param name="str1"></param>
  17.         /// <param name="str2"></param>
  18.         /// <returns></returns>
  19.         public float LevenshteinDistancePercent(String str1, String str2)
  20.         {
  21.             int maxLenth = str1.Length > str2.Length ? str1.Length : str2.Length;
  22.             int val = Levenshtein_Distance(str1, str2);
  23.             return 1 - float.Parse(val.ToString()) / maxLenth;
  24.         }
  25.         /**
  26.          *取最小的一位数
  27.          * @param first
  28.          * @param second
  29.          * @param third
  30.          * @return
  31.          *
  32.          */
  33.         private int LowerOfThree(int first, int second, int third)
  34.         {
  35.             int min = first;
  36.             if (second < min)
  37.                 min = second;
  38.             if (third < min)
  39.                 min = third;
  40.             return min;
  41.         }
  42.         private int Levenshtein_Distance(String str1, String str2)
  43.         {
  44.             int n = str1.Length;
  45.             int m = str2.Length;
  46.             int[][] Matrix = new int[n + 1][];</P>
  47. <P>            int temp = 0;
  48.             char ch1;
  49.             char ch2;
  50.             int i = 0;
  51.             int j = 0;
  52.             if (n == 0)
  53.             {
  54.                 return m;
  55.             }
  56.             if (m == 0)
  57.             {
  58.                 return n;
  59.             }</P>
  60. <P>            for (i = 0; i <= n; i++)
  61.             {
  62.                 Matrix[i] = new int[m + 1];
  63.                 for (j = 0; j <= m; j++)
  64.                 {
  65.                     if (j == 0)
  66.                     {
  67.                         Matrix[i][j] = i;
  68.                     }
  69.                     else if (i == 0)
  70.                     {
  71.                         Matrix[i][j] = j;
  72.                     }
  73.                 }
  74.             }
  75.             for (i = 1; i <= n; i++)
  76.             {
  77.                 ch1 = str1.ToCharArray()[i - 1];
  78.                 for (j = 1; j <= m; j++)
  79.                 {
  80.                     ch2 = str2.ToCharArray()[j - 1];
  81.                     if (ch1 == ch2)
  82.                     {
  83.                         temp = 0;
  84.                     }
  85.                     else
  86.                     {
  87.                         temp = 1;
  88.                     }
  89.                     Matrix[i][j] = LowerOfThree(Matrix[i - 1][j] + 1, Matrix[i][j - 1] + 1, Matrix[i - 1][j - 1] + temp);
  90.                 }
  91.             }
  92.             return Matrix[n][m];
  93.         }
  94.     }
  95. }
  96. </P>
复制代码

  1. <P><STRONG>Delphi代码</STRONG></P>
  2. <P> </P>
  3. <P>function LowerOfThree(nFirst,nSecond,nThird:Integer):Integer;
  4. var
  5.   nMin:Integer;
  6. begin
  7.   nMin := nFirst;
  8.   if nSecond < nMin then nMin := nSecond;
  9.   if nThird < nMin then nMin := nThird;
  10.   Result := nMin;
  11. end;  
  12. //------------------------------------------------------------------------------
  13. function Levenshtein_Distance(Str1:WideString;Str2:WideString):Integer;
  14. var
  15.   n,m,i,j,temp:Integer;
  16.   char1,char2:WideString;
  17.   Matrix:array of array of Integer;
  18. begin
  19.   n := Length(Str1);m := Length(Str2);
  20.   if n=0 then
  21.   begin
  22.     Result := m;
  23.     Exit;
  24.   end;
  25.   if m=0 then
  26.   begin
  27.     Result := n;
  28.     Exit;
  29.   end;
  30.   SetLength(Matrix,n+1,m+1);
  31.   temp := 0; i := 0; j := 0;
  32.   for i := 0 to n do
  33.   begin
  34.     for j:=0 to m do
  35.     begin
  36.       if j=0 then Matrix[i][j] := i
  37.       else if i=0 then Matrix[i][j] := j;
  38.     end;
  39.   end;
  40.   for i := 1 to n do
  41.   begin
  42.     char1 := Copy(Str1,i,1);
  43.     for j:=1 to m do
  44.     begin
  45.       char2 := Copy(Str2,j,1);
  46.       if char1 = char2 then temp := 0
  47.       else temp := 1;
  48.       Matrix[i][j] := LowerOfThree(Matrix[i-1][j]+1,Matrix[i][j-1]+1,Matrix[i-1][j-1]+temp);
  49.     end;
  50.   end;
  51.   Result := Matrix[n][m];
  52. end;
  53. //------------------------------------------------------------------------------
  54. function LevenshteinDistancePercent(Str1:WideString;Str2:WideString):Real;
  55. var
  56.   nMaxLen,Val:Integer;
  57. begin
  58.   if Length(Str1) > Length(Str2) then nMaxLen := Length(Str1)
  59.   else nMaxLen := Length(Str2);
  60.   Val := Levenshtein_Distance(Str1,Str2);
  61.   Result := 1- (Val * 1.0)/(nMaxLen*1.0);
  62. end;
  63. //------------------------------------------------------------------------------</P>
  64. <P><STRONG>--调用示例</STRONG></P>
  65. <P>procedure TFrmMainForm.CompareDataSet(Str1: String);
  66. var
  67.   Str2:WideString;
  68.   nResult:Real;
  69. begin
  70.   cdsPlat.DisableControls;
  71.   cdsPlat.Filtered := False;
  72.   if cdsPlat.IsEmpty then Exit;
  73.   try
  74.     cdsPlat.First;
  75.     while not cdsPlat.Eof do
  76.     begin
  77.       Str2 := cdsPlat.FieldByName('ITEMNAME').AsString;
  78.       nResult := LevenshteinDistancePercent(Str1,Str2);
  79.       cdsPlat.Edit;
  80.       cdsPlat.FieldByName('LIKEBYTE').AsFloat := nResult*100;
  81.       cdsPlat.Post;
  82.       cdsPlat.Next;
  83.     end;
  84.   finally
  85.     cdsPlat.EnableControls;
  86.   end;
  87. end;</P>
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值