The following example demonstrates how to implement a custom word forms provider.
publicclassSimpleWordFormsProvider:IWordFormsProvider{publicstring[]GetWordForms(stringword){List<string>result=newList<string>();// 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 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)+"is");}result.Add(word+"s");result.Add(word+"es");// All rules are implemented in the EnglishWordFormsProvider classreturnresult.ToArray();}}
The next example demonstrates how to set a custom word forms provider for using.
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