Search code examples
c#.netlevenshtein-distancemeasuresimilarity

How to calculate distance similarity measure of given 2 strings?


I need to calculate the similarity between 2 strings. So what exactly do I mean? Let me explain with an example:

  • The real word: hospital
  • Mistaken word: haspita

Now my aim is to determine how many characters I need to modify the mistaken word to obtain the real word. In this example, I need to modify 2 letters. So what would be the percent? I take the length of the real word always. So it becomes 2 / 8 = 25% so these 2 given string DSM is 75%.

How can I achieve this with performance being a key consideration?


Solution

  • What you are looking for is called edit distance or Levenshtein distance. The wikipedia article explains how it is calculated, and has a nice piece of pseudocode at the bottom to help you code this algorithm in C# very easily.

    Here's an implementation from the first site linked below:

    internal static int CalcLevenshteinDistance(string a, string b)
    {
        if (string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b))
        {
            return 0;
        }
    
        if (string.IsNullOrEmpty(a))
        {
            return b.Length;
        }
    
        if (string.IsNullOrEmpty(b))
        {
            return a.Length;
        }
    
        int lengthA = a.Length;
        int lengthB = b.Length;
        var distances = new int[lengthA + 1, lengthB + 1];
    
        for (int i = 0; i <= lengthA; distances[i, 0] = i++);
        for (int j = 0; j <= lengthB; distances[0, j] = j++);
    
        for (int i = 1; i <= lengthA; i++)
        {
            for (int j = 1; j <= lengthB; j++)
            {
                int cost = b[j - 1] == a[i - 1] ? 0 : 1;
                
                distances[i, j] = Math.Min(
                    Math.Min(distances[i - 1, j] + 1, distances[i, j - 1] + 1),
                    distances[i - 1, j - 1] + cost
                );
            }
        }
    
        return distances[lengthA, lengthB];
    }