Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ In this project you are going to implement a Bag class to function as a word

ID: 3694138 • Letter: C

Question

C++

In this project you are going to implement a Bag class to function as a word search engine program. The program allows users to find words among a given set of text files. The program takes a path to a directory as an input. The directory contains a set of files consisting of multiple words. A word can appear multiple times in a single file, or in multiple files. The program reads all the files, parses file content into words, and feeds the words to a Bag object.

You are also provided with bag.h and main.cpp. In bag.h, there are some public functions declared for you. Your task is to implement those functions in a source code file named bag.cpp. You are REQUIRED to use array(s) to store words and associated information in the bag. If you use data structures other than array(s), you will NOT be awarded any credit.

class bag

Bag (int max_num_words = 10000);

Constructor that takes an integer to denote the maximum number of words in a bag. Default value is 10000.

virtual ~Bag ();

Destructor that you need to release the dynamically allocated memory at the end of the object life cycle.

void insert(const string& word, const string& fileName, int count);

Inserts a word, the name of a file in which it appears, and the number of occurrences of the word in that file into the bag.

pair<string, int> lookupFirst(const string& word);

lookup for the first file in insertion order that contains the search word. A pair containing the filename and associated count is returned if the word is found in the bag. A pair with an empty string and 0 is returned if the word is not in the bag.

pair<string, int> Bag::lookupRandom(const string& word)

Similar to lookupFirst, but lookupRandom looks up a random file ( and associated count) that contains the word. Note that every file in which the word appears should have an equal chance to be returned.

void Bag::lookupAll(const string& word, vector< pair<string,int> >&items)

lookupAll finds all the files that contain the search word, and returns a vector of pairs of the file name and associated count.

You can add private member variables and functions at the corresponding places in bag.h, but you should NOT modify other parts of the header file

I need the LookupRandom and lookupAll functions any suggestions based upon the description of the functions? Here is what I have for lookupFirst function.

pair<string, int> Bag::lookupFirst(const string& word)
{

unsigned int index;

for (int i=0; i<used; i++)
   {
       if (word!=words[i])
           return pair<"",0>;
       else
           index = i;
           return pair<words[index], count[index]>;
   }

}

Explanation / Answer

It would have been great had you provided the other files. Still here i am writing how you should approach to solve this problem.

for lookupFirst function:

Here you need to return the first file which has this word .So you should go through the each file in the directory untill you get the first file which contains the word.

Direction to solve this.

int i=0;
while (i<directory_size){ // each i means goinf through each file
   //write a piece of code where you take each word of a line and check if it is equal to our "word". repeat it till the end of file. If the count is 0. then continue to the next file otherwise return this file with Pair<filename,count> and "break" the while loop.
}

lookupAll:

here you need to go through each file in the directory and calculate the count of the word appearance in the file. Make a pair and add it to the resulting vector.

vector<Pair<String,int>> result;

foreach file in the directory {

       //get the count of the word appearance P

      // if count>0 add it to vector result.add(P)

}

return result;