/******************************************************* PROGRAM NAME - spellCheck FILE NAME = spellCheck.cxx PROGRAMMER NAME - Justin C. Miller USAGE = type './spellCheck' to run me COMPILE = type 'make spellCheck' to use my makefile DATE - 10-10-2003 BUGS - I believe that everything works properly DESCRIPTION - This program reads in a file to be spell-checked. This program assumes that a dictionary has been created and resides in the current directory in a file called "words.dat" A dictionary is then built, and each word in the user's file is check to see whether it's spelled correctly or not. The user will be prompted to replace, replace all, ignore, ignore all, or exit upon each mispelled word. If the file the user gave us was called "mystory.txt" the spell-checked version will be sent to a file called "mystory.txt.out" WARNING: if a file called "mystory.txt.out already exists in your directory it will be overwritten! *******************************************************/ #include // cout, cin #include // ifstream, ofstream #include // EXIT_SUCCES, EXIT_FAILURE #include "wordList.h" // so I can create wordList objects #include "map.h" // so I can create map objects #define DEBUG // used for debugging, comment out to get rid of using namespace std ; // so I don't have to type std::cout void getUserFile(string&) ; void rebuildDictionary(wordList &, const string &) ; void spellCheckFile(wordList&, const string&) ; int main(int argc, char * argv[]){ wordList dictionary ; // create an empty dictionary string dict_filename = "words.dat" ; // where my dictionary is located string filename ; // user's filename to be spellchecked rebuildDictionary(dictionary, dict_filename) ; getUserFile(filename) ; spellCheckFile(dictionary, filename) ; return EXIT_SUCCESS ; }//end of main function void getUserFile(string& filename){ cout << "Please enter filename to be spell-checked: " ; cin >> filename ; // read filename from keyboard }//end of getUserFile void rebuildDictionary(wordList & dictionary, const string & dict_filename) { ifstream in_file(dict_filename.c_str(), ios::in) ; // open dictionary file if(!in_file.is_open()) {cout << "Dictionary file (" << dict_filename << ") couldn't be opened.\n"; exit(1) ;} string word ; while(in_file >> word) // read in all words of dictionary file dictionary.insert(word) ; // and insert them into wordlist in_file.close() ; // close the file }//end of rebuildDictionary void spellCheckFile(wordList& dictionary, const string& filename){ string user_input, extension = ".out", a = "a", i = "i", temp; map replacementWords ; char input ; // read one character at a time bool done ; // are we done spellChecking this word? int dummy,pos ; // dummy variable for search function below string word ; // put out spellChecked word string original ; // original word to place out if correct ifstream in_file(filename.c_str(), ios::in) ; // open user's file ofstream out_file((filename + extension).c_str(), ios::out) ; // open output file dictionary.insert(a); // insert a into dictionary dictionary.insert(i); // insert i into dictionary if(!in_file.is_open()) {cout << filename << " couldn't be opened.\n" ; exit(1) ;} if(!out_file.is_open()) {cout << "Couldn't create " << (filename + extension) << endl ; exit(1) ;} input = (char) in_file.get() ; while(in_file.good()){ if(!isalpha(input)) out_file.put(input) ; else{ temp = (char) tolower(input) ; word = "" + temp ; // create lowercase word temp = input ; original = "" + temp ; // keep original word input = (char) in_file.get() ; // DOES THIS CATCH e, o, u as mispelled? if(!in_file.good()) continue ; // if end of file don't do rest while(in_file.good() && isalpha(input)){ // 2 or more letters in word temp = (char) tolower(input) ; word += temp ; // add letter to lowercase word temp = input ; original += temp ; // add letter to original word input = (char) in_file.get() ; }//while if(replacementWords.search(word,pos) == true){ // is it in my map? if(word == replacementWords.getReplacement(pos)) // ignore all out_file << original ; else // replace all out_file << replacementWords.getReplacement(pos) ; }else if(dictionary.search(word, dummy) == false){ // word not found done = false ; do{ cout << "'" << original << "' not found in dictionary." << endl ; cout << "Do you want to replace (R), replace all (P), ignore (I), ignore all (N), or exit (E)? " << endl ; cin >> user_input ; if(user_input == "R" || user_input == "r"){ cout << "Please enter word to replace '" << original << "' : " ; cin >> user_input ; // get new word to replace original out_file << user_input ; // write out new word done = true ; }else if(user_input == "P" || user_input == "p"){ cout << "Please enter word to replace '" << original << "' : " ; cin >> user_input ; // get new word to replace original out_file << user_input ; // write out new word replacementWords.insert(word, user_input) ; done = true ; }else if(user_input == "I" || user_input == "i"){ out_file << original ; // write out original word done = true ; }else if(user_input == "N" || user_input == "n"){ out_file << original ; // write out original word replacementWords.insert(word, word) ; done = true ; }else if(user_input == "E" || user_input == "e"){ out_file << original ; if(in_file.good()) out_file.put(input) ; // make sure last wasn't eof // write out rest of file un-spellchecked input = (char) in_file.get() ; while(in_file.good()){ // write out rest of file out_file.put(input) ; input = (char) in_file.get() ; }//while cout << "Spell-Checked '" << filename << "' and placed it in '" << (filename + extension) << "'" << endl ; out_file.close() ; // close file in_file.close() ; // close file exit(EXIT_SUCCESS) ; // quit program }//elseif }while(!done) ; }else // word was in the dictionary out_file << original ; // write out original word, it was in dictionary if(!in_file.good()) continue ; // if end of file don't do rest out_file.put(input) ; // write out non-character }//else input = (char) in_file.get() ; // read next non-character }//while in_file.close() ; // close the file out_file.close() ; //close the file cout << "Spell-Checked '" << filename << "' and placed it in '" << (filename + extension) << "'" << endl ; }//end of checkUserFile