Design a Dictionary with Spellchecker (En-Fa)(De-En)






4.83/5 (33 votes)
Design a Dictionary with Spellchecker (English to Farsi AND German to English)
- Download demo (English to Farsi) - 1.46 MB
- Download demo (German to English) - 3.42 MB
- Download source code - 1.49 MB
Background
About a month ago, I was searching for a Windows dictionary application that was written in C#, but I couldn't find anything and decided to write it myself.
Introduction
This dictionary has auto-complete functionality and a small spell checker and now works with two text databases – "English to Persian (Farsi)" and "German to English".

Application Design
When I began to write this dictionary, I chose a "three-tier application design" for the main design structure. Why "three-tier application design"? Because we have a GUI, a text file as vocabulary database, and a layer that must be the interface between the GUI and the database. In my opinion, when we have a database and some user interfaces and we want to encapsulate database access (because we don't know the type of the database – in this case a text file), using this design approach is a good practice.
Three-Tier Application Design
The three tiers mentioned above have been defined as below: [1]
- Data Tier: Access to the database is defined in this tier. This tier includes
static
classes and acts as an interface between the text database and the business tier. It is obvious that we don't store any data in this tier but it is a tool that has permission to load and modify the database. The code for this part could be found in the Data folder in Project directory. - Business Tier: Includes classes that define the structure for storing data and it is an interface between data tier and presentation tier. This tier is the core of this application and you can find the code for this section in Core folder in Project directory.
- Presentation Tier: This tier consists of the GUIs that have direct access to the business tier and has nothing to do with the data tier. The code is placed in the Form folder.
The relationship between the three tiers is shown in the diagram below:

Data Tier and Text Database Design
To define the text database as a vocabulary repertoire, we have a text file that has the entries and their definitions described in the following way:
Entry :: Definition
If an entry has multiple definitions, a (#) must be added after the previous definition.

You can see "English to Persian" and "German to English" samples below:
Now we must define classes to interact with the text database.
There are two static
classes:
TextDatabase
is for reading and loading the database into a particular structureTextDatabaseModifier
class is for adding an entry and its definition to the text database.
Business Tier Design
First step: We define a structure for storing each entry (Key
) and its definition (Value
) and its index (Index
) in the dictionary, and we name it DictionaryKeyIndexValue
.
Second step: We define a class and name it DictionaryPack
, the heart of our program. This class is made up of two fields:
System.Collections.Generic.Dictionary<string, Core.DictionaryKeyIndexValue> _dictionary
and
System.Collections.Generic.List<Core.DictionaryKeyIndexValue> _list
This class interacts with the data tier. When we read each line of text database in data tier, we add a DictionaryKeyIndexValue
object to _dictionary
and _list
in a DictionaryPack
instance.
You may ask yourself "Are we crazy to add the same object to _dictionary
and _list
? It uses twice the memory needed!!". Yes, you are right. This needs more memory, but it is a good practice and speeds up the program and the same time enhances the flexibility of the code. It is a tradeoff between memory usage and having speed and flexibility.
So why do we need Dictionary
and List
at the same time? Because we can benefit from this approach as shown in the following way.
If we look up the definition of a word, we have to use the Dictionary
object to find the definition but when we need to get a list of words (e.g. in auto-complete functionality), using Dictionary
object is not feasible because it has no indexing ability, therefore we have to use the List
object.
Anyway, it has its disadvantages too. For example, if you use German to English dictionary (that has about 240,000 words) you need 70MB of memory and if you use English to Persian dictionary (that has about 60,000 words) 20MB of memory is needed.
You can see all properties and methods in the class diagram below:

Using Code
Using the dictionary is very simple. First we have to instantiate an object of DictionaryPack
as shown in the following code:
private Core.DictionaryPack _dictionaryPack;
private void EyeDictionaryForm_Load(object sender, EventArgs e){
//Choose languages for translating}
_dictionaryPack = Core.DictionaryPack.LoadDictionary
(EyeDictionary.Core.TranslatingLanguages.DeutschlandToEnglish);
Now we show how to use DictionaryPack
methods.
("Key
" in the code samples below is the word entered by user.)
-
If we want only one definition of a word, we use:
string value = _dictionaryPack.GetValue(key);
-
If we want all the definitions of a word, we use:
String[] meanings = _dictionaryPack. GetMeanings(key);
-
If we want the index of a specific
key
:int index = _dictionaryPack. IndexOf(key);
-
If we want to know whether
_dictionaryPack
contains a specifickey
:bool exist = _dictionaryPack. ContainsKey(key);
Auto-Complete
This application has a ListBox
to provide auto-complete functionality. If we want to get auto completed words in list box, we must get the word that is most similar to the word that we enter:
DictionaryKeyIndexValue kiv = Level1AutoCompeleteWord(key, AutoCompeleteLevel.Level3));
int properIndex;
string[] autos = _dictionaetPack.GetAutoCompletedBoundaries(kiv.Index,properIndex);<o:p>
ListBox.SelectedIndex = properIndex;
listBoxAutoCompleteWords.Items.AddRange(autos); //Fill ListBox with autos
Spellchecking
For using the spellchecker:
string[] words = GetSugesstionWords(key, Core.SugesstionLevel.Level6, append)
There are 6 levels for spellchecking. In each level, we get different suggestion words and if append is true
, then all the suggestions of the lower levels and current level will be given.
Each level is described as follows:
1. Core.SugesstionLevel.Level1
In this level, if the user misspells a word, GetSugesstionWords()
returns all of the meaningful words that have one character difference with the misspelled word or have one less character than the misspelled one.

2. Core.SugesstionLevel.Level2
It's possible that the word entered by the user has two adjacent characters in the wrong order. This level deals with this kind of misspelling.

3. Core.SugesstionLevel.Level3
If the user has omitted one character in a word, all the words with an extra character anywhere in the misspelled word are returned.

4. Core.SugesstionLevel.Level4
The same as level three but with two characters missing anywhere in the misspelled word.

5. Core.SugesstionLevel.Level5
Combination of other levels.
6. Core.SugesstionLevel.Level6

Returns all the words and phrases containing the word entered.
As you can see with level6
, we have many suggested words.
By setting append to true
, we can have a combination of all lower levels and the current level.
Using level 3 is advised because if we use level 6 and set append to true
, we have too many results.
Notice
All settings (such as text database file path . . .) are stored in a static
class in Global folder. For example, if you want to get the text database file path, you must use the code:
Global.Settings.Dictionary.CurrentUsedDictionaryPath
You can add multiple dictionaries for better spellchecking and more definitions, like the concise version of Babylon® dictionary.
Conclusion
At the end, I should mention that this is just about an application and not an article and doesn't represent a fast and/or best way for developing a dictionary.
Thank you for reading.
Database and References
German to English
- # Version: 1.5 2007-04-09
- # Copyright (c): Frank Richter <frank.richter.tu-chemnitz.de>
- # 1995 - 2007
- # License: GPL Version 2 or later; GNU General Public License
- # URL: http://dict.tu-chemnitz.de/
English to Farsi
- Developer: Iranian Mac User Group
- License: Freeware
- Download Page: http://mac.softpedia.com/get/Utilities/Farsi-English-database.shtml
- Description: Farsi/English and English/ Farsi dictionary-database with more than 100,000 words for Word lookup 2.09 or 3.0.
=======================================================================
[1] - Apress Beginning C# 2005 Databases
History
- Update - "De to En" {German(Deutschland) to English} text database fixed