The StopWordDictionary class is designed to store stop words in an index. Stop words are excluded from indexing only if the use_stop_words index setting is enabled. Information on using stop words during indexing is provided on the Indexing with stop words page.
Note
Stop words define which terms of documents are indexed. Therefore, the stop word dictionary and the use_stop_words setting of an index can be changed only while the index contains no documents and is not performing an operation; otherwise, a change raises RuntimeError. A call that does not change the content of the dictionary is allowed. Configure stop words after creating a new index and before adding documents. See also the Index format and compatibility page.
The dictionary keeps words in lower case without leading and trailing white space. Therefore, the letter case of words does not matter when words are added, removed or checked.
To get the number of stop words in the dictionary, use the count property.
To add stop words to the dictionary, use the add_range method.
To remove words from the dictionary, use the remove_range method.
To check for a word in the dictionary, use the contains method. The method ignores the letter case of the word.
To remove all words from the dictionary, use the clear method.
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 stop word dictionary.
fromgroupdocs.searchimportIndex,IndexSettings# Creating an index settings instancesettings=IndexSettings()settings.use_stop_words=True# Enabling the use of stop words during indexing# Creating a new index in the specified folder; the dictionary can be changed only while the index contains no documentsindex=Index("./index",settings,True)stop_words=index.dictionaries.stop_word_dictionaryifstop_words.count>0:# Removing all words from the dictionarystop_words.clear()# Adding stop words to the dictionarywords=["a","an","the","but","by"]stop_words.add_range(words)# The letter case of words is ignoredifstop_words.contains("But")andstop_words.contains("BY"):# Removing words from the dictionarystop_words.remove_range(["but","by"])# Exporting words to a filestop_words.export_dictionary("./StopWords.txt")# Importing words from a filestop_words.import_dictionary("./StopWords.txt")print("Stop words:",stop_words.count)# Indexing documents; after that, the stop words of the index cannot be changedindex.add("./documents")