Homophone dictionary

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Search MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-search -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-search/search-net-mcp:latest
Folders, licensing and more →

The HomophoneDictionary class is designed to store homophones in an index. For information on searching using homophones, see the Homophone search page.

The homophone dictionary can be changed at any time, also in an index that contains documents. The dictionary keeps words in lower case without leading and trailing white space, so the methods that get homophones ignore the letter case of the specified word.

To get the number of words in the dictionary, use the Count property.

To add groups of homophones to the dictionary, use the AddRange method. The method takes a collection of groups (IEnumerable<string[]>), in which each group is an array of at least two words, for example, an array of string arrays (string[][]), as in the example below.

Since version 26.11, a word that is empty or consists of white space and a repeated word of a group are left out, so a group that is left with one word adds nothing. The method throws ArgumentException for a word that is null and for a word that contains a line break or an unpaired surrogate (half of a surrogate pair), which the dictionary cannot save. The groups are checked before the dictionary is changed: a call that throws an exception adds nothing.

The Clear method is used to remove all homophones from the dictionary.

The GetHomophones method is used to get a list of homophones for a given word. The returned list does not contain the word itself. Since version 26.11, the method returns a copy, so a change of the returned array does not change the dictionary.

The GetHomophoneGroups method is used to get all homophone groups to which a given word belongs.

To get all homophone groups from the dictionary, use the GetAllHomophoneGroups method.

To export homophones to a file, use the ExportDictionary method.

To import homophones from a file, use the ImportDictionary method. Errors of exporting and importing are reported by the ErrorOccurred event of the index instead of exceptions. Since version 26.11, an import that cannot be completed does not change the dictionary.

The following example demonstrates the use of methods of the homophone dictionary.

C#

string indexFolder = @"c:\MyIndex\";

// Creating or opening an index in the specified folder
Index index = new Index(indexFolder);

// Getting homophones for the word 'braid'; the letter case of the word is ignored
string[] homophones = index.Dictionaries.HomophoneDictionary.GetHomophones("Braid");
Console.WriteLine("Homophones for 'braid':");
for (int i = 0; i < homophones.Length; i++)
{
    Console.WriteLine(homophones[i]);
}

// Getting groups of homophones to which the word 'braid' belongs
string[][] groups = index.Dictionaries.HomophoneDictionary.GetHomophoneGroups("braid");
Console.WriteLine("Homophone groups for 'braid':");
for (int i = 0; i < groups.Length; i++)
{
    string[] group = groups[i];
    for (int j = 0; j < group.Length; j++)
    {
        Console.Write(group[j] + " ");
    }
    Console.WriteLine();
}

if (index.Dictionaries.HomophoneDictionary.Count > 0)
{
    // Removing all homophones from the dictionary
    index.Dictionaries.HomophoneDictionary.Clear();
}

// Adding homophones to the dictionary
string[][] homophoneGroups = new string[][]
{
    new string[] { "awe", "oar", "or", "ore" },
    new string[] { "aye", "eye", "i" },
};
index.Dictionaries.HomophoneDictionary.AddRange(homophoneGroups);

// Exporting homophones to a file
index.Dictionaries.HomophoneDictionary.ExportDictionary(@"c:\Homophones.dat");

// Importing homophones from a file
index.Dictionaries.HomophoneDictionary.ImportDictionary(@"c:\Homophones.dat");

More resources

GitHub examples

You may easily run the code from documentation articles and see the features in action in our GitHub examples:

Free online document search App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.