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 @ I = @ I + 1
End
Set @ I = 1
Set @ l = @ L + 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 @ I = @ I + 1
End
Set @ I = 1
Set @ l = @ L + 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
C #------------------------------------------------------
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace consoleapplication6
{
Class semblance
{
Static void main (string [] ARGs)
{
String Re = get_semblance_by_2words ("who I am", "who I am ");
Console. writeline (re );
Console. Readline ();
}
Public static string get_semblance_by_2words (string word1, string word2)
{
Int Re = 0;
Int maxlength;
Int I, L;
List <string> tb1 = new list <string> ();
List <string> tb2 = new list <string> ();
I = 0;
L = 1;
Maxlength = word1.length;
If (word1.length <word2.length)
Maxlength = word2.length;
While (L <= word1.length)
{
While (I <word1.length-1)
{
If (I + L> word1.length)
Break;
Tb1.add (word1.substring (I, L ));
I ++;
}
I = 0;
L ++;
}
I = 0;
L = 1;
While (L <= word2.length)
{
While (I <word2.length-1)
{
If (I + L> word2.length)
Break;
Tb2.add (word2.substring (I, L ));
I ++;
}
I = 0;
L ++;
}
Foreach (string substr in tb1)
{
Int tempre = 0;
If (tb2.contains (substr ))
{
Tempre = substr. length * 100/maxlength;
If (tempre> re)
Re = tempre;
If (tempre = 100)
Break;
}
}
Return re. tostring () + "% ";
}
}
}