Assignment 4, C++ Intro - Spell Checker Application


This page last updated on: Feb. 25, 2002


Contents:


Due Date

Mar. 11, 2002 by end of day

Description

Implement command line and file parsing and use the Word and Dictionary classes to create the spell checker utility program.

The command line argument handling includes a required document file argument, while the main dictionary file argument is optional (defaulted to defdict.txt if not entered), and the local dictionary file is optional (see the syllabus for more information).

The application file parsing functionality will scan through the document and dictionary files, parsing each line for either dictionary or spell checking functionality.

Requirements

DOS-style commands and arguments will be used for this assignment and the spell checker app. To invoke the test program, the following command can be entered:
spellchk DocFile MainDictFile LocalDictFile
The document file is required, the main dictionary file is optional (use a default file if not specified), and the local dictionary file is also optional (if not specified, no local dictionary will be used). (A useful enhancement is to add a help option, which displays the command syntax and options to the user.) Handle all appropriate combinations.

The dictionary files contain multiple text lines, with each line containing one word. The document file can contain any kind of text and punctuation characters.

The main function consists of the following functionality:

  1. Parse the command line arguments
  2. Create two Dictionary objects, one for the main dictionary, one for the local dictionary
  3. Fill the two (or one, if local dictionary is not specified) dictionaries
  4. Parse the document file and test each Word against one or both Dictionary objects, and if the Word is not found in either Dictionary, display an appropriate message along with the Words that sound similar (according to the Soundex functionality)
Of course appropriate error handling must be implemented, such as invalid file names.

Name the source file spellchk.cpp. If the file parsing functionality is put into separate source files, name them parse.cpp and parse.h. The complete application consists of these source files plus the source files for the Word and Dictionary classes.

Internals

Some hints on implementing the command parsing - use character pointers instead of copying strings around:
  char const * localDictFname = 0;
  char const * mainDictFname = "defdict.txt";
  char const * docFname = 0;
Set the pointers to the appropriate argv entry.

Since much of the file parsing is similar, a function can be defined to perform it, with a prototype similar to:

#include <vector> // include std C++ library vector container class
bool parseFile ( char const * fName, std::vector<Word>& v );
The return value will indicate success or failure.

The parsing function will be passed a file name and an empty std::vector container object as parameters, and will insert Word objects into the container as they are parsed from the file. The function will perform appropriate error handling, including file open and read errors.

Inside the parseFile function, each word needs to be scanned and located correctly so that a Word object can be built and appended to the container. Since the Word class handles non-alphabetic characters and case sensitivity, it should be sufficient to locate the end of the text word in the input buffer and delimit it appropriately (with a null character) before passing it to the Word constructor. Use standard C++ I/O functions for file handling, using the following include files:

#include <iostream>
#include <fstream>
An I/O object of type std::ifstream can be created, passing in the name of the file as the constructor argument:
  std::ifstream inFile(fname);
Errors or end-of-file can be checked by invoking a conversion operator:
  if (inFile) { // inFile object is in a good state
The simplest approach to parsing the file is to use the std::fstream operator>> functionality to fill up a standard C++ library std::string object. As each std::string object is extracted, it will contain characters from the std::fstream up to a whitespace character:
#include <string> // C++ std lib string class

  std::string wordStr;
  while (inFile >> wordStr) { // do something with the string object
If there are one or more alphabetic characters, a Word object can then be constructed, relying on the Word constructor to skip the non-alpha characters. A std::string object provides read access to the internal data as a char const * with the c_str() member function (e.g. wordStr.c_str() ).

Explanations and more detail will be provided in class, if necessary.

Topics Learned

The power of layered design and implementation, the ability to abstract functional components, software reuse through pre-written C++ classes, generic typing with templates, container classes, and the importance of clean, simple interfaces are some of the topics learned from this assignment (and project).

This page constructed by Cliff Green, Copyright © 1998-2002.