Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active January 9, 2022 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/a58dbce63ea1a37b97dbdb5b3a8a0a88 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/a58dbce63ea1a37b97dbdb5b3a8a0a88 to your computer and use it in GitHub Desktop.
Search Synonyms in Multiples Files and Folders using C#
// Printing the Synonym Search results in C#
Console.WriteLine("Query: " + query);
Console.WriteLine("Documents: " + result.DocumentCount);
Console.WriteLine("Total occurrences: " + result.OccurrenceCount + "\n");
for (int i = 0; i < result.DocumentCount; i++)
{
FoundDocument document = result.GetFoundDocument(i);
Console.WriteLine("Document: " + document.DocumentInfo.FilePath);
Console.WriteLine("Occurrences: " + document.OccurrenceCount);
for (int j = 0; j < document.FoundFields.Length; j++)
{
FoundDocumentField field = document.FoundFields[j];
Console.WriteLine("\tField: " + field.FieldName);
Console.WriteLine("\tOccurrences: " + document.OccurrenceCount);
// Printing found terms
if (field.Terms != null)
{
for (int k = 0; k < field.Terms.Length; k++)
{
Console.WriteLine("\t\t" + field.Terms[k].PadRight(20) + field.TermsOccurrences[k]);
}
}
}
}
// Search Synonyms in multiples files and folders and Print the results using C#
string query = "make";
string indexFolder = @"path\indexFolder";
string documentsFolder = @"path\documentsFolder";
// Creating an index in the specified folder
Index index = new Index(indexFolder);
// Indexing documents from the specified folder
index.Add(documentsFolder);
// Creating a search options object
SearchOptions options = new SearchOptions();
options.UseSynonymSearch = true; // Enabling synonym search
// Search for the word 'make'
// In addition to the word 'make', the words "do, cause, get, ..." will also be searched
SearchResult result = index.Search(query, options);
// Printing the result
Console.WriteLine("Query: " + query);
Console.WriteLine("Documents: " + result.DocumentCount);
Console.WriteLine("Total occurrences: " + result.OccurrenceCount + "\n");
for (int i = 0; i < result.DocumentCount; i++)
{
FoundDocument document = result.GetFoundDocument(i);
Console.WriteLine("Document: " + document.DocumentInfo.FilePath);
Console.WriteLine("Occurrences: " + document.OccurrenceCount);
for (int j = 0; j < document.FoundFields.Length; j++)
{
FoundDocumentField field = document.FoundFields[j];
Console.WriteLine("\tField: " + field.FieldName);
Console.WriteLine("\tOccurrences: " + document.OccurrenceCount);
// Printing found terms
if (field.Terms != null)
{
for (int k = 0; k < field.Terms.Length; k++)
{
Console.WriteLine("\t\t" + field.Terms[k].PadRight(20) + field.TermsOccurrences[k]);
}
}
}
}
// Search synonyms in multiples files and folders using C#
string query = "make";
string indexFolder = @"path\indexFolder";
string documentsFolder = @"path\documentsFolder";
// Creating an index in the specified folder
Index index = new Index(indexFolder);
index.Add(documentsFolder);
// Creating a search options object
SearchOptions options = new SearchOptions();
options.UseSynonymSearch = true; // Enabling Synonym Search
// Search for the word 'make'
// In addition to the word 'make', the synonyms "do, cause, get, ..." will also be searched
SearchResult result = index.Search(query, options);
Console.WriteLine("Query: " + query);
Console.WriteLine("Documents: " + result.DocumentCount);
Console.WriteLine("Occurrences: " + result.OccurrenceCount);
// Get groups of synonyms in C#
string query = "make";
string[][] groups = new Index().Dictionaries.SynonymDictionary.GetSynonymGroups(query);
Console.WriteLine("Synonyms for " + query + ":");
for (int i = 0; i < groups.Length; i++)
{
Console.Write("- ");
string[] group = groups[i];
for (int j = 0; j < group.Length; j++)
{
Console.Write(group[j] + " ");
}
Console.WriteLine();
}
// Get all the synonyms of any word in C#
string query = "make";
string[] synonyms = new Index().Dictionaries.SynonymDictionary.GetSynonyms(query);
Console.WriteLine("Synonyms for '" + query + "':");
for (int i = 0; i < synonyms.Length; i++)
{
Console.WriteLine("- " + synonyms[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment