In: Computer Science
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)
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
--------------------------------------------------------------------------------
//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()
{
}