字串相似性的演算法(sqlserver轉和c#)

來源:互聯網
上載者: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
 
--測試
--select dbo.get_semblance_By_2words('我是誰','我是誰啊') 
--75

--相似性 

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("我是誰", "我是誰啊");
            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()+"%";
        }
    }
}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.