Http://www.jb51.net/article/55941.htm
C # implementations:
Copy CodeThe code is as follows:
#region Calculating String Similarity
<summary>
To calculate the similarity of a string
</summary>
<param name= "str1" > String 1</param>
<param name= "str2" > String 2</param>
<returns> Similarity </returns>
public static float Levenshtein (string str1, String str2)
{
Calculates the length of a two string.
int len1 = str1. Length;
int len2 = str2. Length;
A space larger than the character length
int[,] dif = new Int[len1 + 1, len2 + 1];
Assign the initial value, step b.
for (int a = 0; a <= len1; a++)
{
Dif[a, 0] = A;
}
for (int a = 0; a <= len2; a++)
{
Dif[0, A] = A;
}
Calculates whether two characters are the same, calculates the value on the left
int temp;
for (int i = 1; I <= len1; i++)
{
for (int j = 1; J <= Len2; j + +)
{
if (str1. Substring (i-1, 1) = = str2. Substring (j-1, 1))
{
temp = 0;
}
Else
{
temp = 1;
}
Take the smallest of three values
Dif[i, J] = Min (dif[i-1, j-1] + temp, dif[i, j-1] + 1, dif[i-1, J] + 1);
}
}
Return 1-(float) dif[len1, Len2]/Math.max (str1. Length, str2. Length);
}
#endregion
Compare 3 numbers to get a minimum value
private static int Min (int i, int j, int k)
{
return I < J? (I < K i:k): (J < K j:k);
}
SQL implementation:
Copy CodeThe code is as follows:
CREATE function Get_semblance_by_2words
(
@word1 varchar (50),
@word2 varchar (50)
)
Returns nvarchar (4000)
As
Begin
DECLARE @re int
DECLARE @maxLenth int
DECLARE @i int,@l int
Declare @tb1 table (child varchar (50))
Declare @tb2 table (child varchar (50))
Set @i=1
Set @l=2
Set @maxLenth =len (@word1)
If Len (@word1) <len (@word2)
Begin
Set @maxLenth =len (@word2)
End
While @l<=len (@word1)
Begin
While @i<len (@word1)-1
Begin
Insert @tb1 (Child) VALUES (SUBSTRING (@word1, @i,@l))
Set @[email protected]+1
End
Set @i=1
Set @[email protected]+1
End
Set @i=1
Set @l=2
While @l<=len (@word2)
Begin
While @i<len (@word2)-1
Begin
Insert @tb2 (Child) VALUES (SUBSTRING (@word2, @i,@l))
Set @[email protected]+1
End
Set @i=1
Set @[email protected]+1
End
Select @re =isnull (Max (len (a.child) *100/@maxLenth), 0) from @tb1 A, @tb2 b where a.child=b.child
Return @re
End
GO
--Test
--select Dbo.get_semblance_by_2words (' Who Am I ', ' Who am I? ')
--75
--Similarity degree
Code sharing of string similarity calculation for C # and SQL implementations