Page 1 of 1

Example of LS-speller implementation

Posted: Sat Apr 01, 2006 10:00 pm
by Stef
hi all,

here an example of the implementation of LS-speller for tRichViewEdit.
The dialog of LS-speller is not used, but a normal popup-menu. The popupmenu is (partial) dynamically extended with suggested words, in case of a misspelling. The popupmenu can also contain the "actions" add word to dictionary and ignore word (stored in a local ignorelist). Upon add or ignore the complete document is re-scanned. The popup menu can of course also hold the normal items, like cut / paste etc.

have fun,
Stef Mientki

Code: Select all

procedure TForm3.RichViewEdit1SpellingCheck(Sender: TCustomRichView;
  const AWord: String; StyleNo: Integer; var Misspelled: Boolean);
(*******************************************************************************
*******************************************************************************)
begin
  misspelled:=false;
  if spellchecker_ignorelist.IndexOf(Aword)>=0 then exit;

  misspelled:=not(spellchecker1.IsKnownWord(Aword,Spellchecker1.Language));
end;

procedure TForm3.SpellLanguageComboBox1Change(Sender: TObject);
(*******************************************************************************
*******************************************************************************)
begin
  //total
  RichViewEdit1.StartLiveSpelling;
end;

procedure TForm3.PopupMenu1Popup(Sender: TObject);
(*******************************************************************************
*******************************************************************************)
var
  aword :string;
  alist :tstringlist;
  i     :integer;
  newitem :tmenuitem;
  style :integer;
  misspelled :boolean;
begin
  //remove all previous suggestions
  with PopupMenu1 do
  begin
    for i:=items.count-1 downto 0 do
      if items[i].Tag>0 then items.Delete(i);
  end;

  if richviewedit1.GetCurrentMisspelling(true,aword,style) then
  begin
    alist:=tstringlist.Create;
    //we've to call IsKnownWord, otherwise the last error typed word is tested
    spellchecker1.IsKnownWord(Aword,Spellchecker1.Language);
    spellchecker1.GetVariants(Aword,alist,Spellchecker1.Language);

    for i:=0 to alist.count-1 do
    begin
      NewItem:=TMenuItem.Create(PopupMenu1);
      NewItem.Caption:=alist[i];
      NewItem.Tag:=i+1;
      NewItem.OnClick:=N27.OnClick;
      PopupMenu1.Items.Insert(i,NewItem);
    end;
    alist.Free;
  end;
end;


procedure TForm3.N27Click(Sender: TObject);
(*******************************************************************************
*******************************************************************************)
begin
  // if it's an item from the list and the selection exists,
  // replace the word
  if (tmenuitem(sender).Tag>0) and richviewedit1.SelectionExists then
  begin
    richviewedit1.DeleteSelection;
    richviewedit1.InsertText(tmenuitem(sender).Caption,false);
  end;
end;


procedure TForm3.AddToDictionary1Click(Sender: TObject);
(*******************************************************************************
*******************************************************************************)
var
  aword :string;
  style :integer;
begin
  if richviewedit1.GetCurrentMisspelling(true,aword,style) then
  begin
    spellchecker1.AddWord(aword,Spellchecker1.Language);
    RichViewEdit1.StartLiveSpelling;
  end;
end;


procedure TForm3.IgnoreWord1Click(Sender: TObject);
(*******************************************************************************
*******************************************************************************)
var
  aword :string;
  style :integer;
begin
  if richviewedit1.GetCurrentMisspelling(true,aword,style) then
  begin
    spellchecker_ignorelist.add(aword);
    RichViewEdit1.StartLiveSpelling;
  end;
end;


Posted: Sun Apr 02, 2006 9:20 am
by Sergey Tkachenko
Thank you, Stef.

One suggestion: it's not necessary to restart live spelling if the word is added to ignore list or dictionary. There is a special method for this case: LiveSpellingValidateWord

Posted: Sun Apr 02, 2006 10:39 am
by Stef
thanks for the tip Sergey,
it works great !

I didn't know that command.
I'm still trying to extend the example by adding "rating" to the suggested words, and using rating to color the menu-items. It's in the speller, but I don't know yet how to retrieve it. If I succeed, I'll post a complete example, which you might willing to place in the example section (only the administrator is allowed to do, which I can understand ;-)

cheers,