The SynonymDictionary class is designed to store synonyms in an index. For information on searching using synonyms, see the Synonym search page.
The synonym 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 synonyms 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 synonyms to the dictionary, use the AddRange method.
The Clear method is used to remove all synonyms from the dictionary.
The GetSynonyms method is used to get a list of synonyms for a given word. The returned list does not contain the word itself.
The GetSynonymGroups method is used to get all synonym groups to which a given word belongs.
To get all synonym groups from the dictionary, use the GetAllSynonymGroups method.
To import synonyms from a file, use the ImportDictionary method. Errors of exporting and importing are reported by the ErrorOccurred event of the index instead of exceptions.
The following example demonstrates the use of methods of the synonym dictionary.
C#
stringindexFolder=@"c:\MyIndex\";// Creating or opening an index in the specified folderIndexindex=newIndex(indexFolder);// Getting synonyms for the word 'make'; the letter case of the word is ignoredstring[]synonyms=index.Dictionaries.SynonymDictionary.GetSynonyms("Make");Console.WriteLine("Synonyms for 'make':");for(inti=0;i<synonyms.Length;i++){Console.WriteLine(synonyms[i]);}// Getting groups of synonyms to which the word 'make' belongsstring[][]groups=index.Dictionaries.SynonymDictionary.GetSynonymGroups("make");Console.WriteLine("Synonym groups for 'make':");for(inti=0;i<groups.Length;i++){string[]group=groups[i];for(intj=0;j<group.Length;j++){Console.Write(group[j]+" ");}Console.WriteLine();}if(index.Dictionaries.SynonymDictionary.Count>0){// Removing all synonyms from the dictionaryindex.Dictionaries.SynonymDictionary.Clear();}// Adding synonyms to the dictionarystring[][]synonymGroups=newstring[][]{newstring[]{"achieve","accomplish","attain","reach"},newstring[]{"accept","take","have"},};index.Dictionaries.SynonymDictionary.AddRange(synonymGroups);// Exporting synonyms to a fileindex.Dictionaries.SynonymDictionary.ExportDictionary(@"c:\Synonyms.dat");// Importing synonyms from a fileindex.Dictionaries.SynonymDictionary.ImportDictionary(@"c:\Synonyms.dat");
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: