Click here to Skip to main content
15,883,705 members
Articles / Desktop Programming / MFC

Spell Checker Dictionary Engine

Rate me:
Please Sign up or sign in to vote.
4.86/5 (34 votes)
20 May 2003CPOL6 min read 118.5K   4.6K   70  
A dictionary engine for use in applications requiring spell checking
// Dictionary.h: interface for the Dictionary classes.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DICTIONARY_H__B9D80D57_E7CF_4D7E_91D9_6C79A83DFE0C__INCLUDED_)
#define AFX_DICTIONARY_H__B9D80D57_E7CF_4D7E_91D9_6C79A83DFE0C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

enum WordMatch
{
    eMatchPerfect,          // Word is a perfect match
    eMatchCapitalisedFirst, // Word matches except first character, which is capitalised (could be valid if at start of sentence)
    eMatchMixedCase,        // Word match found, but incorrect case
    eMatchNone,             // No match found
    eMatchInternalError     // Something went wrong!
};

class CNode
{
public:
    // Construction / destruction
    CNode(char c = '\0');
    virtual ~CNode();

    // Word operations
    void InsertWord(LPCSTR szWord);
    void IsWordListed(LPCSTR szWord, bool bMatchCase, WordMatch & match, bool bIsFirstNode, CNode ** ppFinalNode = NULL);
    void GetPatternMatchingWords(LPCSTR szWord, CStringArray & straSuggestions, CString strWordSoFar = "");

    void GetWordCount(int & nCount);
    
    // Serialisation
    void Serialise(CArchive & ar);

public:
    char m_Character;
    CNode * m_pAlternative; // "Left" node, i.e. if this is not the right character
    CNode * m_pNext; // "Right" node, i.e. if this is the right character
};

class CDictionary  
{
public:
	CDictionary();
	virtual ~CDictionary();

    // Word operations
    void InsertWord(LPCSTR szWord);
    bool RemoveWord(LPCSTR szWord);
    WordMatch IsWordListed(LPCSTR szWord);
    int GetSuggestions(LPCSTR szWord, CStringArray & straSuggestions, bool bCaseSuggestionsOnly);
    int PatternMatchWord(LPCSTR szWord, CStringArray & straSuggestions);
    int GetWordCount();

    // File operations
    bool LoadDictionary(const CString & strFilename);
    bool SaveDictionary(const CString & strFilename);
    bool CreateFromList(const CString & strFilename, bool bAppend = false);

    static WordMatch GetBestMatch(WordMatch match1, WordMatch match2) { return min(match1, match2); };

private:
    void SortSuggestions(CStringArray & straSuggestions);
    void RemoveDuplicates(CStringArray & straSuggestions);

    CNode * m_pRootNode;
};

#endif // !defined(AFX_DICTIONARY_H__B9D80D57_E7CF_4D7E_91D9_6C79A83DFE0C__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
I started computer programming on the Spectrum (writing nothing more complicated than "Hello World" and a few programs that tunelessly Beeped ad infinitum) but then progressed to slightly more serious programming on the Amiga.

After A-Levels in Maths, Physics and Chemistry, I went to the University of East Anglia, Norwich, and studied beer, women and Computing Science.
Some years after graduating, I still have an appreciation of Computing Science, but as I am now married, my other studies are frowned upon.

Since graduating, I have worked on many diverse projects in areas including call centres, logistics, architecture and engineering, and heritage.

Comments and Discussions