Coder Social home page Coder Social logo

Comments (2)

doublehelix avatar doublehelix commented on July 22, 2024

I'm having the same issue...
According to the WIKI article (https://en.wikipedia.org/wiki/Levenshtein_distance) the implemented function is known to be a little inefficient.

There is a pseudocode example in the same article of a better implementation using a matrix.
I have implemented it and it returns in about 6ms with verified results.

public static int LevenshteinDistanceMatrix(this string source, string target)
{
    var sourceLength = source.Length;
    var targetLength = target.Length;

    var matrix = new int[sourceLength + 1, targetLength + 1];

    // First calculation, if one entry is empty return full length
    if (sourceLength == 0)
        return targetLength;

    if (targetLength == 0)
        return sourceLength;

    // Initialization of matrix with row size sourceLength and columns size targetLength
    for (var i = 0; i <= sourceLength; matrix[i, 0] = i++) { }
    for (var j = 0; j <= targetLength; matrix[0, j] = j++) { }

    // Calculate rows and collumns distances
    for (var i = 1; i <= sourceLength; i++)
    {
        for (var j = 1; j <= targetLength; j++)
        {
            var cost = (target[j - 1] == source[i - 1]) ? 0 : 1;
            matrix[i, j] = Math.Min(
                Math.Min(matrix[i - 1, j] + 1, matrix[i, j - 1] + 1),
                matrix[i - 1, j - 1] + cost);
        }
    }
    // return result
    return matrix[sourceLength, targetLength];
}

from fuzzystring.

caesay avatar caesay commented on July 22, 2024

levenshtien algo in this lib was taking 5+ second for me on such a 15 character string. other algorithms complete this same task in milliseconds. don't use this libs levenshtien algo...

from fuzzystring.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.