Spellchecker

Hi everyone/anyone. I just found this website via friend. I'm having trouble with an assignment that needs to read in a file that contains a large list of words. The read in a letter of some kind (without punctuation) to then check each word against my list of words to see if they are spelled correctly. I'm using a binary search function, but I'm having issues sending the entire dictionary to the search. I'll post my actual assignment outline if anyone can help. Any input is appreciated!





here it is:


Assignment – the spell checker

Write and test a program to serve as a primitive spelling checker. This program should read the words in a text file and report or list all misspelled words to the screen.

To keep this program simple, you may create a text file without punctuation. For example, here is a file called letter.txt:

Dear mom
Please send more monie and fud for my cat
Love John

When given this file as input, your program should report that monie and fud are misspelled words.

To accomplish program, use a text file containing a list of correctly spelled words – a dictionary (English_words.txt). This small example dictionary file contains less than 15,000 words. A word is considered misspelled if its lower-case equivalent is not found in the dictionary. So, your program will have the following skeletal pseudocode;

open the dictionary file
read all dictionary words into an array
open the letter file
read a word from letter
repeat until there are no words left in the letter
convert the word to all lower-case
if this word is not in the dictionary array, output the word with the phrase “misspelled!”
read another word from the letter

Naturally, the statement “not in the dictionary” is not a simple operation. If you think about it for a minute, determining if the word is in the dictionary file requires a binary search.

Use the <cstring> library of functions to implement your program (see chapter 10, sections 1-4).







And here is what I have so far:
(that is, if anyone has the patience to try and understand it)


#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <stdio.h>
#include <ctype.h>


using namespace std;
const int LENGTH=30;
int BinarySearch (char ary[][LENGTH], int size, char search_item[LENGTH]);

int main ()
{
int n=0, l=0, w=0, pos;
//const int LENGTH=30;
char dict[15000][LENGTH];
char word[LENGTH+1];
char c;
ifstream fin;
fin.open ("dictionary.txt");


for (int len=0; len<=LENGTH; len++)
word[len]=NULL;
if (fin.fail())
{
cout << "Cannot find dictionary file" << endl;
}

// The next chunk reads in the file "dictionary" into a char array

for (n=0; !fin.eof(); n++)
{
fin.getline(dict[n],LENGTH);
}
fin.close();

//for (l=0; l<n; l++)
// cout << dict[l] << endl;



ifstream letter;
letter.open ("letter.txt");

if (letter.fail())
{
cout << "Cannot find draft file" << endl;
}

// Here is where I try and read in the test letter one word at a time,
// then make each letter in the word lowercase before I search for the
// word using the BinarySearch function.

for (l=0; !letter.eof(); l++)
{
letter >> word;
for (int a=0; word[a]!=NULL; a++)
{
while (word[a])
{

c=word[a];
word[a]= (tolower(c));
a++;
}
}

pos=BinarySearch(dict, n, word);
if (pos==-1)
{
cout << word << " misspelled!!" << endl;
}

}
letter.close();






return 0;
}

int BinarySearch (char ary[][LENGTH], int size, char search_item[LENGTH])
{
int first = 0, last = size-1;
bool found = false;
int position = -1, middle;
while (!found && first <= last)
{
middle = (first + last) / 2;
if ((strcmp(ary[middle], search_item))==0)
{
position = middle;
found = true;
}
else if (ary[middle] < search_item)
first = middle+1;
else last = middle-1;
}
return position;
}
Last edited on
Personally, I think it's easy. If the words in the dictionary were each on a line, then I would make a string array, read the file into this array, then do this (Comparing string from dictionary with string from your file)
1
2
3
4
5
6
7
8
for(Index2=1;Index2<=[/*Number of Words in File*/];Index2++)
{
    for(Index=1;Index<=15000;Index++)
    {
        if(strDICTIONARY[Index].compare(strYOURFILE[Index2]) != 0)
        cout<<strYOURFILE[Index2]<<" is Misspelled"<<endl;
    }
}


Now I haven't read your file because I'm sleepy. But if this is what you have done, I think it should work.
Last edited on
It...is...FINISHED!!!! MUAHAHA four freaking days on the thing and I kept forgetting to change a relational operator...
... How do you delete a forum chat that's done?
I don't think you can. You can edit your first code and delete it if you don't want anyone to take it.
Just edit your topic name and replace the start of it with [SOLVED]
Topic archived. No new replies allowed.