码迷,mamicode.com
首页 > 数据库 > 详细

字符串相似度计算的方法,使用SQL以及C#实现,本文非原创摘自网络(.NET SQL技术交流群入群206656202需注明博客园)

时间:2014-05-26 02:11:57      阅读:486      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   c   code   java   

bubuko.com,布布扣
bubuko.com,布布扣
 1 CREATE   function get_semblance_By_2words 
 2 ( 
 3 @word1 varchar(50), 
 4 @word2 varchar(50)   
 5 ) 
 6 returns nvarchar(4000) 
 7 as 
 8 begin 
 9 declare @re int 
10 declare @maxLenth int 
11 declare @i int,@l int 
12 declare @tb1 table(child varchar(50)) 
13 declare @tb2 table(child varchar(50)) 
14 set @i=1 
15 set @l=2 
16 set @maxLenth=len(@word1) 
17 if len(@word1)<len(@word2)  
18 begin 
19 set @maxLenth=len(@word2) 
20 end 
21 while @l<=len(@word1)  
22 begin 
23 while @i<len(@word1)-1 
24 begin 
25 insert @tb1 (child) values( SUBSTRING(@word1,@i,@l) )  
26 set @i=@i+1 
27 end 
28 set @i=1 
29 set @l=@l+1 
30 end 
31 set @i=1 
32 set @l=2 
33 while @l<=len(@word2)  
34 begin 
35 while @i<len(@word2)-1 
36 begin 
37 insert @tb2 (child) values( SUBSTRING(@word2,@i,@l) )  
38 set @i=@i+1 
39 end 
40 set @i=1 
41 set @l=@l+1 
42 end   
43 select @re=isnull(max( len(a.child)*100/  @maxLenth ) ,0) from @tb1 a, @tb2 b where a.child=b.child 
44 return @re 
45 end 
46 GO 
47   
48 --测试 
49 --select dbo.get_semblance_By_2words(‘我是谁‘,‘我是谁啊‘)  
50 --75 
51 --相似度 
SQL

 


bubuko.com,布布扣
 1 #region 计算字符串相似度
 2         /// <summary>
 3         /// 计算字符串相似度
 4         /// </summary>
 5         /// <param name="str1">字符串1</param>
 6         /// <param name="str2">字符串2</param>
 7         /// <returns>相似度</returns>
 8         public static float Levenshtein(string str1, string str2)
 9         {
10             //计算两个字符串的长度。  
11             int len1 = str1.Length;
12             int len2 = str2.Length;
13             //比字符长度大一个空间  
14             int[,] dif = new int[len1 + 1, len2 + 1];
15             //赋初值,步骤B。  
16             for (int a = 0; a <= len1; a++)
17             {
18                 dif[a, 0] = a;
19             }
20             for (int a = 0; a <= len2; a++)
21             {
22                 dif[0, a] = a;
23             }
24             //计算两个字符是否一样,计算左上的值  
25             int temp;
26             for (int i = 1; i <= len1; i++)
27             {
28                 for (int j = 1; j <= len2; j++)
29                 {
30                     if (str1.Substring(i - 1, 1) == str2.Substring(j - 1, 1))
31                     {
32                         temp = 0;
33                     }
34                     else
35                     {
36                         temp = 1;
37                     }
38                     //取三个值中最小的  
39                     dif[i, j] = Min(dif[i - 1, j - 1] + temp, dif[i, j - 1] + 1, dif[i - 1, j] + 1);
40                 }
41             }
42             return 1 - (float)dif[len1, len2] / Math.Max(str1.Length, str2.Length);
43         }
44         #endregion
45 
46         //比较3个数字得到最小值  
47         private static int Min(int i, int j, int k)
48         {
49             return i < j ? (i < k ? i : k) : (j < k ? j : k);
50         } 
C#

 

bubuko.com,布布扣

字符串相似度计算的方法,使用SQL以及C#实现,本文非原创摘自网络(.NET SQL技术交流群入群206656202需注明博客园),布布扣,bubuko.com

字符串相似度计算的方法,使用SQL以及C#实现,本文非原创摘自网络(.NET SQL技术交流群入群206656202需注明博客园)

标签:style   class   blog   c   code   java   

原文地址:http://www.cnblogs.com/GodIsBoy/p/3749433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!