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

复制代码
复制代码

 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
复制代码

 

复制代码

复制代码
 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#
复制代码

 


复制代码
复制代码

posted on   思多久方为远见  阅读(286)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示