Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Terina BUrr
- //Assignment four
- //due 10/02/15
- //section one
- #include <iostream>
- #include <iomanip>
- #include <map>
- #include <algorithm>
- #include <fstream>
- using namespace std;
- int ITEM_W = 16;
- int NO_ITEMS = 3;
- void print_words( const map<string, int> &m );
- void clean_entry( const string &x, string &y );
- void get_words( map<string, int> &m, ifstream &y );
- int main(int argc, char** argv) {
- if ( argc < 2 ) {
- cerr << "command line argument: input-file-name" << endl;
- return 1;
- }
- ifstream infile(argv[1], ios::in);
- map<string, int> m;
- get_words( m, infile );
- infile.close();
- print_words( m );
- return 0;
- }
- /***************************************************************
- Function: print_words
- Use: prints out the clean words to the screen and prints the number of nonempty words and the number of distinct words in 3 columns
- Arguments: 1. m: map where clean words are
- Returns: nothing
- ***************************************************************/
- void print_words( const map<string, int>&m ) {
- typedef map<string, int> mapType;
- mapType::const_iterator i; //iterator to go through the map
- int count = 0;
- int words = 0;
- for( i = m.begin(); i!=m.end(); i++)
- {
- //using iterator goes through the map
- cout << left << setw(ITEM_W) <<i ->first<<": " << setw(NO_ITEMS)<< i->second;
- // prints out the word and its frequency
- count++;
- words += i->second;
- if(count == NO_ITEMS)
- {
- cout<<endl;
- count = 0;
- //if count = the number of items allowed then it starts a new line and count returns to 0
- }
- // if there is more than three words per line then start a new line
- }
- cout << endl << "no of words in input stream: " << words << endl;
- cout << "no of words in output stream: " << m.size() << endl;
- }
- /***************************************************************
- Function: clean_entry
- Use: this cleans a word from punctuation marks
- Arguments: 1. x: original dirty word with punctuation marks
- 2. y: the cleaned word without punctuation
- Returns: nothing
- ***************************************************************/
- void clean_entry( const string &x, string &y ) {
- int start = 0; // start pos of the clean in the dirty one
- int current = 0; // current pos of where it is in the loop
- int sl = 0; //length of the new string
- // Finds the start of x word
- while(start < (int) x.size())
- {
- if(isalnum(x[start]) )
- break;
- start++;
- }
- current = start;
- // Finds the length of y( clean word) /
- while(current < (int) x.size())
- {
- if(isalnum(x[current]) )
- {
- sl++;
- current++;
- }
- else
- break;
- }
- if(start< (int) x.size())
- y = x.substr(start, sl);
- //get clean word
- for(int i = 0; i < (int) y.size(); i++)
- {
- y[i] = tolower(y[i]);
- }
- //puts in lower case
- }
- /***************************************************************
- Function: get_words
- Use: this function removes punctuation marks by using clean entry and saves the clean words to the map m
- Arguments: 1. m: map where clean words go
- 2. y: file that the words are read from
- Returns: nothing
- ***************************************************************/
- void get_words( map<string, int> &m, ifstream &y ) {
- string dirty, clean;
- while(!y.eof()) // while there is an input
- {
- y>> dirty;
- clean_entry(dirty, clean); // calls the clean entry functiom
- if(clean.length() > 0 )
- {
- m[clean]++; // adds it to set if equal to zero
- }
- clean ="";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement