Tsql T-SQL中的Levenshtein距离
我对计算Levenshtein距离的T-SQL算法感兴趣。Arnold Fribble在这方面有两个建议Tsql T-SQL中的Levenshtein距离,tsql,edit-distance,levenshtein-distance,Tsql,Edit Distance,Levenshtein Distance,我对计算Levenshtein距离的T-SQL算法感兴趣。Arnold Fribble在这方面有两个建议 一个来自和 另一个更新自 这是2006年最年轻的一个: SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE FUNCTION edit_distance_within(@s nvarchar(4000), @t nvarchar(4000), @d int) RETURNS int AS BEGIN DECLARE @
- 一个来自和
- 另一个更新自
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE FUNCTION edit_distance_within(@s nvarchar(4000), @t nvarchar(4000), @d int)
RETURNS int
AS
BEGIN
DECLARE @sl int, @tl int, @i int, @j int, @sc nchar, @c int, @c1 int,
@cv0 nvarchar(4000), @cv1 nvarchar(4000), @cmin int
SELECT @sl = LEN(@s), @tl = LEN(@t), @cv1 = '', @j = 1, @i = 1, @c = 0
WHILE @j <= @tl
SELECT @cv1 = @cv1 + NCHAR(@j), @j = @j + 1
WHILE @i <= @sl
BEGIN
SELECT @sc = SUBSTRING(@s, @i, 1), @c1 = @i, @c = @i, @cv0 = '', @j = 1, @cmin = 4000
WHILE @j <= @tl
BEGIN
SET @c = @c + 1
SET @c1 = @c1 - CASE WHEN @sc = SUBSTRING(@t, @j, 1) THEN 1 ELSE 0 END
IF @c > @c1 SET @c = @c1
SET @c1 = UNICODE(SUBSTRING(@cv1, @j, 1)) + 1
IF @c > @c1 SET @c = @c1
IF @c < @cmin SET @cmin = @c
SELECT @cv0 = @cv0 + NCHAR(@c), @j = @j + 1
END
IF @cmin > @d BREAK
SELECT @cv1 = @cv0, @i = @i + 1
END
RETURN CASE WHEN @cmin <= @d AND @c <= @d THEN @c ELSE -1 END
END
GO
设置带引号的\u标识符
去
将ANSI_空值设置为ON
去
创建函数编辑距离(@s nvarchar(4000),@t nvarchar(4000),@d int)
返回整数
作为
开始
声明@sl int、@tl int、@i int、@j int、@sc-nchar、@c int、@c1 int、,
@cv0 nvarchar(4000),@cv1 nvarchar(4000),@cmin int
选择@sl=LEN(@s),@tl=LEN(@t),@cv1='',@j=1,@i=1,@c=0
当@j@d中断时
选择@cv1=@cv0,@i=@i+1
结束
返回@cminIIRC时的大小写,使用SQL Server 2005及更高版本,您可以用任何.NET语言编写存储过程:。有了这些,编写一个计算程序应该不难
一个简单的你好,世界!摘自帮助:
using System;
using System.Data;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
public class HelloWorldProc
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void HelloWorld(out string text)
{
SqlContext.Pipe.Send("Hello world!" + Environment.NewLine);
text = "Hello world!";
}
}
然后在SQL Server中运行以下命令:
CREATE ASSEMBLY helloworld from 'c:\helloworld.dll' WITH PERMISSION_SET = SAFE
CREATE PROCEDURE hello
@i nchar(25) OUTPUT
AS
EXTERNAL NAME helloworld.HelloWorldProc.HelloWorld
现在您可以测试运行它:
DECLARE @J nchar(25)
EXEC hello @J out
PRINT @J
希望这有帮助。您可以使用Levenshtein距离算法来比较字符串
在这里,您可以在以下位置找到一个T-SQL示例:
该算法只返回stpe计数,通过在一个步骤中替换不同的字符将一个字符串更改为另一个字符串我在TSQL中实现了标准的Levenshtein编辑距离函数,并进行了一些优化,与我所知道的其他版本相比,提高了速度。如果两个字符串的开头有共同的字符(共享前缀),结尾有共同的字符(共享后缀),并且字符串较大且提供了最大编辑距离,则速度的提高是显著的。例如,当输入是两个非常相似的4000个字符串,并且指定了最大编辑距离2时,这几乎比接受答案中的编辑距离\u in
函数快三个数量级,返回答案的时间分别为0.073秒(73毫秒)和55秒。它还具有内存效率,使用的空间等于两个输入字符串中较大的字符串加上一些常量空间。它使用一个表示列的nvarchar“array”,并在其中执行所有计算,以及一些helper int变量
优化:
- 跳过共享前缀和/或后缀的处理
- 如果较大字符串以整个较小字符串开始或结束,则提前返回
- 如果尺寸差异保证超过最大距离,则提前返回
- 仅使用表示矩阵中一列的单个数组(实现为nvarchar)
- 当给定最大距离时,时间复杂度从(len1*len2)变为(min(len1,len2)),即线性
- 当给定最大距离时,在已知无法达到最大距离界限时尽早返回
以下是代码(更新于2014年1月20日,以加快速度):
我也在寻找Levenshtein算法的代码示例,很高兴在这里找到它。当然,我想了解算法是如何工作的,我正在玩上面的一个例子,我正在玩一点,这是作者发布的。为了更好地理解代码,我用矩阵创建了一个EXCEL
图像能说出1000多个单词
有了这个EXCEL,我发现有可能进行额外的性能优化。不需要计算右上角红色区域中的所有值。每个红细胞的值等于左细胞的值加1。这是因为,第二个字符串在该区域中始终比第一个字符串长,这会使每个字符的距离增加1
通过在TSQL中使用语句IF@j,比较两个项目的最佳和最快方法是连接索引列上的表的SELECT语句。因此,如果您想从RDBMS引擎的优势中获益,我建议您实现编辑距离。TSQL循环也可以工作,但对于大容量比较,其他语言中的Levenstein距离计算将比TSQL更快
我已经在几个系统中实现了编辑距离,这些系统使用了一系列针对临时表的连接,这些临时表仅用于编辑距离。它需要一些繁重的预处理步骤——准备临时表——但在进行大量比较时效果非常好
简而言之:预处理包括创建、填充和索引临时表。第一个包含引用ID、一个单字母列和一个charindex列。此表通过运行一系列insert查询来填充,这些查询将每个单词拆分为字母(使用SELECT SUBSTRING),以创建源列表中单词包含字母的行数(我知道,行数很多,但SQL server可以处理数十亿行)。然后制作第二个表,其中有一个2个字母的列,另一个表有一个3个字母的列,等等。最终结果是一系列表,其中包含每个单词的引用ID和子字符串,以及它们在单词中位置的引用
一旦这样做了,整个游戏就是复制这些表,并通过计算匹配数的select查询将它们合并到一个组中。这为每对可能的单词创建了一系列度量,然后将这些度量重新聚合为每对单词的单个Levenstein距离
从技术上讲,这与Levenstein距离(或其变体)的大多数其他实现非常不同,因此您需要深入了解Levenstein距离是如何工作的,以及为什么它是这样设计的。也要研究替代方法,因为使用该方法,您最终会得到一系列基本指标,这些指标可以帮助您同时计算编辑距离的许多变量,为您提供有趣的机器学习潜在改进
另一点
CREATE FUNCTION edit_distance(@s1 nvarchar(3999), @s2 nvarchar(3999))
RETURNS int
AS
BEGIN
DECLARE @s1_len int, @s2_len int
DECLARE @i int, @j int, @s1_char nchar, @c int, @c_temp int
DECLARE @cv0 varbinary(8000), @cv1 varbinary(8000)
SELECT
@s1_len = LEN(@s1),
@s2_len = LEN(@s2),
@cv1 = 0x0000,
@j = 1, @i = 1, @c = 0
WHILE @j <= @s2_len
SELECT @cv1 = @cv1 + CAST(@j AS binary(2)), @j = @j + 1
WHILE @i <= @s1_len
BEGIN
SELECT
@s1_char = SUBSTRING(@s1, @i, 1),
@c = @i,
@cv0 = CAST(@i AS binary(2)),
@j = 1
WHILE @j <= @s2_len
BEGIN
SET @c = @c + 1
SET @c_temp = CAST(SUBSTRING(@cv1, @j+@j-1, 2) AS int) +
CASE WHEN @s1_char = SUBSTRING(@s2, @j, 1) THEN 0 ELSE 1 END
IF @c > @c_temp SET @c = @c_temp
SET @c_temp = CAST(SUBSTRING(@cv1, @j+@j+1, 2) AS int)+1
IF @c > @c_temp SET @c = @c_temp
SELECT @cv0 = @cv0 + CAST(@c AS binary(2)), @j = @j + 1
END
SELECT @cv1 = @cv0, @i = @i + 1
END
RETURN @c
END
select
dbo.edit_distance('Fuzzy String Match','fuzzy string match'),
dbo.edit_distance('fuzzy','fuzy'),
dbo.edit_distance('Fuzzy String Match','fuzy string match'),
dbo.edit_distance('levenshtein distance sql','levenshtein sql server'),
dbo.edit_distance('distance','server')
-- =============================================
-- Computes and returns the Levenshtein edit distance between two strings, i.e. the
-- number of insertion, deletion, and sustitution edits required to transform one
-- string to the other, or NULL if @max is exceeded. Comparisons use the case-
-- sensitivity configured in SQL Server (case-insensitive by default).
--
-- Based on Sten Hjelmqvist's "Fast, memory efficient" algorithm, described
-- at http://www.codeproject.com/Articles/13525/Fast-memory-efficient-Levenshtein-algorithm,
-- with some additional optimizations.
-- =============================================
CREATE FUNCTION [dbo].[Levenshtein](
@s nvarchar(4000)
, @t nvarchar(4000)
, @max int
)
RETURNS int
WITH SCHEMABINDING
AS
BEGIN
DECLARE @distance int = 0 -- return variable
, @v0 nvarchar(4000)-- running scratchpad for storing computed distances
, @start int = 1 -- index (1 based) of first non-matching character between the two string
, @i int, @j int -- loop counters: i for s string and j for t string
, @diag int -- distance in cell diagonally above and left if we were using an m by n matrix
, @left int -- distance in cell to the left if we were using an m by n matrix
, @sChar nchar -- character at index i from s string
, @thisJ int -- temporary storage of @j to allow SELECT combining
, @jOffset int -- offset used to calculate starting value for j loop
, @jEnd int -- ending value for j loop (stopping point for processing a column)
-- get input string lengths including any trailing spaces (which SQL Server would otherwise ignore)
, @sLen int = datalength(@s) / datalength(left(left(@s, 1) + '.', 1)) -- length of smaller string
, @tLen int = datalength(@t) / datalength(left(left(@t, 1) + '.', 1)) -- length of larger string
, @lenDiff int -- difference in length between the two strings
-- if strings of different lengths, ensure shorter string is in s. This can result in a little
-- faster speed by spending more time spinning just the inner loop during the main processing.
IF (@sLen > @tLen) BEGIN
SELECT @v0 = @s, @i = @sLen -- temporarily use v0 for swap
SELECT @s = @t, @sLen = @tLen
SELECT @t = @v0, @tLen = @i
END
SELECT @max = ISNULL(@max, @tLen)
, @lenDiff = @tLen - @sLen
IF @lenDiff > @max RETURN NULL
-- suffix common to both strings can be ignored
WHILE(@sLen > 0 AND SUBSTRING(@s, @sLen, 1) = SUBSTRING(@t, @tLen, 1))
SELECT @sLen = @sLen - 1, @tLen = @tLen - 1
IF (@sLen = 0) RETURN @tLen
-- prefix common to both strings can be ignored
WHILE (@start < @sLen AND SUBSTRING(@s, @start, 1) = SUBSTRING(@t, @start, 1))
SELECT @start = @start + 1
IF (@start > 1) BEGIN
SELECT @sLen = @sLen - (@start - 1)
, @tLen = @tLen - (@start - 1)
-- if all of shorter string matches prefix and/or suffix of longer string, then
-- edit distance is just the delete of additional characters present in longer string
IF (@sLen <= 0) RETURN @tLen
SELECT @s = SUBSTRING(@s, @start, @sLen)
, @t = SUBSTRING(@t, @start, @tLen)
END
-- initialize v0 array of distances
SELECT @v0 = '', @j = 1
WHILE (@j <= @tLen) BEGIN
SELECT @v0 = @v0 + NCHAR(CASE WHEN @j > @max THEN @max ELSE @j END)
SELECT @j = @j + 1
END
SELECT @jOffset = @max - @lenDiff
, @i = 1
WHILE (@i <= @sLen) BEGIN
SELECT @distance = @i
, @diag = @i - 1
, @sChar = SUBSTRING(@s, @i, 1)
-- no need to look beyond window of upper left diagonal (@i) + @max cells
-- and the lower right diagonal (@i - @lenDiff) - @max cells
, @j = CASE WHEN @i <= @jOffset THEN 1 ELSE @i - @jOffset END
, @jEnd = CASE WHEN @i + @max >= @tLen THEN @tLen ELSE @i + @max END
WHILE (@j <= @jEnd) BEGIN
-- at this point, @distance holds the previous value (the cell above if we were using an m by n matrix)
SELECT @left = UNICODE(SUBSTRING(@v0, @j, 1))
, @thisJ = @j
SELECT @distance =
CASE WHEN (@sChar = SUBSTRING(@t, @j, 1)) THEN @diag --match, no change
ELSE 1 + CASE WHEN @diag < @left AND @diag < @distance THEN @diag --substitution
WHEN @left < @distance THEN @left -- insertion
ELSE @distance -- deletion
END END
SELECT @v0 = STUFF(@v0, @thisJ, 1, NCHAR(@distance))
, @diag = @left
, @j = case when (@distance > @max) AND (@thisJ = @i + @lenDiff) then @jEnd + 2 else @thisJ + 1 end
END
SELECT @i = CASE WHEN @j > @jEnd + 1 THEN @sLen + 1 ELSE @i + 1 END
END
RETURN CASE WHEN @distance <= @max THEN @distance ELSE NULL END
END
-- prefix common to both strings can be ignored
WHILE (@start < @sLen AND SUBSTRING(@s, @start, 1) = SUBSTRING(@t, @start, 1) COLLATE SQL_Latin1_General_Cp1_CS_AS)
SELECT @distance =
CASE WHEN (@sChar = SUBSTRING(@t, @j, 1) COLLATE SQL_Latin1_General_Cp1_CS_AS) THEN @diag --match, no change
CREATE FUNCTION [dbo].[f_LevenshteinDistance](@s1 nvarchar(3999), @s2 nvarchar(3999))
RETURNS int
AS
BEGIN
DECLARE @s1_len int;
DECLARE @s2_len int;
DECLARE @i int;
DECLARE @j int;
DECLARE @s1_char nchar;
DECLARE @c int;
DECLARE @c_temp int;
DECLARE @cv0 varbinary(8000);
DECLARE @cv1 varbinary(8000);
SELECT
@s1_len = LEN(@s1),
@s2_len = LEN(@s2),
@cv1 = 0x0000 ,
@j = 1 ,
@i = 1 ,
@c = 0
WHILE @j <= @s2_len
SELECT @cv1 = @cv1 + CAST(@j AS binary(2)), @j = @j + 1;
WHILE @i <= @s1_len
BEGIN
SELECT
@s1_char = SUBSTRING(@s1, @i, 1),
@c = @i ,
@cv0 = CAST(@i AS binary(2)),
@j = 1;
SET @i = @i + 1;
WHILE @j <= @s2_len
BEGIN
SET @c = @c + 1;
IF @j <= @i
BEGIN
SET @c_temp = CAST(SUBSTRING(@cv1, @j + @j - 1, 2) AS int) + CASE WHEN @s1_char = SUBSTRING(@s2, @j, 1) THEN 0 ELSE 1 END;
IF @c > @c_temp SET @c = @c_temp
SET @c_temp = CAST(SUBSTRING(@cv1, @j + @j + 1, 2) AS int) + 1;
IF @c > @c_temp SET @c = @c_temp;
END;
SELECT @cv0 = @cv0 + CAST(@c AS binary(2)), @j = @j + 1;
END;
SET @cv1 = @cv0;
END;
RETURN @c;
END;
-- =============================================
-- Computes and returns the Levenshtein edit distance between two strings, i.e. the
-- number of insertion, deletion, and sustitution edits required to transform one
-- string to the other, or NULL if @max is exceeded. Comparisons use the case-
-- sensitivity configured in SQL Server (case-insensitive by default).
--
-- Based on Sten Hjelmqvist's "Fast, memory efficient" algorithm, described
-- at http://www.codeproject.com/Articles/13525/Fast-memory-efficient-Levenshtein-algorithm,
-- with some additional optimizations.
-- =============================================
CREATE FUNCTION [db0].[Levenshtein](
@s nvarchar(4000)
, @t nvarchar(4000)
, @max int
)
RETURNS int
WITH SCHEMABINDING
AS
BEGIN
DECLARE @distance int = 0 -- return variable
, @v0 nvarchar(4000)-- running scratchpad for storing computed distances
, @start int = 1 -- index (1 based) of first non-matching character between the two string
, @i int, @j int -- loop counters: i for s string and j for t string
, @diag int -- distance in cell diagonally above and left if we were using an m by n matrix
, @left int -- distance in cell to the left if we were using an m by n matrix
, @sChar nchar -- character at index i from s string
, @thisJ int -- temporary storage of @j to allow SELECT combining
, @jOffset int -- offset used to calculate starting value for j loop
, @jEnd int -- ending value for j loop (stopping point for processing a column)
-- get input string lengths including any trailing spaces (which SQL Server would otherwise ignore)
, @sLen int = datalength(@s) / datalength(left(left(@s, 1) + '.', 1)) -- length of smaller string
, @tLen int = datalength(@t) / datalength(left(left(@t, 1) + '.', 1)) -- length of larger string
, @lenDiff int -- difference in length between the two strings
-- if strings of different lengths, ensure shorter string is in s. This can result in a little
-- faster speed by spending more time spinning just the inner loop during the main processing.
IF (@sLen > @tLen) BEGIN
SET @v0 = @s
SET @i = @sLen -- temporarily use v0 for swap
SET @s = @t
SET @sLen = @tLen
SET @t = @v0
SET @tLen = @i
END
SET @max = ISNULL(@max, @tLen)
SET @lenDiff = @tLen - @sLen
IF @lenDiff > @max RETURN NULL
-- suffix common to both strings can be ignored
WHILE(@sLen > 0 AND SUBSTRING(@s, @sLen, 1) = SUBSTRING(@t, @tLen, 1))
SET @sLen = @sLen - 1
SET @tLen = @tLen - 1
IF (@sLen = 0) RETURN @tLen
-- prefix common to both strings can be ignored
WHILE (@start < @sLen AND SUBSTRING(@s, @start, 1) = SUBSTRING(@t, @start, 1))
SET @start = @start + 1
IF (@start > 1) BEGIN
SET @sLen = @sLen - (@start - 1)
SET @tLen = @tLen - (@start - 1)
-- if all of shorter string matches prefix and/or suffix of longer string, then
-- edit distance is just the delete of additional characters present in longer string
IF (@sLen <= 0) RETURN @tLen
SET @s = SUBSTRING(@s, @start, @sLen)
SET @t = SUBSTRING(@t, @start, @tLen)
END
-- initialize v0 array of distances
SET @v0 = ''
SET @j = 1
WHILE (@j <= @tLen) BEGIN
SET @v0 = @v0 + NCHAR(CASE WHEN @j > @max THEN @max ELSE @j END)
SET @j = @j + 1
END
SET @jOffset = @max - @lenDiff
SET @i = 1
WHILE (@i <= @sLen) BEGIN
SET @distance = @i
SET @diag = @i - 1
SET @sChar = SUBSTRING(@s, @i, 1)
-- no need to look beyond window of upper left diagonal (@i) + @max cells
-- and the lower right diagonal (@i - @lenDiff) - @max cells
SET @j = CASE WHEN @i <= @jOffset THEN 1 ELSE @i - @jOffset END
SET @jEnd = CASE WHEN @i + @max >= @tLen THEN @tLen ELSE @i + @max END
WHILE (@j <= @jEnd) BEGIN
-- at this point, @distance holds the previous value (the cell above if we were using an m by n matrix)
SET @left = UNICODE(SUBSTRING(@v0, @j, 1))
SET @thisJ = @j
SET @distance =
CASE WHEN (@sChar = SUBSTRING(@t, @j, 1)) THEN @diag --match, no change
ELSE 1 + CASE WHEN @diag < @left AND @diag < @distance THEN @diag --substitution
WHEN @left < @distance THEN @left -- insertion
ELSE @distance -- deletion
END
END
SET @v0 = STUFF(@v0, @thisJ, 1, NCHAR(@distance))
SET @diag = @left
SET @j = case when (@distance > @max) AND (@thisJ = @i + @lenDiff)
then @jEnd + 2
else @thisJ + 1 end
END
SET @i = CASE WHEN @j > @jEnd + 1 THEN @sLen + 1 ELSE @i + 1 END
END
RETURN CASE WHEN @distance <= @max THEN @distance ELSE NULL END
END