I l@ve RuBoard |
![]() ![]() |
Exercise 3.1Write a program to read a text file. Store each word in a map. The key value of the map is the count of the number of times the word appears in the text. Define a word exclusion set containing words such as a, an, or, the, and, and but. Before entering a word in the map, make sure it is not present in the word exclusion set. Display the list of words and their associated count when the reading of the text is complete. As an extension, before displaying the text, allow the user to query the text for the presence of a word. #include <map> #include <set> #include <string> #include <iostream> #include <fstream> using namespace std; void initialize_exclusion_set( set<string>& ); void process_file( map<string,int>&, const set<string>&, ifstream& ); void user_query( const map<string,int>& ); void display_word_count( const map<string,int>&, ofstream& ); int main() { ifstream ifile( "C:\\My Documents\\column.txt" ); ofstream ofile( "C:\\My Documents\\column.map" ); if ( ! ifile || ! ofile ){ cerr << "Unable to open file -- bailing out!\n"; return -1; } set<string> exclude_set; initialize_exclusion_set( exclude_set ); map<string,int> word_count; process_file( word_count, exclude_set, ifile ); user_query( word_count ); display_word_count( word_count, ofile ); } void initialize_exclusion_set( set<string> &exs ){ static string _excluded_words[25] = { "the","and","but","that","then","are","been", "can","a","could","did","for", "of", "had","have","him","his","her","its","is", "were","which","when","with","would" }; exs.insert( _excluded_words, _excluded_words+25 ); } void process_file( map<string,int> &word_count, const set<string> &exclude_set, ifstream &ifile ) { string word; while ( ifile >> word ) { if ( exclude_set.count( word )) continue; word_count[ word ]++; } } void user_query( const map<string,int> &word_map ) { string search_word; cout << "Please enter a word to search: q to quit"; cin >> search_word; while ( search_word.size() && search_word != "q" ) { map<string,int>::const_iterator it; if (( it = word_map.find( search_word )) != word_map.end() ) cout << "Found! " << it->first << " occurs " << it->second << " times.\n"; else cout << search_word << " was not found in text.\n"; cout << "\nAnother search? (q to quit) "; cin >> search_word; } } void display_word_count( const map<string,int> &word_map, ofstream &os ) { map<string,int>::const_iterator iter = word_map.begin(), end_it = word_map.end(); while ( iter != end_it ){ os << iter->first << " ( " << iter->second << " )" << endl; ++iter; } os << endl; } Here is a small piece of text processed by the program. I removed the punctuation from the text because our program does not handle punctuation: MooCat is a long-haired white kitten with large black patches Like a cow looks only he is a kitty poor kitty Alice says cradling MooCat in her arms pretending he is not struggling to break free Here is a snapshot of the interactive session initiated by user_query(). Notice that although a occurs two times the text, it is an entry in the excluded word set and is not entered in the map of words found in the text. Please enter a word to search: q to quit Alice Found! Alice occurs 1 times. Another search? (q to quit) MooCat Found! MooCat occurs 2 times. Another search? (q to quit) a a was not found in text. Another search? (q to quit) q |
I l@ve RuBoard |
![]() ![]() |