In: Computer Science
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):
Prompt the user for the name of the file containing the dictionary of correctly spelled words.
Read in the name of the given dictionary file.
Open an input file stream associated with this filename.
If the file associated with filename successfully opens:
For each word in the dictionary file:
Add the word to a “dictionary” LinkedSet.
Close the dictionary file.
Do while the user wants to spell check more files:
Prompt the user for the name of a file to spell check.
Read in the name of the given file.
Open an input file stream associated with this file.
If the file successfully opens:
For each word in the file:
Clean the word of extraneous characters.
Convert the word to lowercase.
Add the word to a “fileBeingSpellChecked” LinkedSet.
Close the file being spell checked.
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).
Display the contents of the “difference” LinkedSet.
Clear the contents from the “fileBeingSpellChecked” LinkedSet.
Clear the contents from the “difference” LinkedSet.
Prompt the user whether they want to spell check another file.
Get their response.
Quit.
/*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*/