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 add_range method.
The clear method is used to remove all synonyms from the dictionary.
The get_synonyms method is used to get a list of synonyms for a given word. The returned list does not contain the word itself.
The get_synonym_groups method is used to get all synonym groups to which a given word belongs.
To import synonyms from a file, use the import_dictionary method. Errors of exporting and importing are reported by the error_occurred event of the index instead of exceptions.
The following example demonstrates the use of methods of the synonym dictionary.
fromgroupdocs.searchimportIndex# Creating or opening an index in the specified folderindex=Index("./index")synonyms=index.dictionaries.synonym_dictionary# Getting synonyms for the word 'make'; the letter case of the word is ignoredwords=synonyms.get_synonyms("Make")print("Synonyms for 'make':")forwordinwords:print(word)# Getting groups of synonyms to which the word 'make' belongsgroups=synonyms.get_synonym_groups("make")print("Synonym groups for 'make':")forgroupingroups:print(" ".join(group))ifsynonyms.count>0:# Removing all synonyms from the dictionarysynonyms.clear()# Adding synonyms to the dictionarysynonym_groups=[["achieve","accomplish","attain","reach"],["accept","take","have"],]synonyms.add_range(synonym_groups)print("Words in the dictionary:",synonyms.count)# Exporting synonyms to a filesynonyms.export_dictionary("./Synonyms.dat")# Importing synonyms from a filesynonyms.import_dictionary("./Synonyms.dat")