Synonym dictionary

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 get all synonym groups from the dictionary, use the get_all_synonym_groups method.

To export synonyms to a file, use the export_dictionary method.

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.

from groupdocs.search import Index

# Creating or opening an index in the specified folder
index = Index("./index")

synonyms = index.dictionaries.synonym_dictionary

# Getting synonyms for the word 'make'; the letter case of the word is ignored
words = synonyms.get_synonyms("Make")
print("Synonyms for 'make':")
for word in words:
    print(word)

# Getting groups of synonyms to which the word 'make' belongs
groups = synonyms.get_synonym_groups("make")
print("Synonym groups for 'make':")
for group in groups:
    print(" ".join(group))

if synonyms.count > 0:
    # Removing all synonyms from the dictionary
    synonyms.clear()

# Adding synonyms to the dictionary
synonym_groups = [
    ["achieve", "accomplish", "attain", "reach"],
    ["accept", "take", "have"],
]
synonyms.add_range(synonym_groups)
print("Words in the dictionary:", synonyms.count)

# Exporting synonyms to a file
synonyms.export_dictionary("./Synonyms.dat")

# Importing synonyms from a file
synonyms.import_dictionary("./Synonyms.dat")
Synonyms for 'make':
brand
construct
build
cook
fix
ready
prepare
induce
stimulate
[TRUNCATED]

Download full output

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 the full-featured 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.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.