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.
publicclassSimpleWordFormsProviderimplementsIWordFormsProvider{publicfinalString[]getWordForms(Stringword){ArrayList<String>result=newArrayList<String>();// Assume that the input word is in the plural, then we add the singular
if(word.length()>2&&word.toLowerCase().endsWith("es")){result.add(word.substring(0,word.length()-2));}if(word.length()>1&&word.toLowerCase().endsWith("s")){result.add(word.substring(0,word.length()-1));}// Then assume that the input word is in the singular, we add the plural
if(word.length()>1&&word.toLowerCase().endsWith("y")){result.add(word.substring(0,word.length()-1).concat("is"));}result.add(word.concat("s"));result.add(word.concat("es"));// All rules are implemented in the EnglishWordFormsProvider class
returnresult.toArray(newString[0]);}}
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.
StringindexFolder="c:\\MyIndex\\";StringdocumentsFolder="c:\\MyDocuments\\";// Creating an index in the specified folder
Indexindex=newIndex(indexFolder);// Indexing documents from the specified folder
index.add(documentsFolder);// Setting the custom word forms provider instance
index.getDictionaries().setWordFormsProvider(newSimpleWordFormsProvider());// Creating a search options instance
SearchOptionsoptions=newSearchOptions();options.setUseWordFormsSearch(true);// Enabling search for word forms
// Searching in the index
SearchResultresult=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: