/******************************************************* PROGRAM NAME - buildDict FILE NAME = buildDict.cxx PROGRAMMER NAME - Justin C. Miller USAGE = type './buildDict' to run me COMPILE = type 'make buildDict' to use my makefile DATE - 10-10-2003 BUGS - I believe that everything works properly DESCRIPTION - This program reads in any number of files from the user and creates a dictionary using the words in those files. The dictionary is then placed in a file called "words.dat". WARNING: so if a file named "words.dat" already exists in your directory it will be overwritten! *******************************************************/ #include // cout, cin #include // ifstream, ofstream #include // EXIT_SUCCES, EXIT_FAILURE #include // tolower, isalpha #include "apvector.h" // so I can create apvector objects #include "wordList.h" // so I can create wordList objects //#define DEBUG // for debugging purposes, to turn of, comment out using namespace std ; // so I don't have to type std::cout // function prototypes (tell main what functions it can use) void buildDictionary(wordList &, const string &); void getFilenames(apvector &) ; void insertWord(wordList &, string &) ; void writeToFile(const wordList &, const string &); // main function, where my program starts int main(int argc, char * argv[]){ wordList dictionary ; // create an empty dictionary apvector filenames ; // create a vector of filenames string out_filename = "words.dat" ; // file to write to getFilenames(filenames) ; // get filenames from user for(int i = 0 ; i < filenames.size() ; i++) // use each file buildDictionary(dictionary, filenames[i]) ; // to build dictionary writeToFile(dictionary, out_filename) ; // write to file return EXIT_SUCCESS ; }//end of main function // function definitions below... // ----------------------------- // name : buildDictionary // parameters : // dictionary - a word List to add words to from filename // filename - the file that contains a bunch of words // pre : dictionary can be empty or non-empty, it doesn't matter // filename should exist and have something in it // post : puts all the words in filename into the dictionary wordList void buildDictionary(wordList & dictionary, const string & filename){ ifstream in_file(filename.c_str(), ios::in) ; // open input file if(!in_file.is_open()) { cout << "\n" << filename << " couldn't be opened.\n" ; return ;} string word ; while(in_file >> word) // read everything in the file insertWord(dictionary,word) ; // place each word into the dictionary }//end of buildDictionary // name : getFilenames // parameters : // filenames - collection of filenames // pre : the collection of filenames could be empty or non-empty, doesn't matter // post : gets filenames from user using stdin, places them into collection void getFilenames(apvector & filenames){ string input ; while(true){ // read from user until they type 'continue' cout << "\nEnter a filename (or 'continue' to build the dictionary):" ; cin >> input ; // read from user if(input == "continue") break ; // they typed 'continue' filenames.push_back(input) ; // add to list of filenames }//while }//end of getFilenames // name : insertWord // parameters : // dictionary - a collection of words // word - a word to be inserted into dictionary // pre : dictionary could be empty or non-empty, doesn't matter // word could exist or not-exist in dictionary, doesn't matter // word does not have to be properly formatted // post : word is properly formatted and if it doesn't exist // in the dictionary, then it is inserted, else nothing void insertWord(wordList & dictionary, string & word){ if(word.length() < 2) return ; // check for single letter words int start = 0, end = 0 ; string sub_word ; #ifdef DEBUG cout << "original word: " << word << endl ; #endif for(int i = 0 ; i < word.length() ; i++){ for(; isalpha(word[i]) != 0 ; i++,end++) // skip thru characters word[i] = (char) tolower(word[i]) ; // 1st lowercase them sub_word = word.substr(start, end-start) ; start = end = i + 1 ; if(sub_word.length() < 2) continue ; // skip 0 or 1 letter sub words #ifdef DEBUG cout << "\tword inserting: " << sub_word << endl ; #endif if(dictionary.insert(sub_word) == false){ // insert sub word #ifdef DEBUG cout << "\t--" << sub_word << " already in dictionary" << endl ; #endif }//if }//for }//end of insertWord // name : writeToFile // parameters : // dictionary - collection of words to be written to out_filename // out_filename - name of file to write the dictionary to // pre : dictionary is assumed to have some words in it // out_filename is assumed to not exist, otherwise it will overwrite existing file // post : all words in dictionary are written to out_filename // overwriting original out_filename if it did exist void writeToFile(const wordList & dictionary, const string & out_filename){ ofstream out_file(out_filename.c_str(), ios::out) ; // open file if(!out_file.is_open()) {cout << "\n" << out_filename << " couldn't be created.\n" ; exit(EXIT_FAILURE) ;} out_file << dictionary ; // write to file using overloaded << out_file.close() ; // close file cout << "\n\n--Dictionary (with " << dictionary.numberWords() << " words) written to " << out_filename << "--" << endl ; }//end of writeToFile