String similarity algorithm (SQL Server and C #)

Source: Internet
Author: User
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 () + "% ";
}
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.