应用场景
配置映射时,每次均要都待配置的列表里面去搜索去对应,效率低下且非常麻烦,采用相似度,可以自动过滤相似的部分,快速实现映射配置。
举例:
算法代码:
复制代码
复制代码
配置映射时,每次均要都待配置的列表里面去搜索去对应,效率低下且非常麻烦,采用相似度,可以自动过滤相似的部分,快速实现映射配置。
举例:
![](http://103.25.64.68:8001/data/attachment/forum/201511/06/163554tgrogqbmiutovleb.jpg)
算法代码:
- <P><STRONG>C#代码</STRONG></P>
- <P>using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;</P>
- <P>namespace Tech.Tools.Common
- {
- /// <summary>
- /// 计算两个字符串相似度管理类(算法是由俄国科学家Levenshtein提出)
- /// </summary>
- public class StrSimilarityDegreeParser
- {
- /// <summary>
- /// 返回两个字符串相似度
- /// </summary>
- /// <param name="str1"></param>
- /// <param name="str2"></param>
- /// <returns></returns>
- public float LevenshteinDistancePercent(String str1, String str2)
- {
- int maxLenth = str1.Length > str2.Length ? str1.Length : str2.Length;
- int val = Levenshtein_Distance(str1, str2);
- return 1 - float.Parse(val.ToString()) / maxLenth;
- }
- /**
- *取最小的一位数
- * @param first
- * @param second
- * @param third
- * @return
- *
- */
- private int LowerOfThree(int first, int second, int third)
- {
- int min = first;
- if (second < min)
- min = second;
- if (third < min)
- min = third;
- return min;
- }
- private int Levenshtein_Distance(String str1, String str2)
- {
- int n = str1.Length;
- int m = str2.Length;
- int[][] Matrix = new int[n + 1][];</P>
- <P> int temp = 0;
- char ch1;
- char ch2;
- int i = 0;
- int j = 0;
- if (n == 0)
- {
- return m;
- }
- if (m == 0)
- {
- return n;
- }</P>
- <P> for (i = 0; i <= n; i++)
- {
- Matrix[i] = new int[m + 1];
- for (j = 0; j <= m; j++)
- {
- if (j == 0)
- {
- Matrix[i][j] = i;
- }
- else if (i == 0)
- {
- Matrix[i][j] = j;
- }
- }
- }
- for (i = 1; i <= n; i++)
- {
- ch1 = str1.ToCharArray()[i - 1];
- for (j = 1; j <= m; j++)
- {
- ch2 = str2.ToCharArray()[j - 1];
- if (ch1 == ch2)
- {
- temp = 0;
- }
- else
- {
- temp = 1;
- }
- Matrix[i][j] = LowerOfThree(Matrix[i - 1][j] + 1, Matrix[i][j - 1] + 1, Matrix[i - 1][j - 1] + temp);
- }
- }
- return Matrix[n][m];
- }
- }
- }
- </P>
- <P><STRONG>Delphi代码</STRONG></P>
- <P> </P>
- <P>function LowerOfThree(nFirst,nSecond,nThird:Integer):Integer;
- var
- nMin:Integer;
- begin
- nMin := nFirst;
- if nSecond < nMin then nMin := nSecond;
- if nThird < nMin then nMin := nThird;
- Result := nMin;
- end;
- //------------------------------------------------------------------------------
- function Levenshtein_Distance(Str1:WideString;Str2:WideString):Integer;
- var
- n,m,i,j,temp:Integer;
- char1,char2:WideString;
- Matrix:array of array of Integer;
- begin
- n := Length(Str1);m := Length(Str2);
- if n=0 then
- begin
- Result := m;
- Exit;
- end;
- if m=0 then
- begin
- Result := n;
- Exit;
- end;
- SetLength(Matrix,n+1,m+1);
- temp := 0; i := 0; j := 0;
- for i := 0 to n do
- begin
- for j:=0 to m do
- begin
- if j=0 then Matrix[i][j] := i
- else if i=0 then Matrix[i][j] := j;
- end;
- end;
- for i := 1 to n do
- begin
- char1 := Copy(Str1,i,1);
- for j:=1 to m do
- begin
- char2 := Copy(Str2,j,1);
- if char1 = char2 then temp := 0
- else temp := 1;
- Matrix[i][j] := LowerOfThree(Matrix[i-1][j]+1,Matrix[i][j-1]+1,Matrix[i-1][j-1]+temp);
- end;
- end;
- Result := Matrix[n][m];
- end;
- //------------------------------------------------------------------------------
- function LevenshteinDistancePercent(Str1:WideString;Str2:WideString):Real;
- var
- nMaxLen,Val:Integer;
- begin
- if Length(Str1) > Length(Str2) then nMaxLen := Length(Str1)
- else nMaxLen := Length(Str2);
- Val := Levenshtein_Distance(Str1,Str2);
- Result := 1- (Val * 1.0)/(nMaxLen*1.0);
- end;
- //------------------------------------------------------------------------------</P>
- <P><STRONG>--调用示例</STRONG></P>
- <P>procedure TFrmMainForm.CompareDataSet(Str1: String);
- var
- Str2:WideString;
- nResult:Real;
- begin
- cdsPlat.DisableControls;
- cdsPlat.Filtered := False;
- if cdsPlat.IsEmpty then Exit;
- try
- cdsPlat.First;
- while not cdsPlat.Eof do
- begin
- Str2 := cdsPlat.FieldByName('ITEMNAME').AsString;
- nResult := LevenshteinDistancePercent(Str1,Str2);
- cdsPlat.Edit;
- cdsPlat.FieldByName('LIKEBYTE').AsFloat := nResult*100;
- cdsPlat.Post;
- cdsPlat.Next;
- end;
- finally
- cdsPlat.EnableControls;
- end;
- end;</P>