# Fuzzy Search

, 2 Jun 2009 CPOL
 Rate this:
A simple implementation of the fuzzy string search.

## Introduction

This article describes a simple implementation of the fuzzy (string) search. It can be used for approximate string matching (for more information, see http://en.wikipedia.org/wiki/Fuzzy_string_searching).

Other algorithms for approximate string searching exist (e.g., Soundex), but those aren't as easy to implement. The algorithm in this article is easy to implement, and can be used for tasks where approximate string searching is used in an easy way.

A `List<string>` is used for searching, and therefore it's quite easy to search a database.

## Background

The algorithm used the Levenshtein-distance for determining how exact a string from a word list matches the word to be found. Information about the Levenshtein-distance can be found at http://en.wikipedia.org/wiki/Levenshtein_distance.

## Using the code

The following example will show how simply the class can be used.

```static void Main(string[] args)
{
string word = "Code Project";
List<string> wordList = new List<string>
{
"Code Project",
"Code project",
"codeproject",
"Code Projekt",
"Kode Project",
"Other Project"
};

List<string> foundWords = FuzzySearch.Search(
word,
wordList,
0.70);

foundWords.ForEach(i => Console.WriteLine(i));
}```

Output:

```Code Project
Code project
codeproject
Code Projekt
Kode Project```

## Implementation

A basic approach is shown. Instead of the Levenshtein-distance, a more optimized algorithm could be used - but here, a quite simple implementation is given for clarity reasons.

### Levenshtein-distance

For computing the Levenshtein-distance, I use the following algorithm:

```public static int LevenshteinDistance(string src, string dest)
{
int[,] d = new int[src.Length + 1, dest.Length + 1];
int i, j, cost;
char[] str1 = src.ToCharArray();
char[] str2 = dest.ToCharArray();

for (i = 0; i <= str1.Length; i++)
{
d[i, 0] = i;
}
for (j = 0; j <= str2.Length; j++)
{
d[0, j] = j;
}
for (i = 1; i <= str1.Length; i++)
{
for (j = 1; j <= str2.Length; j++)
{

if (str1[i - 1] == str2[j - 1])
cost = 0;
else
cost = 1;

d[i, j] =
Math.Min(
d[i - 1, j] + 1,              // Deletion
Math.Min(
d[i, j - 1] + 1,          // Insertion
d[i - 1, j - 1] + cost)); // Substitution

if ((i > 1) && (j > 1) && (str1[i - 1] ==
str2[j - 2]) && (str1[i - 2] == str2[j - 1]))
{
d[i, j] = Math.Min(d[i, j], d[i - 2, j - 2] + cost);
}
}
}

return d[str1.Length, str2.Length];
}```

### The Searching

In the search process, for each word in the wordlist, the Levenshtein-distance is computed, and with this distance, a score. This score represents how good the strings match. The input argument `fuzzyness` determines how much the strings can differ.

```public static List<string> Search(
string word,
List<string> wordList,
double fuzzyness)
{
List<string> foundWords = new List<string>();

foreach (string s in wordList)
{
// Calculate the Levenshtein-distance:
int levenshteinDistance =
LevenshteinDistance(word, s);

// Length of the longer string:
int length = Math.Max(word.Length, s.Length);

// Calculate the score:
double score = 1.0 - (double)levenshteinDistance / length;

// Match?
if (score > fuzzyness)
}
return foundWords;
}```

#### LINQ-variant

This piece of code could be written in LINQ too.

```public static List<string> Search(
string word,
List<string> wordList,
double fuzzyness)
{
// Tests have prove that the !LINQ-variant is about 3 times
// faster!
List<string> foundWords =
(
from s in wordList
let levenshteinDistance = LevenshteinDistance(word, s)
let length = Math.Max(s.Length, word.Length)
let score = 1.0 - (double)levenshteinDistance / length
where score > fuzzyness
select s
).ToList();

return foundWords;
}```

## History

• 2009 June 1st: Initial release.

## Share

Software Developer (Senior) Foidl Günther
Austria
Engineer in combustion engine development.
Programming languages: C#, FORTRAN 95, Matlab

FIS-overall worldcup winner in Speedski (Downhill) 2008/09 and 2009/10.

 First Prev Next
 qwestion scheee 17-Jan-13 2:42
 Parallelize may help but it is quite limited Member 9575148 5-Nov-12 19:29
 My vote of 5 Petr Abdulin 18-Apr-12 20:36
 My vote of 5 Arlen Navasartian 9-Apr-12 9:26
 My vote of 5 manoj kumar choubey 26-Feb-12 22:32
 Works Great ! damon88 10-Dec-09 20:27
 Re: Works Great ! Günther M. FOIDL 29-Dec-09 14:33
 Re: About dictionary Günther M. FOIDL 22-Oct-09 11:55
 great explanation Win32nipuh 8-Jun-09 21:29
 Re: great explanation Günther M. FOIDL 9-Jun-09 15:31
 Re: great explanation coduresearch 2-Sep-09 8:45
 Re: great explanation Günther M. FOIDL 10-Sep-09 7:22
 Re: great explanation Nicolas Dorier 21-Oct-09 9:28
 Searching large text daniel.zolnjan 4-Jun-09 6:56
 Re: Searching large text Günther M. FOIDL 4-Jun-09 10:25
 Linq does not seam to be slower [modified] Moshe Plotkin 1-Jun-09 14:55
 Re: Linq does not seam to be slower Günther M. FOIDL 1-Jun-09 22:44
 Re: Linq does not seam to be slower Moshe Plotkin 2-Jun-09 3:15
 on the contrary, I find that linq/extension methods and the other enhancements they are adding to c# actually abstract away the complexities and allow the concepts to be presented in a less cluttered way.   but anyway, the only thing you should remove from the actual article is the line saying that you tested linq and it was slower. Why would you have extraneous comments in your article that are wrong AND have nothing to do with your presentation?
 Re: Linq does not seam to be slower Günther M. FOIDL 2-Jun-09 4:14
 Re: Linq does not seam to be slower riskka 10-Jun-09 3:28
 more optimized algorithm? Unruled Boy 1-Jun-09 4:11
 Re: more optimized algorithm? Günther M. FOIDL 1-Jun-09 4:22
 Re: more optimized algorithm? gstolarov 10-Jun-09 13:18
 Re: more optimized algorithm? Psycho_Coder 22-May-14 20:10
 Last Visit: 31-Dec-99 19:00     Last Update: 25-Dec-14 3:40 Refresh 1