Last year I started a thread here on the forum in which I published some early versions of a spellchecker. At a certain point, I saw it more fit to make a library of the code I had written which I now post here.
Here is some information about the spellchecker-library:
Overview - WMSpellChecker
Basically, a spell checker customarily consists of two parts:
1) A set of routines for scanning text and extracting words, and
2) An algorithm for comparing the extracted words against a known list of correctly spelled words (i.e., the dictionary).
However, what mentioned above is only a "half" spell checker since these days spell checkers also suggest replacements/corrections for misspelled words (among other things such as synonyms and grammar-hints). Said suggestions can be proposed by the program based upon various techniques:
- phonetic algorithms such as "Soundex" among others.
- word lists containing common misspelled words and letters commonly inverted
- functions called "Near Miss Strategy" and introduced by one of the first spell-checkers on the market, namely Ispell for UNIX and with its roots dating back to 1971.
- algorithms like "edit distance" which measures the amount of difference between two sequences. A famous one is the "Levenshtein distance".
- and other techniques
I am aware of the fact that (at least) WM6 already offers spelling-suggestions and a spellchecker if Word (Office) has been installed but still I liked this idea so I decided to make a library. In any case, as far as I know, only the dictionary corresponding to the language of WM6 is being installed so if you want to spell check words in other languages you cannot do so.
The way it works.....
First of all, apart from referencing the library itself, you need to add two objects to your application, namely Dictionary and ComputeDetection.
Then you need to load the dictionary-files by using "LoadDict". Currently they consist of four separate files. However, I may change this in a future release. The dictionary-files must be located in the application-directory although you can create sub-folders. This first release only supports English and the dictionaries distributed with the library must not be tempered with. Next release will bring support for other languages and will also include a separate program for handling dictionaries.
Once the dictionaries have been loaded, you can start the spellchecking by calling the library using "ComputeDetection" which passes on your textbox-control to the library for parsing. You may tell the library to ignore words depending on their length by setting the property "SetMinimumWordLength" before executing "ComputeDetection". In case there are words that are not present in the dictionary, then a set of suggestions will be returned to the calling application and at the same time the word which was not found will be shown in the textbox in capital-letters. The suggestions produced by the library can be obtained using "ReturnSuggestions" which returns a string-array.
Once you have shown the suggestions returned by the library, you can let your user in your application decide what to do i.e. ignore the wrong word ("IgnoreWord"), add an own word ("AddWord") to replace the wrong word or to replace the wrong word with a word from the suggestions ("ReplaceWord").
At this point, you tell the library to continue spellchecking by using "ContinueDetection". You should also verify if spellchecking has been terminated by using "IsSpellingFinished".
At any time, you can interrupt spellchecking by using "UnloadDict". This will be useful in a future release of the library so you can unload an English dictionary and to replace it with, for instance, a French dictionary without exiting your own application. However, before unloading the dictionary, you should verify if a dictionary has already been loaded or not by using "IsDictionaryLoaded".
In the help-file, you can find more (important) information as to the commands available. Please also check out the two enclosed sample-programs (one using a classic spellchecking-interface and another one using context-menus) where the source-code has been commented.
Other comments....
This first release has some limitations, such as support only for English and the need for a textbox-control. However, I will add other features in the future, for instance:
-support for other languages
-dictionary-tools (for creating dictionaries) - will be an external program
-possibility to add a user-dictionary
-possibility to limit amount of suggestions produced by the library (by using a "ranking-system")
-no further need for a textbox-control in your application. Your application will be able to pass on to the library only the word(s) you wish to spellcheck and the library will only return the suggestion(s) in a string-array. In this way, the spellchecker-library will not "interfere" with your application and you can use whichever control you prefer such as WebBrowser.
-spellchecking "on the fly"
-extended error-handling
A few notes regarding dictionaries....
The English dictionary supplied with the library is composed of nearly 70'000 words. Dictionaries to be used with the library must be sorted and each word in the dictionary must use LF = chr(10) as line-endings. In addition, the dictionary should be saved as UTF-8.
From the dictionary, a KeyMap is created using either a Soundex - or a DoubleMetaphone-algorithm. In this moment, the KeyMap is being furnished with the library and loaded as an external file but future releases might create it on the fly (or at least an option to do so). With next release, I will add a utility, to be run from the Desktop, which will let you create your own dictionary and corresponding KeyMap which are compatible with WMSpellChecker.
Unlike English and Scandinavian ones, dictionaries for German and Latin languages such as Spanish, Italian and French will probably be rather large. This is due to the fact that German, Italian and other similar languages use a lot of suffixes for instance when creating verbs. In order to overcome this, certain spellcheckers such as ASpell, ISpell, HunSpell (used by OpenOffice) have implemented dictionaries which mostly contain only the base-form of words/verbs. However, they use an additional file called "affix" which contains a lot of grammar-rules and this file together with the simplified dictionary overcomes the problem of large dictionaries. However, I believe this system is probably rather memory - and performance-hungry and might not be the best solution for Windows Mobile and PPC. However, maybe in the future I will look into this.
Another negative side-effect of using a too large dictionary is that said dictionary may include more obscure words which will increase the risk that the spelling-engine will "miss" real-word errors. For instance, the word wether illustrates this. The word is, arguably, so obscure that any occurrence of wether in a passage is more likely to be a misspelling of weather or whether than a genuine occurrence of wether, so that a spellchecker that did not have the word in its dictionary would do better than one that did.
Conclusion....
The library can be used with projects developed with Basic4ppc (PPC and Desktop) but should also work with projects created in Visual Studio and SharpDevelop (using VB.NET and C#). The library has been compiled targeting Framework Version 2.0.
Library-version: 1.01
Helpfile-version: 1.01
Change log:
20/03/2009 - added the property "SetMinimumWordLength" as per request
PS: I am attaching four zip-files as follows:
1) WMSpellCheckerLibrary.zip (library and helpfile)
2) DictionaryFiles.zip (in English and needed for the below examples)
3) TextEditor.zip (this is Erel's texteditor to which I added spellchecking and context-menus)
4) SpellingForm.zip (classic interface of a spellchecker)
Note: Use above dictionary-files with the sample-applications. Please locate the files in the application-directory.
Please check and test the library and let me know if it works.
Feedback would be highly appreciated.
Regards,
\moster67
Italy - March 15, 2009
Here is some information about the spellchecker-library:
Overview - WMSpellChecker
Basically, a spell checker customarily consists of two parts:
1) A set of routines for scanning text and extracting words, and
2) An algorithm for comparing the extracted words against a known list of correctly spelled words (i.e., the dictionary).
However, what mentioned above is only a "half" spell checker since these days spell checkers also suggest replacements/corrections for misspelled words (among other things such as synonyms and grammar-hints). Said suggestions can be proposed by the program based upon various techniques:
- phonetic algorithms such as "Soundex" among others.
- word lists containing common misspelled words and letters commonly inverted
- functions called "Near Miss Strategy" and introduced by one of the first spell-checkers on the market, namely Ispell for UNIX and with its roots dating back to 1971.
- algorithms like "edit distance" which measures the amount of difference between two sequences. A famous one is the "Levenshtein distance".
- and other techniques
I am aware of the fact that (at least) WM6 already offers spelling-suggestions and a spellchecker if Word (Office) has been installed but still I liked this idea so I decided to make a library. In any case, as far as I know, only the dictionary corresponding to the language of WM6 is being installed so if you want to spell check words in other languages you cannot do so.
The way it works.....
First of all, apart from referencing the library itself, you need to add two objects to your application, namely Dictionary and ComputeDetection.
Then you need to load the dictionary-files by using "LoadDict". Currently they consist of four separate files. However, I may change this in a future release. The dictionary-files must be located in the application-directory although you can create sub-folders. This first release only supports English and the dictionaries distributed with the library must not be tempered with. Next release will bring support for other languages and will also include a separate program for handling dictionaries.
Once the dictionaries have been loaded, you can start the spellchecking by calling the library using "ComputeDetection" which passes on your textbox-control to the library for parsing. You may tell the library to ignore words depending on their length by setting the property "SetMinimumWordLength" before executing "ComputeDetection". In case there are words that are not present in the dictionary, then a set of suggestions will be returned to the calling application and at the same time the word which was not found will be shown in the textbox in capital-letters. The suggestions produced by the library can be obtained using "ReturnSuggestions" which returns a string-array.
Once you have shown the suggestions returned by the library, you can let your user in your application decide what to do i.e. ignore the wrong word ("IgnoreWord"), add an own word ("AddWord") to replace the wrong word or to replace the wrong word with a word from the suggestions ("ReplaceWord").
At this point, you tell the library to continue spellchecking by using "ContinueDetection". You should also verify if spellchecking has been terminated by using "IsSpellingFinished".
At any time, you can interrupt spellchecking by using "UnloadDict". This will be useful in a future release of the library so you can unload an English dictionary and to replace it with, for instance, a French dictionary without exiting your own application. However, before unloading the dictionary, you should verify if a dictionary has already been loaded or not by using "IsDictionaryLoaded".
In the help-file, you can find more (important) information as to the commands available. Please also check out the two enclosed sample-programs (one using a classic spellchecking-interface and another one using context-menus) where the source-code has been commented.
Other comments....
This first release has some limitations, such as support only for English and the need for a textbox-control. However, I will add other features in the future, for instance:
-support for other languages
-dictionary-tools (for creating dictionaries) - will be an external program
-possibility to add a user-dictionary
-possibility to limit amount of suggestions produced by the library (by using a "ranking-system")
-no further need for a textbox-control in your application. Your application will be able to pass on to the library only the word(s) you wish to spellcheck and the library will only return the suggestion(s) in a string-array. In this way, the spellchecker-library will not "interfere" with your application and you can use whichever control you prefer such as WebBrowser.
-spellchecking "on the fly"
-extended error-handling
A few notes regarding dictionaries....
The English dictionary supplied with the library is composed of nearly 70'000 words. Dictionaries to be used with the library must be sorted and each word in the dictionary must use LF = chr(10) as line-endings. In addition, the dictionary should be saved as UTF-8.
From the dictionary, a KeyMap is created using either a Soundex - or a DoubleMetaphone-algorithm. In this moment, the KeyMap is being furnished with the library and loaded as an external file but future releases might create it on the fly (or at least an option to do so). With next release, I will add a utility, to be run from the Desktop, which will let you create your own dictionary and corresponding KeyMap which are compatible with WMSpellChecker.
Unlike English and Scandinavian ones, dictionaries for German and Latin languages such as Spanish, Italian and French will probably be rather large. This is due to the fact that German, Italian and other similar languages use a lot of suffixes for instance when creating verbs. In order to overcome this, certain spellcheckers such as ASpell, ISpell, HunSpell (used by OpenOffice) have implemented dictionaries which mostly contain only the base-form of words/verbs. However, they use an additional file called "affix" which contains a lot of grammar-rules and this file together with the simplified dictionary overcomes the problem of large dictionaries. However, I believe this system is probably rather memory - and performance-hungry and might not be the best solution for Windows Mobile and PPC. However, maybe in the future I will look into this.
Another negative side-effect of using a too large dictionary is that said dictionary may include more obscure words which will increase the risk that the spelling-engine will "miss" real-word errors. For instance, the word wether illustrates this. The word is, arguably, so obscure that any occurrence of wether in a passage is more likely to be a misspelling of weather or whether than a genuine occurrence of wether, so that a spellchecker that did not have the word in its dictionary would do better than one that did.
Conclusion....
The library can be used with projects developed with Basic4ppc (PPC and Desktop) but should also work with projects created in Visual Studio and SharpDevelop (using VB.NET and C#). The library has been compiled targeting Framework Version 2.0.
Library-version: 1.01
Helpfile-version: 1.01
Change log:
20/03/2009 - added the property "SetMinimumWordLength" as per request
PS: I am attaching four zip-files as follows:
1) WMSpellCheckerLibrary.zip (library and helpfile)
2) DictionaryFiles.zip (in English and needed for the below examples)
3) TextEditor.zip (this is Erel's texteditor to which I added spellchecking and context-menus)
4) SpellingForm.zip (classic interface of a spellchecker)
Note: Use above dictionary-files with the sample-applications. Please locate the files in the application-directory.
Please check and test the library and let me know if it works.
Feedback would be highly appreciated.
Regards,
\moster67
Italy - March 15, 2009
Attachments
Last edited: