Question

In: Computer Science

Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this...

Using C++,

Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp.

The only required constructor for this class is

KeywordsInFile( filename_with_keywords, filename_with_text)

  • filename_with_keywords is a name of a plain text file that contains the list of keywords. For the future reference, the number of words in this file is denoted by N.
  • filename_with_text is a name of a plain text file that contains the text where the keywords must be found. For the future reference, the number of words in this file is denoted by M.

Consider a word to contain only upper- and lower- alphabet symbols. Any non-alphabet symbol is considered a separator.

Your class should be case-sensitive. So, “sun” and “Sun” are two different words.
The processing of the files is done during the object construction. All the data needed for the methods below is stored in the object.

The processing should be done in O(N+M) time. Consider string hashing to be O(1) for this assignment.

The number of keywords can be large. The text can be large. Number of keywords in the text can be very large or very small. Design your class accordingly.

Disable the default non-parametrized constructor.

The following methods should be implemented:

KeywordFound( keyword ) – returns true if the specified keyword was found in the text. Returns false otherwise. The method should run in O(1) time.

KeywordInLine( keyword, line_number ) – returns the number of times the specified keyword was seen in the specified line of the text. The method should run in at most O(log L) time, where L is the number of times the keyword was found in the text.

TotalOccurrences( keyword ) – returns the total number of the occurrences of the given keyword in the text. The method should run in at most O(log L) time, where L is the number of times the keyword was found in the text.

Overload operator<< that allows you to use object of your class with cout. When in the program I use command cout

Solutions

Expert Solution


--------------------------------------------------------------------------------
//KeywordsInFile.h

bool KeywordFound(string str)
{

}
int KeywordInLine(string str,int line)
{
  
}
int TotalOccurrences(string str)
{
  
}
void operator<<(KeywordsInFile obj)
{
  
}


--------------------------------------------------------------------------------
//KeywordsInFile.cpp.
#include<KeywordsInFile.h>
class KeywordsInFile
{
    public:
    vector<string> Keywords;
    unordered_map<string,int> store;
    vector<vector<string>> v;
    KeywordsInFile(const char *file filename_with_keywords,const char *filename_with_text)
    {
        int i;
        fstream newfile;
      
        newfile.open(filename_with_keywords,ios::in);
        if (newfile.is_open())
        {
            string tp;
            while(getline(newfile, tp))
            {
                string s="";
                for(i=0;i<tp.length();i++)
                {
                    if((tp[i]>='a'&&tp[i]<='z')||(tp[i]>'A'&&tp[i]<='Z'))
                    {
                        s+=string(1,tp[i]);
                    }
                    else
                    {
                        Keywords.push_back(s);
                        s="";
                    }
                }
            }
        }
        newfile.close();
      
        newfile.open(filename_with_text,ios::in);
        if(newfile.is_open())
        {
            string tp;
            while(getline(newfile, tp))
            {
                vector<string> v1;
                string s="";
                for(i=0;i<tp.length();i++)
                {
                    if((tp[i]>='a'&&tp[i]<='z')||(tp[i]>'A'&&tp[i]<='Z'))
                    {
                        s+=string(1,tp[i]);
                    }
                    else
                    {
                        v1.push_back(s);
                        store[s]+=1;
                        s="";
                    }
                }
                v.push_back(v1);
            }
        }
      
    }
    bool KeywordFound(string str)
    {
        if(stored[str]>=1)
        return true;
        return false;
    }
    int KeywordInLine(string str,int line)
    {
        int i,count;
        count=0;
        for(i=0;i<v[line].size();i++)
        {
            if(v[line][i]==str)
            count+=1;
        }
        return count;
    }
    int TotalOccurrences(string str)
    {
        return stored[str];
    }
    void operator<<(KeywordsInFile obj)
    {
        int i;
        cout<<"keywords in "<<filename_with_keywords<<endl;
        for(i=0;i<keywords.size();i++)
        {
            cout<<keywords[i]<<" ";
        }
        cout<<endl;
        cout<<"words in "<<filename_with_text<<endl;
        for(it=stored.begin();it!=stored.end();it++)
        {
            cout<<it->first<<" ";
        }
        cout<<endl;
    }
};

int main()

{

}


Related Solutions

JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
C++ Code Required to Show The constructor of parent class executes before child class
C++ Code Required to Show The constructor of parent class executes before child class
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
using correct c++ syntax, in one part write a short header file with only one constructor...
using correct c++ syntax, in one part write a short header file with only one constructor and one private variable. in another part write the function definition for the same constructor of the header file you just wrote. using a correct c++ syntax, write only the top line of a derived header file that uses a base class. write the function definitions of two constructors in this derived class
Write the code to create a class Square implementing the interface Figure with constructor to initialize...
Write the code to create a class Square implementing the interface Figure with constructor to initialize with a size and toString method. Write another class SquareUser that creates and array of Squares and initializing with sides 1, 2,3, 4 and 5. Also write the code to call the toString method of squares in a loop to print the string for all squares in the array.
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor with parameters, a copy constructor, a destructor, and an overloaded assignment operator (=). //main.cpp #include <iostream> using namespace std; #include "Party.h" int main() { return 0; } //party.h #ifndef PARTY_H #define PARTY_H class Party { private: string location; string *attendees; int maxAttendees; int numAttendees;    public: Party(); Party(string l, int num); //Constructor Party(/*parameters*/); //Copy constructor Party& operator=(/*parameters*/); //Add destructor void addAttendee(string name); void changeAttendeeAt(string...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s...
Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s flavour and the number of scoops Create an IceCreamCone class with two fields - flavor and scoops The IceCreamCone constructor calls two data entry methods — getFlavor() and getScoops() The getScoops() method throws an IceCreamConeException when the scoop quantity exceeds 3 Write a program that establish three TceCreamCone objects and handles the Exception
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT