Question

In: Computer Science

Write a method ( C++ ) map occurance_map(const string path); that reads in an ascii text...

Write a method ( C++ )

map occurance_map(const string path);

that reads in an ascii text file and returns an assocation where each key is a word in the text file and each value is the number of occurances of that word. Ignore punctuation and numbers. The method should be case-insensitive and should store the keys as lowercase. A word is definited by an string consisting entirely of alpha-numeric characters or apostrophes (single quote characteris). For example, if the file contained

This is a sentence. Don't think of wier_d strings as words. Really, 123 is a nice number. 

you would include sentence, don't, and 123 but not ., ,, or wier_d. Using single quotes as quotes, as in

'I should use double quotes'

is a user error and will catch the "words"

'I
should
use
double
quotes'

Solutions

Expert Solution

CodeToCopy:

count_occurences.cpp

#include <iostream>     /* cout object */

#include <fstream>      /* for ifstream class */

#include <vector>       /* for vector class */

#include <map>          /* for map class */

#include <algorithm>    /* for using find() function */

#include <iomanip>    /* for using find() function */

/* Macros which define ascii values of characters 0,9,A,Z,a,z,',.,, */

#define ZERO_ASCII 48

#define NINE_ASCII 57

#define UPPERCASE_A_ASCII 65

#define UPPERCASE_Z_ASCII 90

#define LOWERCASE_A_ASCII 97

#define LOWERCASE_Z_ASCII 122

#define APOSTROPHE_ASCII 39

#define PERIOD_ASCII      46

#define COMMA_ASCII      44

#define SPACE_ASCII      32

#define NEWLINE_ASCII    10

using namespace std;

/* This function adds word to the map, if word does not exist.

* If word already exists, then it increases the frequency of

* the word */

void add_word(map<string, int> * occurences_map, string word) {

    /* finding word in the map */

    map<string, int> ::iterator it = occurences_map->find(word);

    /* checking if word is present or not */

    if (it == occurences_map->end()) {

        /* if word is not there, inserting word with frequency 1 */

        occurences_map->insert(make_pair(word, 1));

    }

    else {

        /* if word is there, increasing its frequency by 1 */

        it->second = it->second + 1;

    }

}

map<string, int> occurance_map(const string path) {

   

    /* stores the file name */

    const char * text_file_name = path.c_str();

   

    /* stores the letter */

    char ch;

   

    /* flag variables tells whether parsing a word is in progress or not */

    bool wordstart = false;

   

    /* string variable stores the word */

    string word;   

   

    /* map which stores words and their frequencies as a key-value pair */

    map<string, int> word_occurences;

   

    /* creating ifstream objects to open text file */

    ifstream text_file;

   

    /* opening Teams.txt file in read mode */

    text_file.open(text_file_name, ios::in);

   

    /* checking Teams.txt is opened or not */

    if (text_file.fail()) {

        /* if not, printing error and exiting the program */

        cout << "Error::: Teams.text: No such file exists. ";

        return word_occurences;

    }

   

    /* reading letter by letter from input text file */

    while(text_file >> noskipws >> ch) {

       

        /* condition that enables only parsing alpha numerics and apostrophe */

        if ((ch >= ZERO_ASCII && ch <= NINE_ASCII) ||

            (ch >= UPPERCASE_A_ASCII && ch <= UPPERCASE_Z_ASCII) ||

            (ch >= LOWERCASE_A_ASCII && ch <= LOWERCASE_Z_ASCII) ||

            (ch == APOSTROPHE_ASCII)) {

       

            /* changing word-start flag variable to true */

            wordstart = true;

           

            /* appending each character string variable */

            word.append(1u, ch);

        }

        else {

           

            /* checking for word ending characters such as SPACE, COMA, PERIOD */

            if ((ch == SPACE_ASCII || ch == PERIOD_ASCII || ch == COMMA_ASCII || ch == NEWLINE_ASCII) && wordstart == true) {

                /* if word ending character is found, then adding the parsed word to map */

                add_word(&word_occurences, word);

               

                /* resetting word-start flag to false */

                wordstart = false;

               

                /* resetting letters of string variable */

                word.clear();

            }

            else {

                /* check for unsupported characters when word processing is in progress */

                if (wordstart == true && ch != ' ') {

                   

                    /* parsing rest of the characters in the word and ignoring them */

                    string post_letters;

                    text_file >> post_letters;

                   

                    /* clearing stored characters */

                    word.clear();

                   

                    /* resetting letters of string variable */

                    wordstart = false;

                }

            }

        }

    }

   

    /* if string variable characters are not reset, then adding the stored

     * word in map */

    if (word.size()) {

        add_word(&word_occurences, word);

    }

   

    /* closing the file */

    text_file.close();

   

    /* returing map */

    return word_occurences;

}

/* main function */

int main(int argc, char **argv) {

    /* checking number of command line arguments for user input-file */

    if (argc != 2) {

        cout << "Usage: " << argv[0] << ": <input-text-file>" << endl;

        return 0;

    }

   

    /* calling occurance_map() function with path */

    map<string, int> occurences = occurance_map(argv[1]);   

   

   

    /* iterating map to print key-value pair values */

    map<string, int> ::iterator it = occurences.begin();

   

    /* printing word and frequencies with proper format */

    cout << setw(6) << right << "Word" << setw(20) << right <<"Frequency ";

    cout << setw(15) << left << "-------" << setw(3) << right <<"---------- ";

    for(; it != occurences.end(); ++it) {

        cout << setw(15) << left << it->first << " " << setw(5) << right << it->second << " ";

    }

   

    return 0;

}

OutputScreenshot:



Related Solutions

⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state,...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip) : StreetNumber(street), CityName(city), StateName(state), ZipCode(zip) {} std::string GetStreetNumber() const { return StreetNumber; } void SetStreetNumber(const std::string& street) { StreetNumber = street; } std::string GetCity() const { return CityName; } void SetCity(const std::string& city) { CityName = city; } std::string GetState() const { return StateName; } void SetState(const std::string& state) { StateName = state; } std::string GetZipCode() const { return ZipCode; }...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT