Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A Spell Checker- help in code 2

Status
Not open for further replies.

pkakkar

Programmer
Apr 24, 2008
15
0
0
FI
Hello,

I am making a spell checker and I am a beginner in Delphi.

Well, I have 2 forms - Form1 has a MemoBox, and 3 buttons (Open File, Check File and Save File). When I click on Open file, the selected file is loaded in MemoBox and when I click on check file. It should start checking for spelling checks. and saving a file..shud save the file. In my Form 2, I have 2 edit boxes for 'Not in dictionary' and 'replace with' and 2 buttons 'Ignore' and 'Change'.

Now I am stuck with 'Check file' code in Form 1. As soon as click on this button, the cursor should go to MemoBox. and it should start checking word by word from my 'list_of_words.text'. In case it found or not , Form 2 shoudl show up.

I am not able to write code for this.
I have just written this and dont know how to proceed. Pls suggest.

procedure TForm1.BitBtn3Click(Sender: TObject);
var
Txt:string;
StartPos, TxtLen, FoundPos:integer;
begin
try
While not EOF(MemoBox.text) do
begin
StartPos:=MemoBox.SelStart+MemoBox.SelLength

//I think it will go char by char and as soon as it will find a space '', it will treat it as a word and then look up in the 'list_of_words.text' file.

Regards
Pooja
 
Well I'm not sure what you are looking to do, precisely. But I'll give you a hint. What you want to do is go through each string you encounter until you find a space, and then consider that a word to be checked.

To look at the first sentence I wrote, I can process it by doing something like:

Code:
CurrentPos := 1;
RestString := MyString;
position := pos(' ', RestString);
myword := copy(RestString, CurrentPos, Position-1);

MyWord then should be "Well". Then to get ready for the next word, I can do something like:

Code:
CurrentPos := Position + 1;
RestString := Copy(RestString, CurrentPos, Length(RestString)-CurrentPos);

RestString should now be the whole line minus the word we just did.

All untested of course, and it could be more elegant, but it'll give you a start.

Dilbert is not a fictional cartoon. It is a documentary.
 
If myword is not found in your dictionary, you could use:
Code:
MessageDlg(myword + ' Not found', MtWarning, [MBOK],0);

Steven
 
Hello Glenn,

I tried using what you have said. instead of making a function, i wanted to see first if this piece fo code working correctly so i added showMessage(myWord)..but it shows nothing...
I actually wanted that it reads the text loaded in MemoBox word by word and checks from my dictionary.txt and display if found or not.
Have i done this correctly??


procedure TForm1.BitBtn3Click(Sender: TObject);
var
i,CurrentPos, position:integer;
RestString, myWord:string;

begin
CurrentPos:=1;
for i:=0 to MemoBox.Lines.Count-1 do begin
RestString:=MemoBox.Lines;
position:=pos('', RestString);
myWord:=copy(RestString, CurrentPos, position-1);
showMessage(myWord);
CurrentPos:=position+1;
RestString:=copy(RestString, CurrentPos, Length(RestString)-CurrentPos);
end;


end;

 
There are several problems with your code.

Use the debugger to single step through your code and examine your local variables at each line and you should be able to determine what is wrong.

At present, you are assuming that words are separated by a space. I would imagine that you should also cater for words terminated by punctuation characters such as comma, period, colon, semi colon, exclamation marks, tabs and so on.

Also, have you considered what happens at the end of each line?

You might find it simpler to process the MemoBox using its text property. If your definition of a word is something that contains the characters 'A' to 'Z' and 'a' to 'z' then your code might then look something like:
Code:
procedure TForm1.BitBtn3Click(Sender: TObject);
const
  letters = [ 'A'..'Z', 'a'..'z' ];
var
  x: integer;
  item: string;
begin
  item := '';
  for x := 1 to length(MemoBox.Text) do begin
    if MemoBox.Text[x] in letters then
      item := item + MemoBox.Text[x]
    else begin
      if Length(item) > 0 then
        ShowMessage( item );
      item := '';
    end;
  end;
end;


Andrew
Hampshire, UK
 
Thanks perfect!!!

yes i know there are so many mistakes in my code and i m still a learner...but I truly hope I become perfect in this like you guys..

Regards
Pooja
 
Hello,

can u also tell me what if I have to search these 'item's in my WordList.text file one by one. If not found then display 'word not found' else 'No spelling error found.'

Regards
Pooja
 
i tried making a TString List and storing the WordList file contents in that.

and tried using Find function.

but what if the wordlist has 'the' and the text written in my memo has 'hte'..how can i write code to recognise these spellign errors and correcting them..

regards
Pooja
 
hello ,

not able to get what i wanted using 'find ' function. is there any other way to search the words in my Memo with the WordList.text file. If not found then display 'word not found' else 'No spelling error found.'



regards
Pooja
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top