Word forms provider

The IWordFormsProvider interface is designed to implement a word forms provider for searching by word forms. For information on searching by word forms, see the Search for different word forms page.

The IWordFormsProvider interface contains only one get_word_forms method, which returns various forms for the word passed as an argument. In Python, a provider is a plain class with the get_word_forms(word) method that returns a list of strings; a function that takes the word also works. An example implementation of a simple provider of word forms is presented below.

class SimpleWordFormsProvider:
    def get_word_forms(self, word):
        result = []
        lower_word = word.lower()

        # We assume that the input word is in the plural, then we add the singular
        if len(word) > 3 and lower_word.endswith("ies"):
            result.append(word[:-3] + "y")
        if len(word) > 2 and lower_word.endswith("es"):
            result.append(word[:-2])
        if len(word) > 1 and lower_word.endswith("s"):
            result.append(word[:-1])

        # Then we assume that the input word is in the singular, we add the plural
        if len(word) > 1 and lower_word.endswith("y"):
            result.append(word[:-1] + "ies")
        elif lower_word.endswith(("s", "x", "ch", "sh")):
            result.append(word + "es")
        else:
            result.append(word + "s")
        # All rules are implemented in the EnglishWordFormsProvider class

        return result

By default, the EnglishWordFormsProvider class is used, which for English generates various forms of nouns, adjectives, pronouns, verbs, etc. A custom provider is set with the word_forms_provider property of the Index.dictionaries object. The provider can be replaced at any time; it is not saved in the index, so it must be set each time the index is opened. An example of setting a custom provider of word forms is presented below.

from groupdocs.search import Index
from groupdocs.search.options import SearchOptions


class SimpleWordFormsProvider:
    def get_word_forms(self, word):
        result = []
        lower_word = word.lower()

        # We assume that the input word is in the plural, then we add the singular
        if len(word) > 3 and lower_word.endswith("ies"):
            result.append(word[:-3] + "y")
        if len(word) > 2 and lower_word.endswith("es"):
            result.append(word[:-2])
        if len(word) > 1 and lower_word.endswith("s"):
            result.append(word[:-1])

        # Then we assume that the input word is in the singular, we add the plural
        if len(word) > 1 and lower_word.endswith("y"):
            result.append(word[:-1] + "ies")
        elif lower_word.endswith(("s", "x", "ch", "sh")):
            result.append(word + "es")
        else:
            result.append(word + "s")

        return result


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

# Indexing documents from the specified folder
index.add("./documents")

# Setting the custom word forms provider instance
index.dictionaries.word_forms_provider = SimpleWordFormsProvider()

# Creating a search options instance
options = SearchOptions()
options.use_word_forms_search = True  # Enabling search for word forms

# Searching in the index
result = index.search("relative", options)

# The following words can be found:
# relative
# relatives
for document in result:
    print(document.document_info.file_path)
    print("\tFound words:", ", ".join(document.terms))

The following sample files are used in this example: documents/einstein-notes.txt, documents/newton.txt, documents/curie.txt

documents\einstein-notes.txt
	Found words: relative, relatives

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.