Question

In: Computer Science

I am having trouble with a C++ code that I'm working on. It is a spell...

I am having trouble with a C++ code that I'm working on.

It is a spell checker program.

It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled.

All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into the second array must be cleaned of !@#$%^&*()_-+={}[]:;"'`<>,.?/|\ and made to be lowercase before the array is compared to the dictionary.

The algorithm for the main driver should go (something) like this (you are free to adjust this algorithm as needed):

  1. Prompt the user for the name of the file containing the dictionary of correctly spelled words.

  2. Read in the name of the given dictionary file.

  3. Open an input file stream associated with this filename.

  4. If the file associated with filename successfully opens:

    1. For each word in the dictionary file:

      1. Add the word to a “dictionary” LinkedSet.

    2. Close the dictionary file.

  5. Do while the user wants to spell check more files:

    1. Prompt the user for the name of a file to spell check.

    2. Read in the name of the given file.

    3. Open an input file stream associated with this file.

    4. If the file successfully opens:

      1. For each word in the file:

        1. Clean the word of extraneous characters.

        2. Convert the word to lowercase.

        3. Add the word to a “fileBeingSpellChecked” LinkedSet.

      2. Close the file being spell checked.

    5. Create a “difference” LinkedSet that holds the difference between the “fileBeingSpellChecked” set and the “dictionary” set (note that the “difference” set will contain only misspelled words).

    6. Display the contents of the “difference” LinkedSet.

    7. Clear the contents from the “fileBeingSpellChecked” LinkedSet.

    8. Clear the contents from the “difference” LinkedSet.

    9. Prompt the user whether they want to spell check another file.

    10. Get their response.

  6. Quit.

Solutions

Expert Solution

/*spell checker function*/

#include"dictionary.h"

#include<fstream>

#include<string>

#include<iostream>

using namespace std;

void tries::add(string s)

{

tries* trav = this;

for(int i = 0; i<s.length(); i++)
{

int pos = tolower(s[i]-'a');

if(trav->pointers[pos]==NULL)
   trav->pointers[pos] = new tries;

trav = trav->pointers[pos];

   }

  


trav->value = true;

}

  


bool tries::searchtries(string word, int i)
{

if(i==word.length())

return value;

if(pointers[tolower(word[i])-'a']==NULL)
return false;
return pointers[tolower(word[i])-'a']->searchtries(word,i+1);
}

  


void tries::deletetries()
{
for(int i = 0; i<26; i++)
{

if(pointers[i]!=NULL)

pointers[i]->deletetries();

}

delete this;

}

  


bool dictionary::load()

{

fstream f;

f.open(dict,ios::in);

if(!f.is_open())

{

return false;

}

  
root = new tries;

string str;
while(!f.eof())
{

   f>>str;
   root->add(str);
}

  


f.close();

return true;

}

bool dictionary::check(string word)

{
   return root->searchtries(word,0);
}


void dictionary::unload(void)

{

root->deletetries();
}

/*main function in different file*/

#include <iostream>
#include "dictionary.h"
#include <fstream>
#include <cstdlib>
#include <conio.h>
#define DICTIONARY "dictionary.txt" //put the name of file representing the dictionary here

using namespace std;

/*
This function asks the user for a file name and checks that file for misspelled words
*/

void checkfile()
{
system("cls");

//loading the dictionary from file in the memory
dictionary d(DICTIONARY);

if(!d.load())
{
cout<<"could not load dictionary";
return;
}


char text[80]; //stores the name of the file to be checked

cout<<"Enter the name of file ";
cin>>text;

fstream f;
f.open(text,ios::in);

if(!f.is_open())
{
cout<<"could not open file";
//free up memory acquired by the dictionary words if the file does not exist
d.unload();
return;
}

cout<<"MISSPELLED WORDS: ";

string word;
f>>word;

while(!f.eof())
{
//Each word is checked against the set of words stored in dictionary and wrong words are printed out
bool misspelled = !d.check(word);
if(misspelled)
cout<<word<<endl;
f>>word;
}

f.close();
getch();
}

/*
This function saves the file typed by user. User signals the end of file by typing EXITTYPING
*/

void writefile()
{
system("cls");
char filename[80]; //stores the name of the file to be saved


fstream f;
char flag = 'y';

//take the name of file from user and if that file already exists ask if he/she wants to replace it
do
{
flag = 'y';
cout<<"save as filename(spaces not allowed):\t";
cin>>filename;

f.open(filename,ios::in);
if(f.is_open())
{
cout<<"File already exists\nDo you want to replace it?(y/n)\t";
cin>>flag;
f.close();
}
}while(flag=='n');

f.open(filename,ios::out);
if(!f.is_open())
{
cout<<"could not open file";
return;
}
cout<<"Enter text, type EXITTYPING and press enter to end file\n";

//taking the words from console and printing it in the file
string word;
cin>>word;
while(word.compare("EXITTYPING"))
{
f<<word<<" ";
cin>>word;
}
f.close();
cout<<"File saved!";
getch();
}

