Help with a Rudimentary Speller

Hi. I want to create a speller that will read in a txt file called Plain and read in a dictionary txt file called words. Then I want to check each word in the text file to see if it is spelled correctly or not by comparing each word to every word in the dictionary. If it isn't in the dictionary, it would be misspelled and outputted. FYI: all i know so far in C++ is stream, arrays, loops, structures, classes, if statements, variables, and functions.

Code so far:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;

int main()
{
ifstream Plain;
ifstream words;

Plain.open("C:\\Users\\Kevin Nguyen\\Desktop\\Plain.txt");
if (Plain.fail())
{
cout << "Can not open file Plain.txt." << endl;
return EXIT_FAILURE;
}

words.open("C:\\Users\\Kevin Nguyen\\Desktop\\words.txt");
if (words.fail())
{
cout << "Can not open file words.txt." << endl;
return EXIT_FAILURE;
}

const int Dictionary_Size(50000);
int n(0);
int c;
string dict[Dictionary_Size];
string word;
string modified;

while (words>>dict[n++])
;

while (Plain>>word)
{
for (int a=0;a<word.length();++a)
{
if (!ispunct(word[a]))
modified = modified + word[a];
else if (isupper(word[a]))
{
word[a] = tolower(word[a]);
modified = modified + word[a];
}
}

for (c=0;c<Dictionary_Size;++c)
{
if (dict[c]==modified)
{
cout << modified;
}
}

cout << "Mispelled words are:" << endl;

if (modified!=dict[c])
cout << modified << endl;

}
return EXIT_SUCCESS;
}
Last edited on
I don't see you building your dictionary first, which I would want to do before opened the Plain file.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// set up the max size of the dictionary.
const int Dictionary_Size(50000);
ifstream words;
string dict[Dictionary_Size];
int nArrayIndex = 0;

words.open("C:\\Users\\Kevin Nguyen\\Desktop\\words.txt");
if(words.good())
{
       // lets read in the dictionary.
       while(!words.eof());
       {
              // I don't know if I need to normalize these words to all upper or lower case.
              words >> dict[nArrayIndex];  // fill the array with words.
              // increment the index.
              nArrayIndex++;
           
       }
       cout << "Dictionary has " << nArrayIndex+1 << "words in it." << endl;
}
else
{
      cout << "Had a problem opening words!!" << endl;
}


After I read in my dictionary I would open plain, and looping my reads to the end of file. After reading each word I would normalize it, convert it to upper or to Lower case which ever matched my dictionary. Then I would scan the dictionary with each read in word and see it was there or not. In my example, nArrayIndex is the number of words read in, which assumes only that I didn't know how many words were in the Words file.

I hope you can see the direction I am pointing you in.

Last edited on
Topic archived. No new replies allowed.