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...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
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
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
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.
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 C# ONLY~~~ Create a console application Design a class named Person with properties for holding...
~~~USING C# ONLY~~~ Create a console application Design a class named Person with properties for holding a person’s name, address, and telephone number. Design a class named Customer, which is derived from the Person class. The Customer class should have the variables and properties for the customer number, customer email, a spentAmount of the customer’s purchases, and a Boolean variable indicating whether the customer wishes to be on a mailing list. It also includes a function named calcAmount that calculates...
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 the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT