- 6 minutes to read
The ASPxSpellChecker uses special Spell Check Algorithms which require a dictionary to provide the list of words and/or rules. At a minimum, one of the four dictionary types must be defined for ASPxSpellChecker. Therefore, to add spell-checking to your project, you need to:
- Choose the most appropriate dictionary type (Simple, ISpell, OpenOffice or a Hunspell).
- Add it to your application for every particular language (culture) you want to support.
- Add a custom dictionary for every language, if you want to allow end-users to add custom words.
All dictionaries are added to the ASPxSpellChecker.Dictionaries collection. You can add multiple dictionaries of the same type to this collection. For example, you may add different dictionaries for different languages to a single ASPxSpellChecker, and then the ASPxSpellChecker chooses appropriate dictionaries according to its current ASPxSpellChecker.Culture.
This dictionary type lists words in a plain text file where each line contains only one word. To use this dictionary, manually prepare a text file (or export any existing dictionary to a plain text file).
The following code demonstrates how to create the ASPxSpellCheckerDictionary dictionary at runtime. It also shows how to use an alphabet file containing all letters in your preferred language:
using System.Globalization; using DevExpress.Web.ASPxSpellChecker; //... ASPxSpellCheckerDictionary simpleDictionary = new ASPxSpellCheckerDictionary(); simpleDictionary.AlphabetPath = "~/Dictionaries/EnglishAlphabet.txt"; simpleDictionary.Culture = new CultureInfo("en-US"); simpleDictionary.DictionaryPath = "~/Dictionaries/american.txt"; ASPxSpellChecker1.Dictionaries.Add(simpleDictionary);
An ideal dictionary would contain all the words of a given language. However, it’s much smaller and more effective to split the dictionary into several parts (depending on the language). For example, in several Indo-European languages, including English, words are derived from the base by adding affixes - prefixes or postfixes. So the size of the dictionary can be greatly reduced if the base words, affixes and the rules for adding affixes to base words are placed into separate files. The complete list of words could be built in-place, when necessary. This technique especially proves its effectiveness for synthetic languages (rich in verbal and inflective forms) - Lithuanian or Russian, for example.
The ISpell dictionary is based upon this approach. It includes base words and affixes. Physically it’s represented by the Alphabet file - *.txt, the Affix file - *.aff, and the Base Words file - *.xlg or *.hash. (Note: SpellChecker doesn’t provide support for compressed *.hash files but you may use *.xlg files).
ISpell dictionaries are primarily developed by enthusiasts all over the world, and you may freely find them for different languages on the Web. Such dictionaries can be found at https://ftp.gnu.org/gnu/aspell/dict/0index.html. Additionally, while developed by different individuals, each ISpell dictionary may be redistributed under its specific license agreement. Most of them are free.
The following code demonstrates how to create the ASPxSpellCheckerISpellDictionary dictionary at runtime:
using System.Globalization; using DevExpress.Web.ASPxSpellChecker; //... ASPxSpellCheckerISpellDictionary dictionary = new ASPxSpellCheckerISpellDictionary(); dictionary.AlphabetPath = "~/Dictionaries/EnglishAlphabet.txt"; dictionary.Culture = new CultureInfo("en-US"); dictionary.DictionaryPath = "~/Dictionaries/american.xlg"; dictionary.GrammarPath = "~/Dictionaries/english.aff"; ASPxSpellChecker1.Dictionaries.Add(dictionary);
Open Office Dictionary
The Open Office dictionary is similar to ISpell, since it also generates the entire word list based on the Affix file - *.aff, and the Base Words file - *.dic. But note that this standard provides different rules for the Affix file than ISpell, so you can’t use the same Affix files for both dictionaries.
In general, Open Office dictionaries are word rich and contain less mistakes than ISpell since they are developed by more people. The dictionaries and affix files used are a part of the OpenOffice.org project.
The following code demonstrates how to create the ASPxSpellCheckerOpenOfficeDictionary dictionary at runtime:
using System.Globalization; using DevExpress.Web.ASPxSpellChecker; //... ASPxSpellCheckerOpenOfficeDictionary openOfficeDictionary = new ASPxSpellCheckerOpenOfficeDictionary(); openOfficeDictionary.DictionaryPath = "~/Dictionaries/en_US.dic"; openOfficeDictionary.GrammarPath = "~/Dictionaries/en_US.aff"; openOfficeDictionary.Culture = new CultureInfo("en-US"); ASPxSpellChecker1.Dictionaries.Add(openOfficeDictionary);
Hunspell allows dictionaries to define complex rules for compound word usage and was designed for languages with rich morphology and complex word compounding or character encoding. Its advantages include morphological analysis, Unicode support and reduced memory usage.
Hunspell dictionaries are used in the OpenOffice, FireFox, and OpenSpell spell check engines. As such, you can easily download existing dictionaries and thus streamline your deployment.
Hunspell dictionary includes two files - the Affix file with grammar rules- *.aff, and the Base Words file - *.dic file. The SpellChecker component uses them to create a HunspellDictionary in the same manner as it does for the OpenOffice dictionaries described above. You can specify the HunspellDictionary.GrammarPath and the DictionaryBase.DictionaryPath, then use the DictionaryBase.Load method to load a dictionary. Or you can load both dictionary parts from streams via the HunspellDictionary.LoadFromStream method.
Custom dictionaries are intended to store end-user additions (words that are considered by end-users as “correct”). For example, after a ASPxSpellChecker has a custom dictionary for the current Culture, an end-user is able to click on the Add to Dictionary button on the Check Spelling Form and add this word to the currently available custom dictionary. The other dictionary types don’t support this feature.
A custom dictionary consists of a text file (you can start from an empty file, or add several words, each on a new line) and an alphabet file. An alphabet file lists language characters. The English alphabet file contains the string “ABCDEFGHIJKLMNOPQRSTUVWXYZ”.
The following code demonstrates how to create the ASPxSpellCheckerCustomDictionary dictionary at runtime:
using System.Globalization; using DevExpress.Web.ASPxSpellChecker; //... ASPxSpellCheckerCustomDictionary customDictionary = new ASPxSpellCheckerCustomDictionary(); customDictionary.Culture = new CultureInfo("en-US"); customDictionary.AlphabetPath = "~/Dictionaries/EnglishAlphabet.txt"; customDictionary.DictionaryPath = "~/Dictionaries/CustomEnglish.dic"; ASPxSpellChecker1.Dictionaries.Add(customDictionary);
A custom dictionary is overwritten by ASPxSpellChecker every time a new word is added to it, or it’s manually changed by an end-user. Therefore this file must not be set to read-only.