Home

Awesome

Fastenshtein

NuGet GitHub action build AppVeyor Build License Unit test coverage

One of the fastest .Net Levenshtein projects around.

Fastenshtein is an optimized and fully unit tested Levenshtein implementation. It is optimized for speed and memory usage.

From the included brenchmarking tests comparing random words of 3 to 20 random chars to other Nuget Levenshtein implementations.

MethodMeanRatioRankGen0AllocatedAlloc Ratio
Fastenshtein1.077 ms1.001-6345 B1.000
FastenshteinStatic1.122 ms1.0423.9063265441 B41.835
NinjaNye1.899 ms1.76476.17194274593 B673.695
StringSimilarity2.899 ms2.6957.8125543770 B85.701
FuzzyStringsNetStandard7.351 ms6.816414.062522967283 B3,619.745

Usage

int levenshteinDistance = Fastenshtein.Levenshtein.Distance("value1", "value2");

Alternative method for comparing one item against many (quicker due to less memory allocation, not thread safe)

Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein("value1");
foreach (var item in new []{ "value2", "value3", "value4"})
{
	int levenshteinDistance = lev.DistanceFrom(item);
}

How to include Fastenshtein in Microsoft SQL Server (SQLCLR)

We will create Fastenshtein as a CLR Scalar-Valued Function within SQL Server. This will allow the fast Levenshtein implementationto be used within SQL Server.

  1. To enable CLR integration for the server:
sp_configure 'clr enabled', 1
RECONFIGURE
  1. Beginning with SQL Server 2017 (14.x). Either configure CLR strict security or run the below to disable it.
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;

EXEC sp_configure 'clr strict security', 0;
RECONFIGURE;
  1. Place the Fastenshtein.dll on the same computer as the SQL Server instance in a directory that the SQL Server instance has access to. You must use the .Net framework version 4.5.2 of Fastenshtein. To create the assembly (dll) either:

OR

  1. Create the assembly
CREATE ASSEMBLY FastenshteinAssembly FROM 'C:\Fastenshtein.dll' WITH PERMISSION_SET = SAFE
  1. Then create the function
CREATE FUNCTION [Levenshtein](@value1 [nvarchar](MAX), @value2 [nvarchar](MAX))
RETURNS [int]
AS 
EXTERNAL NAME [FastenshteinAssembly].[Fastenshtein.Levenshtein].[Distance]
GO
  1. It is now ready to be used:
-- Usage
DECLARE @retVal as integer
select @retVal = [dbo].[Levenshtein]('Test','test')
Select @retVal