zoukankan      html  css  js  c++  java
  • 字符串相似度计算的方法,使用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#
    
    
    
    
    
  • 相关阅读:
    2017.0323.数字电路与系统-触发器
    2017.0322.数字电路与系统-触发器
    前端切图|点击按钮div变色
    当鼠标聚焦时输入框变色(focus事件实例)
    ajax实现简单的点击左侧菜单,右侧加载不同网页
    前端切图:自制简易音乐播放器
    移动开发之css3实现背景几种渐变效果
    jQuery实现多种切换效果的图片切换的五款插件
    jQuery实现点击开关图片切换
    三个Bootstrap免费字体和图标库
  • 原文地址:https://www.cnblogs.com/GodIsBoy/p/3749433.html
Copyright © 2011-2022 走看看