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

using c++ using classes and file io Part I In this part of the assignment, you a

ID: 3606843 • Letter: U

Question

using c++ using classes and file io

Part I

In this part of the assignment, you are to create a class, SpellChecker. You will define some class data members, member methods and helper functions. The class methods will be used to check the spelling of words and assess the word count across documents. Elements of this assignment are intentionally vague; at this point in the semester, you should be able to make your own decisions about appropriate data structures for storing and looking up data, as well as defining helper functions. You can assume that your code will never be storing more than 10,000 valid or corrected words. SpellChecker should have at least the following Public members:

string language: the name of the language this spell checker is using (i.e. “English”, “Spanish”, "Italian", "Hindi", ...) SpellChecker should have at least the following Private members:

char start_marker: used for marking the beginning of an unknown word in a string

char end_marker: used for marking the end of an unknown word. SpellChecker should have three constructors:

Default Constructor, the one with no arguments.

Second constructor that takes a string for the object's language.

Third constructor that takes a string for the object's language and two filenames as parameters. The first filename specifies the file with correctly spelled words and the second filename specifies the misspelled words with their corrections. You will be dealing with two different files:

The data in the first filename supplies a list of correctly spelled words, one word per line:

The data in the second filename contains a list of misspelled words and their correct spellings. The word and its correction are separated by a tab character (‘ ’): It is very important you understand the format of this file. The correctly spelled words may have spaces in them! For example a file that converts common texting abbreviations into words: The constructor with the filename arguments should open the files and read them into an appropriate data members of the class. To find if a word is a valid spelling or is a misspelling, you should think about storing the words in the right structure so that it’s easy to search and access it. SpellChecker should also include the following public methods:

bool readValidWords(string filename): this method should read in a file in exactly the same way as detailed in the description of the constructor. This file will have the format specified for correctly spelled words. This method should return a boolean of whether or not the file was successfully read in. This method should add the words from the file to the list of words already contained in the object.

bool readCorrectedWords(string filename): this method should read in a file in exactly the same way as detailed in the description of the constructor. The file will have the format specified for the wrongly spelled words and their corrected spellings. This method should return a boolean of whether or not the file was successfully read in. This method should add the words from the file to the list of words already contained in the object.

Setters and Getters for the markers to be used for unknown words (see description of marker use below).

return true for the settters if the new marker has been accepted

bool setStartMarker(char begin)

bool setEndMarker(char end)

char getStartMarker()

char getEndMarker()

string repair(string sentence): Repair will take in a string of multiple words, strip out all the punctuation, ignore the case and return the sentence with all misspellings replaced or marked. For example: here are what the following calls would return: If you cannot find a word in the list of valid words or in the list of misspelled words (for instance, if the word is misspelled beyond recognition), you should just return the misspelled words with the start_marker in front and the end_marker at the end. For example: if start_marker and end_marker are both ‘~’, the call:

Testing

Testing of your class and all of its methods is now in your hands. You must determine the test cases that will test if your implementation returns the correct results in all conditions. For example, you would need to write code that will declare SpellChecker objects with each of the possible constructors, to verify that each of those methods will create and initialize the object correctly. The same must be done for each of the other public methods to verify that your implementation works correctly in all possible conditions and ordering of calls to those methods. Once you are satisfied that your code works as intended, submit it to COG for its evaluation.

Part II

In this part of the assignment, you are to create a class, WordCounts. You will define some class data members, member methods and helper functions. The class methods will be used keep a running count of the number of times each word is being used. You can assume that there will never be more than 10,000 unique words being counted. Your class will provide the following public methods to support counting word usage: void tallyWords(string sentence): This function will take in a string of multiple words, remove the punctuation, and increment the counts for all words in the string. If a word is not already in the list, add it to the list. This function is used to keep a running count of each unique word processed; that means multiple calls to the function should update the count of the words, not replace them. If we call the function three times: The count for the words “the” and “fox” should be 2, the count for the words “brown”, ”red”, ”blue”, “cat”, and “teh” should be 1.

int getTally (string word): return the current count of the given word. If the word is not found to be in the current list of words, return 0.

void resetTally(): reset all word counts to zero.

