The SpellingCorrector class is designed to correct spelling errors in search queries, as well as to store words with correct spelling. You can learn about spelling correction in search queries on the Spell checking page.
The spelling corrector dictionary can be changed at any time, also in an index that contains documents.
To get the number of words in the spelling corrector dictionary, use the count property.
To get a list of words contained in the spelling corrector dictionary, the get_words method is used.
To add words to the spelling corrector dictionary, use the add_range method.
The clear method is used to remove all words from the spelling corrector dictionary.
To import words 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 spelling corrector.
fromgroupdocs.searchimportIndex# Creating or opening an index in the specified folderindex=Index("./index")corrector=index.dictionaries.spelling_correctorifcorrector.count>0:# Removing all words from the dictionarycorrector.clear()# Adding words to the dictionarywords=["achieve","accomplish","attain","reach"]corrector.add_range(words)# Getting all words of the dictionaryall_words=corrector.get_words()print("Words in the dictionary:",len(all_words))# Exporting words to a filecorrector.export_dictionary("./Words.txt")# Importing words from a filecorrector.import_dictionary("./Words.txt")