- You don’t know what color or colour spelling was used in the document.
- Searching for “John“, but it could have been spelled as “Jon” or maybe “Jhon“.
- Locating “USA” when the user inputs “U.S.A“.
- The word or phrase you are looking for could have a “mistaek“, oops it’s again a “mistake“.
Here comes the Fuzzy Search. Fuzzy search lets you find approximate matches rather than exact matches in data, allowing for flexibility in search queries. This is particularly useful in scenarios with typos, misspellings, or variations in the data. This article shows how to programmatically perform a fuzzy search in multiple documents across folders using C#.
C# Fuzzy Search Library
For Fuzzy Search, we’ll utilize the GroupDocs.Search for .NET API. This allows for a certain degree of tolerance in spelling, making it effective in accommodating regional language variations like those between British and American English.
This library empowers us to programmatically search text in a wide range of file formats, such as Word documents (DOC, DOCX), spreadsheets (XLS, XLSX), presentations (PPT, PPTX), PDF files, Markup (HTML, XML), Markdown (MD), eBooks (EPUB, CHM, FB2), email messages (MSG, EML), OneNote documents, and ZIP archives.
To find out all the types of files you can work with, check out the documentation.
You can download the API from the releases page or add it to your .NET program using NuGet. The code examples in this article use the API of GroupDocs.Search for .NET 26.10 and the namespaces GroupDocs.Search, GroupDocs.Search.Common, GroupDocs.Search.Highlighters, GroupDocs.Search.Options and GroupDocs.Search.Results.
PM> Install-Package GroupDocs.Search
Let’s Fuzzy Search in Files using C#
Follow these steps to perform a fuzzy search in multiple files of various file formats within folders using C#:
- Create an Index by providing a folder path.
- Add the parent folder path for the search directory to the created index.
- Define the search query.
- Activate the Fuzzy Search by enabling the option.
- Set the Similarity Level in the Fuzzy Algorithm as required.
- Execute the search using the Search method to get the search results.
- Now, you can iterate on the SearchResult to create or print the output as you like.
The fuzzy search in the below-mentioned C# code finds the approximate matches of the given query in all the files across all sub folders with 40% error tolerance in spellings.
string indexFolder = @"c:\MyIndex\"; // Specify path to the index folder
string documentsFolder = @"c:\MyDocuments\"; // Specify path to the parent folder with documents
string query = "nulla"; // Specify the search query
// Creating an index in the specified folder
Index index = new Index(indexFolder);
// Indexing the parent folder; documents in the subfolders are indexed as well
index.Add(documentsFolder);
// Creating a search options instance
SearchOptions options = new SearchOptions();
options.FuzzySearch.Enabled = true; // Enabling the fuzzy search
options.FuzzySearch.FuzzyAlgorithm = new SimilarityLevel(0.6); // Setting the similarity level to 60 %
// Searching in the index
SearchResult result = index.Search(query, options);
Here, the similarity level is set to 0.6 i.e. 60% match which corresponds to 40% error tolerance. The maximum number of mistakes in a word is calculated as (int)((1 - similarityLevel) * wordLength), so for the five-letter word “nulla” up to two mistakes (insertions, deletions, substitutions or transpositions of adjacent characters) are allowed. You can adjust the tolerance level by tweaking the similarity level in the code. Since version 26.10, fuzzy search uses all processor cores by default, so the order of the found words may differ from run to run. To get the same order every time, set the SearchThreads index setting to NumberOfThreads.One.
Below are the fuzzy search results you can obtain from the above code. It is quite simple, however, the printing code is also available later in this article.
Query: nulla
Documents: 2
Occurrences: 135
Document: Lorem ipsum.docx
Occurrences: 132
Field: content
Occurrences: 132
nulla 98
nullam 34
Document: EnglishText.txt
Occurrences: 3
Field: content
Occurrences: 3
dull 1
full 1
fully 1
Printing Search Results
The following C# code provides two ways to present your search results.
- Highlight all the approximate matches.
- Print the results in a readable and analyzable format
string outputFolder = @"c:\Output\"; // Specify path to the folder for the HTML files
// The 'index', 'query' and 'result' objects are taken from the previous example
Directory.CreateDirectory(outputFolder);
// Printing the summary
Console.WriteLine("Query: " + query);
Console.WriteLine(" Documents: " + result.DocumentCount);
Console.WriteLine(" Occurrences: " + result.OccurrenceCount);
for (int i = 0; i < result.DocumentCount; i++)
{
FoundDocument document = result.GetFoundDocument(i);
// 1. Highlighting the approximate matches in an HTML file named after the document
string outputPath = Path.Combine(outputFolder, Path.GetFileName(document.DocumentInfo.FilePath) + ".html");
FileOutputAdapter outputAdapter = new FileOutputAdapter(OutputFormat.Html, outputPath);
Highlighter highlighter = new DocumentHighlighter(outputAdapter);
index.Highlight(document, highlighter);
// 2. Printing the results in a readable format
Console.WriteLine();
Console.WriteLine(" Document: " + Path.GetFileName(document.DocumentInfo.FilePath));
Console.WriteLine(" Occurrences: " + document.OccurrenceCount);
for (int j = 0; j < document.FoundFields.Length; j++)
{
FoundDocumentField field = document.FoundFields[j];
Console.WriteLine(" Field: " + field.FieldName);
Console.WriteLine(" Occurrences: " + field.OccurrenceCount);
for (int k = 0; k < field.Terms.Length; k++)
{
Console.WriteLine(" " + field.Terms[k].PadRight(20) + field.TermsOccurrences[k]);
}
}
}
Getting a Free License or a Free Trial
Free License
Obtain a temporary license for free to explore this library without constraints.
Free Trial
You can download the free trial from the releases page.
Conclusion
In this article, we looked into the C# programmatic approach to the magic of Fuzzy search to find all the approximate matching words but with a certain degree of error tolerance. This feature makes the fuzzy search effective in accommodating regional language variations like those between British and American English, typos in text, name variations, and phonetic matching.
For comprehensive details about the API, readers are advised to refer to the documentation.
Any queries or additional discussions can be directed to the forum.