int mostTimes(string words[], int counts[], int n): find the n most common words in the text that has been counted and return those words and counts in via the arrays given as parameters. Assume the arrays are large enough to hold the number of elements requested.

Testing

Testing of your class and all of its methods is now in your hands. You must determine the test cases that will test if your implementation returns the correct results in all conditions. For example, you would need to write code that will declare WordCounts objects with each of the possible constructors, to verify that each of those methods will create and initialize the object correctly. The same must be done for each of the other public methods to verify that your implementation works correctly in all possible conditions and ordering of calls to those methods. Once you are satisfied that your code works as intended, submit it to COG for its evaluation.

Explanation / Answer

#include <iostream>
#include <conio.h>
#include <fstream>
#include <cstring>

using namespace std;
int main()
{
Int item[5]
Int I
ifstream inFile;
inFile.open("C:\temp\datafile.txt");
Char str[30];
Cin.get(str,31);

  
Edit & Run
You do realize why nobody is helping you out, right? This assignment is obviously a number of weeks into the course and your code makes it blatantly obvious that you have not even attempted this assignment. In fact, it makes me wonder if you even go to class.

That being said, I will still help you out; if you put some effort into it. Start by accepting the file name from the command line; you can do a search for examples on this site.  

#include <iostream>
#include <conio.h>
#include <fstream>
#include <cstring>

using namespace std;
int main ( int argc, char *argv[] )
{
if ( argc != 2 )
cout<<"usage: "<< argv[0] <<" <C:\temp\datafile.txt> ";

  
Edit & Run

good so far ?

That's a start. Assuming that return 0; } follows. I also don't see any reason to include cstring or conio.h at this point.

  

#include <iostream>
#include <conio.h>
#include <fstream>
#include <cstring>

using namespace std;
int main ( int argc, char *argv[] )
{
if ( argc != 2 )
cout<<"usage: "<< argv[0] <<" <C:\temp\datafile.txt> ";
return 0;
}
{
std::ifstream file;
file.open(argv[1]);
}

  
Edit & Run
Don't forget to close the ifstream after its use.

Also, I just noticed, there will be two ifstreams; one to read in a hard-coded dictionary data file and then the one that opens the command line argument file to be spell-checked.

well he said that i dont have to make it fast..
the file with the dictionary is datafile.txt

i thought what i already coded was the dictionary alrdy in so i would just need to somehow add a command line that will let a user input words that r to be
#include <iostream>
#include <conio.h>
#include <fstream>
#include <cstring>
string words;
using namespace std;
int main ( int argc, char *argv[] )
{
if ( argc != 2 )
cout<<"usage: "<< argv[0] <<" <C:\temp\datafile.txt> ";
return 0;
}
{
std::ifstream file;
file.open(argv[1]);
}
{
cout <<''enter words to be checked by spell checker";
getline(cin, words)
cout << words << "you typed"
}

  
Edit & Run


I was under the same impression, but after I re-read the assignment, I think the program should:

1. load datafile.txt into a data structure
  

ifstream data( "datafile.txt" ); // hard-coded dictionary file
// load words into data structure
data.close();

  


2. process the argv[1] file word by word and look up the words in that data structure

ifstream input( argv[1] ); // the file to be spell checked passed on the command line
string word;
while( input >> word )
{
    // look up word in dictionary structure
}
input.close();

  


If you want to accept a string entered by the user for now, that will work until you get it working...it's up to you.

The testing for the appropriate number of arguments and outputting the usage is also a good idea; just be sure to test it.

#include <iostream>
#include <conio.h>
#include <fstream>
#include <cstring>
string words;
using namespace std;
int main ( int argc, char *argv[] )
{
if ( argc != 2 )
cout<<"usage: "<< argv[0] <<" <C:\temp\datafile.txt> ";
return 0;
}
{
ifstream data( "datafile.txt" );
data.close();
}
{
cout <<''enter words to be checked by spell checker";
getline(cin, words)
cout << words << "you typed"
}
ifstream input( argv[1] ); // the file to be spell checked passed on the command line
string word;
while( input >> word )
{
}
input.close();

  
Edit & Run