/* * Copyright (c) 2002 by Cliff Green. All rights reserved. Individual files * may be covered by other copyrights (as noted in the file itself). * * Redistribution and use in source and binary forms are permitted * provided that this entire copyright notice is duplicated in all such * copies. * * This software is provided "as is" and without any expressed or implied * warranties, including, without limitation, the implied warranties of * merchantibility and fitness for any particular purpose. */ //---------------------------------------------------------------------- // Source file: spellchk.cpp // Written by: Cliff Green, 2002 // Compiler: Metrowerks CodeWarrior Pro 6, g++ 3.0.4 // History: // Modified: Mar. 5, 2002 // By: Cliff Green // Comments: Spell checker application. //---------------------------------------------------------------------- // This commentheader supports documentation tools such as Doc++ and // Doxygen: http://www.doxygen.org/ //---------------------------------------------------------------------- /// Spell checker application (main) file. /** * Simple and straightforward spell checker with Soundex functionality. * Since this is meant for the C++ Intro class, there are not any * namespaces. */ #include "word.h" #include "dict.h" #include "parse.h" #include #include #include void displayUsage (char const* appName, char const* mainDictName); int main(int argc, char* argv[]) { char const* localDictName (0); char const* mainDictName ("defdict.txt"); char const* docName (0); // Very simple but easy command line parsing - a production application would // have additional error checking and tighten up some command possibilities switch (argc) { case 1: { displayUsage(argv[0], mainDictName); // left out a critical argument return 1; } case 2: { docName = argv[1]; break; } case 3: { docName = argv[1]; mainDictName = argv[2]; break; } case 4: { docName = argv[1]; mainDictName = argv[2]; localDictName = argv[3]; break; } default: { displayUsage(argv[0], mainDictName); // left out a critical argument return 1; } } // Done with command parsing, create dictionaries and fill them Dictionary mainDict; Dictionary localDict; { // Parse files, fill vectors of words, add them to dictionaries WordVec mainDictVec(parseFile(mainDictName)); if (mainDictVec.empty()) { std::cout << "Main dictionary file parsing error!" << std::endl; return 1; } // Fill up main dictionary object for (WordVecSizeType i (0); i != mainDictVec.size(); ++i) { if (!mainDict.insert(mainDictVec[i])) { std::cout << "Main dictionary insert error!" << std::endl; return 1; } } if (localDictName) { WordVec localDictVec(parseFile(localDictName)); if (localDictVec.empty()) { std::cout << "Local dictionary file parsing error!" << std::endl; return 1; } for (WordVecSizeType j (0); j != localDictVec.size(); ++j) { if (!localDict.insert(localDictVec[j])) { std::cout << "Main dictionary insert error!" << std::endl; return 1; } } } } // don't need local and main Word vectors any longer, let them destruct // Get document file words WordVec docFileVec (parseFile (docName)); if (docFileVec.empty()) { std::cout << "Doc file parsing error!" << std::endl; return 1; } // Seach through dictionaries with each Word in doc vector for (WordVecSizeType i (0); i != docFileVec.size(); ++i) { if (!mainDict.search (docFileVec[i])) { if (!localDict.search (docFileVec[i])) { std::cout << "Word not found in dictionary: " << docFileVec[i] << std::endl; WordVec const ss (mainDict.getSameSdx (docFileVec[i])); if (ss.size() > 1) { std::cout << " Words that sound alike: "; for (WordVecSizeType j (0); j != ss.size(); ++j) { std::cout << " " << ss[j]; } std::cout << std::endl; } } } } return 0; } void displayUsage (char const* appName, char const* mainDictName) { std::cout << std::endl; std::cout << "Usage: " << appName << " doc-file " << std::endl; std::cout << " doc-file: (required) document file to be checked" << std::endl; std::cout << " main-dict: (optional) main dictionary, default: " << mainDictName << std::endl; std::cout << " local-dict: (optional) local dictionary file" << std::endl; }