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.
constwordFormsProvider=java.newProxy('com.groupdocs.search.dictionaries.IWordFormsProvider',{getWordForms:function(word){constresult=[];// Assume that the input word is in the plural, then we add the singular
if(word.length>2&&word.toLowerCase().endsWith('es')){result.push(word.substr(0,word.length-2));}if(word.length>1&&word.toLowerCase().endsWith('s')){result.push(word.substr(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.push(word.substr(0,word.length-1)+'is');}result.push(word+'s');result.push(word+'es');// All rules are implemented in the EnglishWordFormsProvider class
returnjava.newArray('java.lang.String',result);},});
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.
constindexFolder='c:/MyIndex/';constdocumentsFolder='c:/MyDocuments/';// Creating an index in the specified folder
constindex=newgroupdocs.search.Index(indexFolder,true);// Subscribing to the event
index.getEvents().ErrorOccurred.add(java.newProxy('com.groupdocs.search.events.EventHandler',{invoke:function(sender,args){console.log(args.getMessage());},}),);// Indexing documents from the specified folder
index.add(documentsFolder);String.prototype.endsWith=function(str){varlastIndex=this.lastIndexOf(str);returnlastIndex!==-1&&lastIndex+str.length===this.length;};// Setting the custom word forms provider instance
constwordFormsProvider=...// See code example above
index.getDictionaries().setWordFormsProvider(wordFormsProvider);// Creating a search options instance
constoptions=newgroupdocs.search.SearchOptions();options.setUseWordFormsSearch(true);// Enabling search for word forms
// Searching in the index
constquery='mrs';constresult=index.search(query,options);// The following words can be found:
// mrs
// mr
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: