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 GetWordForms method, which returns various forms for the word passed as an argument. An example implementation of a simple provider of word forms is presented below.
C#
publicclassSimpleWordFormsProvider:IWordFormsProvider{publicstring[]GetWordForms(stringword){List<string>result=newList<string>();// We assume that the input word is in the plural, then we add the singularif(word.Length>3&&word.EndsWith("ies",StringComparison.InvariantCultureIgnoreCase)){result.Add(word.Substring(0,word.Length-3)+"y");}if(word.Length>2&&word.EndsWith("es",StringComparison.InvariantCultureIgnoreCase)){result.Add(word.Substring(0,word.Length-2));}if(word.Length>1&&word.EndsWith("s",StringComparison.InvariantCultureIgnoreCase)){result.Add(word.Substring(0,word.Length-1));}// Then we assume that the input word is in the singular, we add the pluralif(word.Length>1&&word.EndsWith("y",StringComparison.InvariantCultureIgnoreCase)){result.Add(word.Substring(0,word.Length-1)+"ies");}elseif(word.EndsWith("s",StringComparison.InvariantCultureIgnoreCase)||word.EndsWith("x",StringComparison.InvariantCultureIgnoreCase)||word.EndsWith("ch",StringComparison.InvariantCultureIgnoreCase)||word.EndsWith("sh",StringComparison.InvariantCultureIgnoreCase)){result.Add(word+"es");}else{result.Add(word+"s");}// All rules are implemented in the EnglishWordFormsProvider classreturnresult.ToArray();}}
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 WordFormsProvider 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.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating or opening an index in the specified folderIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentsFolder);// Setting the custom word forms provider instanceindex.Dictionaries.WordFormsProvider=newSimpleWordFormsProvider();// Creating a search options instanceSearchOptionsoptions=newSearchOptions();options.UseWordFormsSearch=true;// Enabling search for word forms// Searching in the indexSearchResultresult=index.Search("relative",options);// The following words can be found:// relative// relatives
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: