Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C++
Article

A Spell Checking Engine

Rate me:
Please Sign up or sign in to vote.
4.88/5 (16 votes)
5 Feb 2001 264.6K   7K   108   55
A free spell checking engine for use in your C++ applications. Includes the current US English dictionary
  • Download demo project - 85 Kb
  • Download the current US English Dictionary - 820 Kb
  • Image 1 Image 2

    Description:

    This project is an evolution of the spell checking engine project I submitted earlier. This project includes numerous enhancements to the core spelling engine plus the addition of a "check-as-you-type" edit control and the related support dialogs (see above).

    This project is not complete, it is a work-in-progress. There are numerous issues with the current version which need to be addressed. My long term goal is to develop this to "commercial quality".

    I am going to continue to improve this engine toward my goal. I will continue to post updates as I feel necessary.

    Changes from previous version:

    1. Reorganized class architecture. Added CFPSSpellCheckEngineOptions, CFPSDictionary to core engine.
    2. Created CFPSSpellingEditCtrl CEdit derived class to implement "check-as-you-type" edit control.
    3. Created options property pages (see above)
    4. Created spelling dialog (see above)
    5. Created common-use dictionary. (Download available above)
    6. Updated US English dictionary w/improved word list + proper names.
    7. Incremental changes to the MetaphoneEx function.
    8. Addition of the EditDistance function.
    9. Added support for case-sensitive dictionary entries.
    10. Added file header to dictionaries.

    Image 3

    Image 4

    Image 5

    To-do-list:

    1. Re-write EditDistance algorithm for performance.
    2. Research compression options for dictionaries
    3. Create ATL ActiveX control from edit control
    4. Create COM based dictionary support for language independence
    5. Create Rich Edit "check-as-you-type" control.
    6. Add auto-correct capability.
    7. Add sentence begin recognition for automatic upper case decisions.
    8. Continue to improve US English dictionary.
    9. Implement a binary-search mechanism on dictionary look-ups.
    10. Continue to improve MetaphoneEx function.
    11. Create C# (.net) version (when C# stabilizes)
    12. ETC...

    Classes:

    CFPSSpellCheckEngine This is the core spelling engine. It is intended to be language independent. (not currently). This engine encapsulates the functionality of managing dictionaries, making suggestions (through dictionaries), and maintaining spelling options.
    CFPSSpellCheckEngineOptions Support class for CFPSSpellCheckEngine which implements support for storing, saving and loading spell checking options. Currently, this uses a serialized file to store options, but could easily be changed to INI file or registry.
    CFPSDictionary Base dictionary class. Defines a set of virtual functions generic to all dictionaries. Also, provides base implementation of all virtual functions based on current requirements. This class uses a defined file structure w/ a file header and any number of dictionary records. Future derivations of this class will provide language specific support.
    CDlgSpellChecker CDialog derived class which implements the spell checker dialog. Currently the undo support is based on edit control undo support, not spell checker undo. Need to improve this further.
    CPrShtSpellOptions CPropertySheet derived class which implements the spell checking engines property sheet.
    CPrPgeSpellOptions_General CPropertyPage derived class implementing general options panel.
    CPrPgeSpellOptions_User CPropertyPage derived class implementing user dictionary options panel.
    CPrPgeSpellOptions_Common CPropertyPage derived class implementing common misspellings options panel.

    Support functions of importance:

    void CheckSpellingEdit (CFPSSpellCheckEngine* pEngine, CEdit* pEdit)
    This function is called when a user presses the F7 (or configured) hot key from within the "check-as-you-type" edit control. It displays the
    CDlgSpellChecker
    
    dialog box.
    void CheckSpellingRich (CFPSSpellCheckEngine* pEngine, CRichEditCtrl* pEdit)
    This function is called when a user presses the F7 (or configured) hot key from within the "check-as-you-type" edit control. It displays the
    CDlgSpellChecker
    
    dialog box.

    NOTE: This function is not currently being used because the rich edit control is not complete.

    int EditDistance(const char *szWord1, const char *szWord2) This function is passed in 2 words and returns an approximation of the minimum number of changes a user would need to make to make the 2 words match. This function is not a true edit-distance algorithm, but is a customized algorithm for this spell checking application.
    void MetaphoneEx(const char *szInput, char *szOutput, int
            iMaxLen)
    This function is passed a word and it returns (through the szOutput parameter) a modified-metaphone representation of the word. This is a variation on the algorithm originally wrote by
    <a href="http://www.cuj.com/archive/1806/feature.html">Lawrence
            Philips.</a> 
    A newer version of his algorithm (double-metaphone) is also available. I have tested this algorithm with the spell checking engine and was not impressed with the results. It does provide fast results and a high hit-rate, but it also returns far too many results (on average). However, I am considering using it in conjunction with the EditDistance algorithm and will further review this.
    void SortMatches(LPCSTR lpszBadWord, CStringList &Matches) This function sorts a list of word suggestions based on the approximate edit-distance between the words in the list and the misspelled word based in as lpszBadWord.

    Architecture:

    CORE ENGINE

    The core spell checking engine consists of the three classes: CFPSSpellCheckEngine,

    CFPSSpellCheckEngineOptions
    
    and CFPSDictionary. These classes provide support for dictionary related functions such as add a word, remove a word, ignore a word, load dictionary, save dictionary, Is a word in the dictionary, suggest possible matches, etc.

    The core engine is implemented as a strict back-end engine. It has no user-interface components. Most of the functions exposed by these classes where an error might occur return an int return code. These return codes are defined in 1) FPSSpellCheckerInclude.h and 2) the header file for a given class. The return codes should always be examined to determine the completion status of these functions.

    Special care has been taken to insure that these classes are very stable and robust. Also, performance considerations weigh heavy on the implementation of these classes. Very little MFC code is used in these classes and functions.

    CHECK-AS-YOU-TYPE EDIT CONTROL

    The check-as-you-type edit control is contained in the CFPSSpellingEditCtrl class. It is derived off of CEdit and works by subclassing an existing edit control through the AttachEdit function.

    To improve performance, this control implements a timer and whenever there is no user activity (typing, mouse clicking, scrolling, etc) checks the spelling of the displayed portion of the edit control. The function RedrawSpellingErrors is called to perform the checking. It checks only the displayed portion of the edit control and calls DrawSpellingError for each displayed word. If a word is not found in the dictionary, this function calls DrawSquiglyI to draw the squigly underline for the word. DrawSquigly creates a structure of type FPSSPELLEDIT_ERRORS and adds it to the m_SpellingErrors member list.

    The OnRButtonDown function checks the m_SpellingErrors to determine when to display the normal popup menu and when to display the spell check popup menu. Suggestions returned from the core engine are sorted using the SortMatches function to display them in order of edit-distance.

    The PreTranslateMessage checks for a hot key (defaults to F7). This can be customized by calling the SetHotKey static member function. When the hot key is pressed the CheckSpellingEdit function is called to display the spell checking dialog box.

    SPELL CHECK DIALOG BOX

    The spell checking dialog box is implemented in the CDlgSpellChecker class. This is a standard CDialog derived class based on the IDD_SPELL_CHECK dialog resource.

    The spell checking dialog is modelled after the Microsoft Word implementation of spell checking. It is laid out the same and functions (for the most part) the same. This dialog searches an edit control (or rich edit control) for sentences misspelled words and displays the sentence with the misspelled word highlighted.

    Suggestions returned from the core engine are sorted using the SortMatches function to display them in order of edit-distance.

    How to use the demo:

    1. Unzip the provided file into a directory (be sure to extract the sub directories.)
    2. Make sure that the USMain.dic file is in the \Release directory.
    3. Make sure that the USCommon.dic file is in the \Release directory.
    4. Execute the FPSSpellChecker.exe from the \Release directory.

    How to incorporate the spell checker into an application:

    1. In your applications InitInstance function, add a call to CFPSSpellingEditCtrl::InitSpellingEngine(NULL) static member function; OR, instead of NULL, pass in a string containing a fully qualified path to a spell checking engine options file.
    2. In your applications ExitInstance function, add a call to CFPSSpellingEditCtrl::Terminate static member function
    3. Add the following files to your project.
      DlgSpellChecker.cpp DlgSpellChecker.h
      DlgSpellingEditCtrl.cpp DlgSpellingEditCtrl.h
      FPSDictionary.cpp FPSDictionary.h
      FPSSpellCheckEngine.cpp FPSSpellCheckEngine.h
      FPSSpellCheckEngineOptions.cpp FPSSpellCheckEngineOptions.h
      FPSSpellCheckerInclude.cpp FPSSpellCheckerInclude.h
      FPSSpellingEditCtrl.cpp FPSSpellingEditCtrl.h
      PrPgeSpellOptions_Common.cpp PrPgeSpellOptions_Common.h
      PrPgeSpellOptions_General.cpp PrPgeSpellOptions_General.h
      PrPgeSpellOptions_User.cpp PrPgeSpellOptions_User.h
      PrShtSpellOptions.cpp PrShtSpellOptions.h
    4. Copy the following resource items to your project.
      IDD_SPELL_CHECK
      IDD_SPELL_OPTION_COMMON
      IDD_SPELL_OPTION_GENERAL
      IDD_SPELL_OPTION_USER
    5. Include the "FPSSpellCheckerInclude.h" file in your stdafx.h file.
      #include "FPSSpellCheckerInclude.h"
    6. Place a standard edit control on a form or dialog resource and give it a unique control id (ie. ID_TEST_EDIT)
    7. Add a member variable of type CFPSSpellingEditCtrl to the dialog/form class file (ie. m_editTest)
    8. In the OnInitDialog function, call the
      AttachEdit
      
      member function of CFPSSpellingEditCtrl (ie.
      m_editTest.AttachEdit(this,
          ID_TEST_EDIT);

    Known Issues

    1. Performance is still not as good as it needs to be.
    2. Language support is limited to US English.
    3. The EditDistance function needs work.
    4. The MetaphoneEx function needs work.
    5. There is a painting problem with the edit control when scrolling the control while the spelling error "squigly" lines are displayed.
    6. No complete support for rich edit control.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Web Developer
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionCRichEditCtrl Pin
    andwan05-Nov-12 0:16
    andwan05-Nov-12 0:16 
    GeneralVS 2005 Version Pin
    napadaan11-Nov-09 0:40
    napadaan11-Nov-09 0:40 
    QuestionWhat about a WTL version of the spellchecker Pin
    ceekays10-Sep-09 23:38
    ceekays10-Sep-09 23:38 
    GeneralGreat Article Pin
    Anthony Daly17-Aug-09 0:47
    Anthony Daly17-Aug-09 0:47 
    GeneralApplication crash. Pin
    Member 40859497-Aug-09 0:40
    Member 40859497-Aug-09 0:40 
    QuestionWhat about spell checking in WTL Pin
    ceekays18-Nov-08 4:49
    ceekays18-Nov-08 4:49 
    GeneralIgnore All Does Not Work Pin
    c-sharp6-Jan-05 9:33
    c-sharp6-Jan-05 9:33 
    GeneralSpelling Checker Research Pin
    ain288212-Sep-04 19:46
    ain288212-Sep-04 19:46 
    GeneralCommercial version released Pin
    Adam@Spellican9-Apr-03 4:14
    Adam@Spellican9-Apr-03 4:14 
    GeneralRe: Commercial version released Pin
    .dan.g.13-May-04 13:25
    professional.dan.g.13-May-04 13:25 
    GeneralRe: Commercial version released Pin
    Anonymous12-Sep-04 21:03
    Anonymous12-Sep-04 21:03 
    GeneralRe: Commercial version released Pin
    Thomas Holz14-Feb-11 0:38
    Thomas Holz14-Feb-11 0:38 
    QuestionCan I use your dictionary on my school project? Pin
    tomc7-Apr-03 16:23
    tomc7-Apr-03 16:23 
    AnswerRe: Can I use your dictionary on my school project? Pin
    Matt Gullett7-Apr-03 16:28
    Matt Gullett7-Apr-03 16:28 
    GeneralNEED OF UK DICTIONARY Pin
    laksammu27-Dec-02 0:21
    laksammu27-Dec-02 0:21 
    GeneralHelp Needed Pin
    Pushparaj12-Aug-02 23:35
    Pushparaj12-Aug-02 23:35 
    GeneralDouble Metaphone and Related Algos Pin
    Suresh Limaye17-Apr-02 9:56
    Suresh Limaye17-Apr-02 9:56 
    GeneralRe: Double Metaphone and Related Algos Pin
    Mike Nordell17-Apr-02 10:06
    Mike Nordell17-Apr-02 10:06 
    GeneralRe: Double Metaphone and Related Algos Pin
    Matt Gullett18-Apr-02 0:26
    Matt Gullett18-Apr-02 0:26 
    GeneralOther languages Pin
    17-Mar-02 6:31
    suss17-Mar-02 6:31 
    GeneralRe: Other languages Pin
    Matt Gullett18-Apr-02 0:31
    Matt Gullett18-Apr-02 0:31 
    GeneralRe: Other languages Pin
    alex.barylski7-Oct-04 23:14
    alex.barylski7-Oct-04 23:14 
    GeneralTrouble installing... Pin
    Cless Averin13-Dec-01 23:45
    Cless Averin13-Dec-01 23:45 
    QuestionHow do I get change notifications back? Pin
    13-Dec-01 10:11
    suss13-Dec-01 10:11 
    AnswerRe: How do I get change notifications back? Pin
    AdamJTP2-Sep-02 4:26
    AdamJTP2-Sep-02 4:26 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.