65

What is the best way to compare two strings to see how similar they are?

Examples:

My String
My String With Extra Words

Or

My String
My Slightly Different String

What I am looking for is to determine how similar the first and second string in each pair is. I would like to score the comparison and if the strings are similar enough, I would consider them a matching pair.

Is there a good way to do this in C#?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Brandon
  • 10,744
  • 18
  • 64
  • 97
  • 3
    Levenshtein edit distance, Soundex, and Hamming distance all do this in different ways. You'll need to better define your metric before you can find an implementation. – bmm6o Aug 04 '11 at 15:25
  • 3
    For anyone else stumbling into this question: consider https://github.com/DanHarltey/Fastenshtein – Mugen Nov 07 '19 at 12:37
  • related: https://stackoverflow.com/questions/83777/are-there-any-fuzzy-search-or-string-similarity-functions-libraries-written-for-c – StayOnTarget Jan 21 '20 at 18:53

3 Answers3

109
static class LevenshteinDistance
{
    public static int Compute(string s, string t)
    {
        if (string.IsNullOrEmpty(s))
        {
            if (string.IsNullOrEmpty(t))
                return 0;
            return t.Length;
        }

        if (string.IsNullOrEmpty(t))
        {
            return s.Length;
        }

        int n = s.Length;
        int m = t.Length;
        int[,] d = new int[n + 1, m + 1];

        // initialize the top and right of the table to 0, 1, 2, ...
        for (int i = 0; i <= n; d[i, 0] = i++);
        for (int j = 1; j <= m; d[0, j] = j++);

        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
                int min1 = d[i - 1, j] + 1;
                int min2 = d[i, j - 1] + 1;
                int min3 = d[i - 1, j - 1] + cost;
                d[i, j] = Math.Min(Math.Min(min1, min2), min3);
            }
        }
        return d[n, m];
    }
}
decden
  • 679
  • 4
  • 19
Marty Neal
  • 8,741
  • 3
  • 33
  • 38
  • 5
    This was going to be my answer. The Damereau-Levenshein Distance algorithm calculates the number of letter additions, subtractions, substitutions, and transpositions (swaps) necessary to convert one string to another. The lower the score, the more similar they are. – KeithS Aug 04 '11 at 15:27
  • 1
    It should be noted that this approach is very memory-intensive even for medium-sized strings. There’s an easy fix that requires only `min(n, m) + 1` extra memory. – Konrad Rudolph Aug 04 '11 at 15:29
  • 1
    This worked great. Luckily all of my strings are very short (50 characters or less), so it processes very quickly for me. – Brandon Aug 04 '11 at 17:46
  • 1
    Faster implementation is here: http://web.archive.org/web/20120526085419/http://www.merriampark.com/ldjava.htm. Some tests I was running went from 30-50 seconds to 8-10 seconds. – Frank Schwieterman Sep 13 '19 at 15:49
  • @FrankSchwieterman Instead of the full matrix, only store the previous column vector, as well as the single field corresponding to the previous row of the current coloumn, `prev` (hence +1). At a given row *i*, all values from 0–(*i*-1) in the vector correspond to the updated values. That is, the assignment in the loop reads `prev = d[i]; d[i] = Math.Min(…);`. It’s worth noting that this is *better* than the implementation you’ve linked to in your updated comment. – Konrad Rudolph Sep 13 '19 at 15:50
11

If anyone was wondering what the C# equivalent of what @FrankSchwieterman posted is:

public static int GetDamerauLevenshteinDistance(string s, string t)
{
    if (string.IsNullOrEmpty(s))
    {
        throw new ArgumentNullException(s, "String Cannot Be Null Or Empty");
    }

    if (string.IsNullOrEmpty(t))
    {
        throw new ArgumentNullException(t, "String Cannot Be Null Or Empty");
    }

    int n = s.Length; // length of s
    int m = t.Length; // length of t

    if (n == 0)
    {
        return m;
    }

    if (m == 0)
    {
        return n;
    }

    int[] p = new int[n + 1]; //'previous' cost array, horizontally
    int[] d = new int[n + 1]; // cost array, horizontally

    // indexes into strings s and t
    int i; // iterates through s
    int j; // iterates through t

    for (i = 0; i <= n; i++)
    {
        p[i] = i;
    }

    for (j = 1; j <= m; j++)
    {
        char tJ = t[j - 1]; // jth character of t
        d[0] = j;

        for (i = 1; i <= n; i++)
        {
            int cost = s[i - 1] == tJ ? 0 : 1; // cost
            // minimum of cell to the left+1, to the top+1, diagonally left and up +cost                
            d[i] = Math.Min(Math.Min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
        }

        // copy current distance counts to 'previous row' distance counts
        int[] dPlaceholder = p; //placeholder to assist in swapping p and d
        p = d;
        d = dPlaceholder;
    }

    // our last action in the above loop was to switch d and p, so p now 
    // actually has the most recent cost counts
    return p[n];
}
  • I think that s or t could be null or empty as then the difference would be 100% or none if both are the same. I would also do an equal to see if they are the same right at the beginning – Walter Verhoeven Sep 16 '20 at 18:31
2

I am comparing two sentences like this

string[] vs = string1.Split(new char[] { ' ', '-', '/', '(', ')' },StringSplitOptions.RemoveEmptyEntries);
string[] vs1 = string2.Split(new char[] { ' ', '-', '/', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);


vs.Intersect(vs1, StringComparer.OrdinalIgnoreCase).Count();

Intersect gives you a set of identical word lists , I continue by looking at the count and saying if it is more than 1, these two sentences contain similar words.

yhackup
  • 189
  • 2
  • 15