Using RTE dictionary for spell check
Recently a client asked if we could spell check search terms in case they did not yield results and show suggested terms. My immediate answer was yes, we most certainly can. Then came the catch, they had a bunch of product names, which were not always valid English words and they wanted to show these names as suggested terms. During our discussion the client informed me that the content authors frequently used the Rich-text editor and added their product names to a dictionary and asked if we used that dictionary to run the spell checker. I knew Sitecore uses Telerik s Rich text box for its Rich Text Field. The rich text box ships with a spell checker and language specific dictionaries. After a couple of coffees and some time with my favorite tool, .Net Reflector I wrote the following code that solves the problem.
public static IEnumerable GetSuggestions(string word, Language language)
{
if (string.IsNullOrEmpty(word))
return Enumerable.Empty();
try
{
CultureInfo cultureInfo = language != null ? language.CultureInfo : Sitecore.Context.Language.CultureInfo;
if (cultureInfo.IsNeutralCulture)
{
cultureInfo = Language.CreateSpecificCulture(cultureInfo.Name);
}
Telerik.Web.UI.SpellChecker checker = new SpellChecker(FileUtil.MapPath(Settings.GetAppSetting("DictionaryFolder", "/sitecore/shell/controls/rich text editor/Dictionaries/")))
{
Text = word,
DictionaryLanguage = cultureInfo.Name
};
return checker.GetSuggestions(word);
}
catch (Exception ex)
{
Log.Error("dictionary could not find a match", ex, new object());
}
return Enumerable.Empty();
}