Search code examples
c#.netfuzzy-search

How to fuzzy search for a specific person into a list of people (concatenated surname+lastname)


I am trying to match an input (coming from a third-party software) like this:

PIPPO CANASTA PER FT 501 del 1/11/2016

against a list of people that can be modelized as an array of strings (coming from another software)

[
  ...
  "CANASTA              PIPPO"
  ...
]

How can I accomplish this using C# (.NET)?


Solution

  • You can split each string into an array of words and search the list for the most number of matching elements:

    string[] arrayToSearch = new string[] {
        "OTHER STUFF",
        "CANASTA              PIPPO",
        "MORE STUFF"
    };
    
    string stringToFind = "PIPPO CANASTA PER  FT 501 del 1/11/2016";
    
    string[] wordsToFind = stringToFind.Split(default(Char[]), StringSplitOptions.RemoveEmptyEntries);
    
    string bestMatch = arrayToSearch.OrderByDescending(
        s => s.Split(default(Char[]), StringSplitOptions.RemoveEmptyEntries)
              .Intersect(wordsToFind, StringComparer.OrdinalIgnoreCase)
              .Count()
    ).FirstOrDefault();
    
    Console.WriteLine("Best match: " + bestMatch);
    Console.ReadKey();