Man Versus Code

The personal website of Joe Marrero.

Spell Checker v1.0

This is a program that checks a specified text or word document for spelling errors. It works by reading in a dictionary file and then checking each word in the document to see if it exists in the dictionary. It uses MFC for the GUI components and is written in C++.

How do you build a map of misspelled words to their corresponding line numbers?

First you have to build a list of words in a dictionary. Then you read in the document line by line parse each line for tokens that are seperated by whitespace. Each token could possibly be a word, so the token must be checked for a punctuation mark at the end ( . ? , ; : ) and removed accordingly. A token is a word, if after any punctuation is removed, it is entirely made up of alphabet characters. Furthermore, the word should be optionally converted to lowercase. To build the misspellings map, each token must be verified to be a word and then checked if it exists in the dictionary. If it does not exist, the word should be added to the map as a key and the line number should be added to a vector as a value.

Code Snippet: Building the dictionary list

string currentWord, currentLine;
istringstream st;
if( !m_fin ) return;
while( getline( m_fin, currentLine ) )
{
   	st.str( currentLine );
    	while( st >> currentWord )
   	{
   		string tmp( strlwr( (char *)currentWord.c_str() ) );
   		m_Dictionary.insert( tmp );
   	}
   	st.clear( );
}

Code Snippet: Building the misspelled words map

string currentWord, currentLine;
unsigned int lineNumber = 1;
istringstream st;
if( !m_fin ) return;

while( getline( m_fin, currentLine ) )
{
   	st.str( currentLine );
   	while( st >> currentWord )
   	{
   		currentWord = removePunctuation( currentWord );
    		if( currentWord != "" &&
   		    m_Dictionary.find( currentWord ) == m_Dictionary.end( ) )
  		{
  			m_misspelledWords[ currentWord ].push_back( lineNumber );
   		}
  	}

   	st.clear( );
   	lineNumber++;
}


Screenshots

Screenshot 23 Image