Skip to main content
A newer version of this page is available. .

Dictionaries

  • 7 minutes to read

The DXSpellChecker for WPF uses special spell checking 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 SpellChecker. Therefore, to add spell-checking to your project, you need to:

  • Choose the most appropriate dictionary type (Simple, ISpell, OpenOffice or a Hunspell).
  • Add a dictionary 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.

Dictionaries of different types have the same base interface - ISpellCheckerDictionary. All dictionaries should be added to the SpellCheckerBase.Dictionaries collection of the SpellChecker component. 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 SpellChecker, and then the SpellChecker chooses appropriate dictionaries according to its current SpellCheckerBase.Culture.

To reduce application loading time and memory consumption, you can specify that a dictionary is not loaded until required - the SpellCheckerBase.LoadOnDemand property.

After each dictionary is successfully loaded, the ISpellCheckerDictionary.Loaded event fires. You can subscribe to the SpellCheckerBase.UnhandledException event to catch problematic situations which may happen while dictionary is loaded and prepared for use.

Simple Dictionary

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 SpellCheckerDictionary dictionary at runtime. It also shows how to use an alphabet file containing all letters in your preferred language:

using System.Globalization;
using DevExpress.XtraSpellChecker;
//...

SpellCheckerDictionary simpleDictionary = new SpellCheckerDictionary();
simpleDictionary.AlphabetPath = "EnglishAlphabet.txt";
simpleDictionary.Culture = new CultureInfo("en-US");
simpleDictionary.DictionaryPath = "american.txt";

spellChecker1.Dictionaries.Add(simpleDictionary);

ISpell Dictionary

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 is especially effective for synthetic languages (rich in verbal and inflective forms) - Lithuanian or Russian, for example.

The ISpell dictionary is based on this approach. It includes base words and affixes. Physically it is represented by the Alphabet file - *.txt, the Affix file - *.aff, and the Base Words file - *.xlg or *.hash. (Note: SpellChecker does not 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. Additionally, while developed by different individuals, each ISpell dictionary may be redistributed under its specific license agreement. Most of them are free. Visit the English Spell Checker Dictionaries page for more information.

The following code demonstrates how to create the SpellCheckerISpellDictionary dictionary at runtime.

Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.SpellChecker
Imports DevExpress.XtraSpellChecker
Imports System.Globalization
Imports System.IO
Imports System.Reflection
Imports System.Windows
' ...
            checker.Dictionaries.Clear()

            Dim dict_en_US As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("american.xlg")
            Dim grammar_en_US As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("english.aff")
            Dim alphabet_en_US As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("nglishAlphabet.txt")

            Dim ispellDictionaryEnglish As New SpellCheckerISpellDictionary()
            ispellDictionaryEnglish.LoadFromStream(dict_en_US, grammar_en_US, alphabet_en_US)
            ispellDictionaryEnglish.Culture = New CultureInfo("en-US")
            checker.Dictionaries.Add(ispellDictionaryEnglish)

Hunspell Dictionary

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. To download them, visit English Spell Checker Dictionaries or Hunspell pages.

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.

The following code demonstrates how to create the HunspellDictionary dictionary at runtime.

Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.SpellChecker
Imports DevExpress.XtraSpellChecker
Imports System.Globalization
Imports System.IO
Imports System.Reflection
Imports System.Windows
' ...
            checker.Dictionaries.Clear()

            Dim dict_en_US As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("en_US.dic")
            Dim grammar_en_US As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("en_US.aff")

            Dim hunspellDictionaryEnglish As New HunspellDictionary()
            hunspellDictionaryEnglish.LoadFromStream(dict_en_US, grammar_en_US)
            hunspellDictionaryEnglish.Culture = New CultureInfo("en-US")
            checker.Dictionaries.Add(hunspellDictionaryEnglish)

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.

The dictionaries and affix files are a part of the OpenOffice.org project and can be downloaded from the Extensions OpenOffice Page. Note that OpenOffice license term can be unacceptable for the commercial projects.

Tip

Files downloaded from the Open Office project site has an .oxt extension. In fact, it is a zip archive. Rename the file extension to .zip, open the file using an archiver, find .aff and .dic files and extract them.

The following code demonstrates how to create the SpellCheckerOpenOfficeDictionary dictionary at runtime.

Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.SpellChecker
Imports DevExpress.XtraSpellChecker
Imports System.Globalization
Imports System.IO
Imports System.Reflection
Imports System.Windows
' ...
            checker.Dictionaries.Clear()
            Dim resources() As String = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames
            Dim dict_en_US As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("oo_en_US.dic")
            Dim grammar_en_US As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("oo_en_US.aff")
            Dim alphabet_en_US As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("EnglishAlphabet.txt")

            Dim openOfficeDictionaryEnglish As New SpellCheckerOpenOfficeDictionary()
            openOfficeDictionaryEnglish.LoadFromStream(dict_en_US, grammar_en_US, alphabet_en_US)
            openOfficeDictionaryEnglish.Culture = New CultureInfo("en-US")
             checker.Dictionaries.Add(openOfficeDictionaryEnglish)

Custom Dictionary

Custom dictionaries are intended to store user additions (words that are considered by users as “correct”). For example, after a SpellChecker has a custom dictionary for the current Culture, an end-user is able to click on the Add button and add this word to the currently available custom dictionary. The other dictionary types don’t support this feature.

Also, a set of words in a custom dictionary may be manually changed by an end-user by invoking the Custom Dictionary dialog via the Edit button on the Spelling Options dialog.

The following code demonstrates how to create the SpellCheckerCustomDictionary dictionary at runtime.

Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.SpellChecker
Imports DevExpress.XtraSpellChecker
Imports System.Globalization
Imports System.IO
Imports System.Reflection
Imports System.Windows
' ...
            Dim customDictionary As New SpellCheckerCustomDictionary()
            customDictionary.AlphabetPath = "Dictionaries\EnglishAlphabet.txt"
            customDictionary.DictionaryPath = "Dictionaries\CustomEnglish.dic"
            customDictionary.Culture = CultureInfo.InvariantCulture
            checker.Dictionaries.Add(customDictionary)

Note

A custom dictionary is overwritten by SpellChecker 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.

If the dictionary is not available, make sure that it is not held by another processes.