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>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("e",StringComparison.InvariantCultureIgnoreCase)){result.Add(word+"s");}if(word.Length>1&&word.EndsWith("y",StringComparison.InvariantCultureIgnoreCase)){result.Add(word.Substring(0,word.Length-1)+"is");}result.Add(word+"es");// 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. An example of setting a custom provider of word forms is presented below.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating 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: