IWordFormsProvider

IWordFormsProvider interface

Defines interface of a word forms provider.

public interface IWordFormsProvider

Methods

Name Description
GetWordForms(string) Gets the word forms for the specified word. The resulting array does not contain the original word.

Remarks

Learn more

Examples

The following example demonstrates how to implement a custom word forms provider.

public class SimpleWordFormsProvider : IWordFormsProvider
{
    public string[] GetWordForms(string word)
    {
        List<string> result = new List<string>();

        // Assume that the input word is in the plural, then we add the singular
        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 assume that the input word is in the singular, we add the plural
        if (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 class

        return result.ToArray();
    }
}

The next example demonstrates how to set a custom word forms provider for using.

string indexFolder = @"c:\MyIndex\";
string documentsFolder = @"c:\MyDocuments\";
  
// Creating an index in the specified folder
Index index = new Index(indexFolder);
  
// Indexing documents from the specified folder
index.Add(documentsFolder);
 
// Setting the custom word forms provider instance
index.Dictionaries.WordFormsProvider = new SimpleWordFormsProvider();
 
// Creating a search options instance
SearchOptions options = new SearchOptions();
options.UseWordFormsSearch = true; // Enabling search for word forms
  
// Searching in the index
SearchResult result = index.Search("relative", options);
  
// The following words can be found:
// relative
// relatives

See Also