C#和SQL实现的字符串相似度计算代码分享

作者:junjie 时间:2021-06-10 14:23:20 

C#实现:


#region 计算字符串相似度
        /// <summary>
        /// 计算字符串相似度
        /// </summary>
        /// <param name="str1">字符串1</param>
        /// <param name="str2">字符串2</param>
        /// <returns>相似度</returns>
        public static float Levenshtein(string str1, string str2)
        {
            //计算两个字符串的长度。 
            int len1 = str1.Length;
            int len2 = str2.Length;
            //比字符长度大一个空间 
            int[,] dif = new int[len1 + 1, len2 + 1];
            //赋初值,步骤B。 
            for (int a = 0; a <= len1; a++)
            {
                dif[a, 0] = a;
            }
            for (int a = 0; a <= len2; a++)
            {
                dif[0, a] = a;
            }
            //计算两个字符是否一样,计算左上的值 
            int temp;
            for (int i = 1; i <= len1; i++)
            {
                for (int j = 1; j <= len2; j++)
                {
                    if (str1.Substring(i - 1, 1) == str2.Substring(j - 1, 1))
                    {
                        temp = 0;
                    }
                    else
                    {
                        temp = 1;
                    }
                    //取三个值中最小的 
                    dif[i, j] = Min(dif[i - 1, j - 1] + temp, dif[i, j - 1] + 1, dif[i - 1, j] + 1);
                }
            }
            return 1 - (float)dif[len1, len2] / Math.Max(str1.Length, str2.Length);
        }
        #endregion

        //比较3个数字得到最小值 
        private static int Min(int i, int j, int k)
        {
            return i < j ? (i < k ? i : k) : (j < k ? j : k);
        }

SQL实现:


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#,SQL,字符串相似度计算
0
投稿

猜你喜欢

  • Java实战之用springboot+netty实现简单的一对一聊天

    2023-12-03 07:28:19
  • 关于Java中的IO流总结(推荐)

    2023-08-23 18:13:56
  • Java使用JDBC连接postgresql数据库示例

    2022-11-06 22:49:02
  • Spring Boot实现Undertow服务器同时支持HTTP2、HTTPS的方法

    2021-09-23 17:02:30
  • 解决idea删除模块后重新创建显示该模块已经被注册的问题

    2023-04-24 18:06:12
  • Java中的OkHttp使用教程

    2023-08-10 19:30:59
  • C#使用GDI+创建缩略图实例

    2023-02-21 01:57:16
  • Java实现多文件上传功能

    2023-08-02 12:52:02
  • Java设计模式之享元模式实例详解

    2021-12-19 17:54:00
  • Android View与Compose互相调用实例探究

    2021-06-11 09:07:29
  • java反射深入剖析(推荐)

    2022-10-10 18:50:35
  • C# partial关键字说明

    2021-12-29 15:29:54
  • Java接口DAO模式代码原理及应用详解

    2023-06-21 05:29:04
  • 详解Spring Boot Security工作流程

    2023-12-17 12:23:52
  • C#中动态数组用法实例

    2021-11-30 16:42:23
  • 关于SpringCloud的微服务结构及微服务远程调用

    2021-11-06 20:11:45
  • Android实现淘宝客户端倒计时界面

    2023-09-18 21:25:09
  • 基于C#设计一个双色球选号工具

    2021-10-07 20:39:57
  • c# 制作gif的四种方法

    2023-03-17 20:01:08
  • 浅谈SpringBoot中的Bean初始化方法 @PostConstruct

    2022-06-11 07:21:17
  • asp之家 软件编程 m.aspxhome.com