/*
This funciton adds a word to the dictionary
*/

void addword()
{
system("cls");
cout<<"Enter the word you wish to add\n";
string word;
cin>>word;

ofstream f;
f.open(DICTIONARY,ios::app);
if(!f.is_open())
cout<<"could not open"<<DICTIONARY;
f<<word<<"\n";

cout<<"Added!";
getch();
}

int main()
{
/*The program gives a menu to user where he/she can choose either to
check an existing file,
create a new file or
to add a word in the dictionary
*/
while(1)
{
system("cls");
cout<<"Enter your choice\n";
cout<<"1. Check a file\n2. Write a file\n3. Add word to dictionary\n4. Exit\n";

char choice;

choice = getch();
switch(choice)
{
case '1':
checkfile();
break;
case '2':
writefile();
break;
case '3':
addword();
break;
case '4':
return 0;
}
}

return 0;
}
/*create two different cpp files for both the program function and then compile*/


Related Solutions

Using dev c++ I'm having trouble with classes. I think the part that I am not...
Using dev c++ I'm having trouble with classes. I think the part that I am not understanding is sending data between files and also using bool data. I've been working on this program for a long time with many errors but now I've thrown in my hat to ask for outside help. Here is the homework that has given me so many issues: The [REDACTED] Phone Store needs a program to compute phone charges for some phones sold in the...
I am working through this solution in rstudio and am having trouble fitting this table into...
I am working through this solution in rstudio and am having trouble fitting this table into a linear regression analysis. an answer with corrosponding r code used would be greatly appreciated A study was conducted to determine whether the final grade of a student in an introductory psychology course is linearly related to his or her performance on the verbal ability test administered before college entrance. The verbal scores and final grades for all 1010 students in the class are...
I'm having trouble figuring out the constraints to this problem. I know that I am maximizing...
I'm having trouble figuring out the constraints to this problem. I know that I am maximizing 55x + 45y, however the variables are throwing me off. I only need help on question #1 as it would be a great help to understanding the rest of what the questions are asking. The problem is as follows: NorCal Outfitters manufactures a variety of specialty gear for outdoor enthusiasts. NorCal has decided to begin production on two new models of crampons: the Denali...
I am working on these study questions and am having trouble understanding how it all works...
I am working on these study questions and am having trouble understanding how it all works together. Any help would be greatly appreciated!! An all equity firm is expected to generate perpetual EBIT of $50 million per year forever. The corporate tax rate is 0% in a fantasy no tax world. The firm has an unlevered (asset or EV) Beta of 1.0. The risk-free rate is 5% and the market risk premium is 6%. The number of outstanding shares is...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed to change the parameter list in the functions. This is what I have so far, written in C. So far I've been getting segfaults, and I feel like it's because I'm off by 1, somewhere... void merge_the_array(int *list, int l1, int h1, int l2, int h2){ // create a temp array for l1-h1 int arr_a[(h1-l1)+1]; // create a temp array for l2-h2, adding 1...
I'm having trouble with validating this html code. Whenever I used my validator, it says I...
I'm having trouble with validating this html code. Whenever I used my validator, it says I have a stray end tag: (tbody) from line 122 to 123. It's the last few lines. Thanks and Ill thumbs up whoever can help solve my problem. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <title>L7 Temperatures Fields</title> <!--    Name:    BlackBoard Username:    Filename: (items left blank for bb reasons    Class Section: (blank)    Purpose: Making a table to demonstrate my...
I am currently having trouble understanding/finding the errors in this python code. I was told that...
I am currently having trouble understanding/finding the errors in this python code. I was told that there are 5 errors to fix. Code: #!/usr/bin/env python3 choice = "y" while choice == "y": # get monthly investment monthly_investment = float(input(f"Enter monthly investment (0-1000):\t")) if not(monthly_investment > 0 and monthly_investment <= 100): print(f"Entry must be greater than 0 and less than or equal to 1000. " "Please start over.")) #Error 1 extra ")" continue # get yearly interest rate yearly_interest_rate = float(input(f"Enter...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do this for me. The template for the lab is below which you must use. You're only supposed to create/edit the product function. The assignment has to be written in asm(Mips) You will need to create a NOS table and use the structure below and write a function called product. The structure used in this program is: struct Values { short left; short right; int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT