Fastenshtein

repository·master·Indexed 43 days ago

https://github.com/danharltey/fastenshtein

A highly optimized .NET implementation of the Levenshtein distance algorithm designed for maximum speed and minimal memory allocation. It provides the static Fastenshtein.Levenshtein.Distance method for single comparisons and an instance-based DistanceFrom method for efficient bulk comparisons. It can also be integrated into Microsoft SQL Server as a CLR Scalar-Valued Function.

Tokens
844
Snippets
3
Records
4
Agent score
5%

What's inside Fastenshtein

  1. Include Fastenshtein in Microsoft SQL Server (SQLCLR)

    master

    You can use Fastenshtein as a CLR Scalar-Valued Function within SQL Server to perform fast Levenshtein calculations directly in your SQL queries.

    1. Enable CLR Integration

    Run the following to enable CLR on your server:

    sp_configure 'clr enabled', 1
    RECONFIGURE

    2. Configure Security (SQL Server 2017+)

    If using SQL Server 2017 (14.x) or later, you must either configure CLR strict security or disable it:

    EXEC sp_configure 'show advanced options', 1;
    RECONFIGURE;
    
    EXEC sp_configure 'clr strict security', 0;
    RECONFIGURE;

    3. Load the Assembly

    Fastenshtein requires the .NET Framework version 4.6.2. You can load it using one of two methods:

    Method A: Using Assembly Bits (Hex) Download the "Fastenshtein SQL Assembly Hex" from the latest release. Copy the contents of Fastenshtein_net462.hex into the following command:

    CREATE ASSEMBLY FastenshteinAssembly
    FROM 0x{contents of Fastenshtein_net462.hex}
    WITH PERMISSION_SET = SAFE;

    Method B: Using a Local Path or Network Location Place Fastenshtein.dll (compiled in Release config or extracted from the NuGet package's lib\net462 folder) in a directory accessible by SQL Server:

    CREATE ASSEMBLY FastenshteinAssembly FROM 'C:\Fastenshtein.dll' WITH PERMISSION_SET = SAFE

    4. Create the SQL Function

    Once the assembly is loaded, create the [Levenshtein] function:

    CREATE FUNCTION [Levenshtein](@value1 [nvarchar](MAX), @value2 [nvarchar](MAX))
    RETURNS [int]
    AS 
    EXTERNAL NAME [FastenshteinAssembly].[Fastenshtein.Levenshtein].[Distance]
    GO

    5. Usage

    DECLARE @retVal AS INTEGER
    SELECT @retVal = [dbo].[Levenshtein]('Test','test')
    SELECT @retVal
  2. Calculate Levenshtein distance with Fastenshtein.Levenshtein.Distance

    master

    To calculate the shortest possible distance between two strings (the number of insertions, deletions, and substitutions required to transform one string into another), use the static Distance method. This is the simplest way to perform a one-off comparison between two strings.

    int levenshteinDistance = Fastenshtein.Levenshtein.Distance("value1", "value2");
  3. Calculate Levenshtein distance between two strings

    master

    Use the static Fastenshtein.Levenshtein.Distance method to calculate the edit distance between two strings. This is the standard way to perform a single comparison.

    int levenshteinDistance = Fastenshtein.Levenshtein.Distance("value1", "value2");
  4. Compare one string against multiple items efficiently

    master

    When comparing a single source string against a large collection of target strings, instantiate a Fastenshtein.Levenshtein object with the source string. Then, use the DistanceFrom method for each target item. This approach is faster and results in less memory allocation than the static method, but note that the instance is not thread safe.

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