LMD-Tools SpellPack Guide


Spell Checker Component


TLMDSpellChecker component is the main component in LMD SpellPack, which provides spell checking functionality. The component provides public API interface for checking words for spelling, and as well, can be linked to some supported controls for live spelling functionality. To be usable, dictionary files should be specified, and required contained dictionaries should be activated.

 

Dictionaries

 

To make TLMDSpellChecker component usable, at least one spell dictionary should be active. Look here to learn how to configure dictionaries.

 

clip0002

 

The component can handle several *.oxt dictionary files at the same time. Each dictionary file contains one or more dictionaries. Dictionary files are exposed via Files published collection property. Collection provides all usual methods and properties for adding, removing and iterating items. Also, SetFileList and SetFromDir helpher methods are added to simplify files loading.

 

Each dictionary file is represented by TLMDDictFile class. It provides FileName and Active property, along with Open and Close helper methods, which can be used to open the file. In active state read file is kept in shared-read mode in OS file system. Active dictionary file also provides some meta-data properties with file description:

 

FileId

Publisher

Version

DisplayName

 

Collection of dictionaries read from active dictionary files is represented by Dictionaries overloaded method group. The methods supports different set of parameters, which allow to query for all dictionaries, specific dictionary by id, dictionaries of specific type, or active only dictionaries. Also, HasActiveDicts helper method is provided, which allow to test whether spell checker component has active dictionaries of specified types.

 

Supported *.oxt files may contain two kinds of dictionaries:

 

Spell (for spell check, analyze words, or for suggest variants of misspelled words)

Hyphenation (for hyphenate words)

 

They are represented by two classes - TLMDSpellDict and TLMDHyphDict correspondingly. However, these classes do not declare any public interface, but instead all useful stuff is declared in their base class: TLMDDictionary.

 

TLMDDictionary class provides Active property, along with Open and Close methods, which can be used to activate/deactivate the dictionary. As well it provides Source property, which refers to parent dictionary file object. ID, Name and Locales properties allow to get access to dictionary's meta-data.

 

Live Spelling

 

LMD SpellPack provides intergation of TLMDSpellChecker component with some text editing controls. The following controls are currently supported:

 

TLMDEdit

TLMDMemo

TLMDEditView (syntax edit)

Standard TEdit (beta)

Standard TMemo (beta)

 

Live spell feature, implemented for these controls, is the main package feature, which allows to show spell errors visually in real-time, while the user edits a text:

 

clip0003

 

clip0005

 

Additionally, word suggestions is added in control's default context menu.

 

LMD Controls

 

Supported LMD controls provide SpellChecker published property, which allow to link the control to TLMDSpellChecker component. As well, LMD controls provide Spelling property, which is of TLMDSpellObject type and allows to change some spelling related settings:

 

LiveSpell

Boolean property allow to enable/disable live spelling.

PopupMenu

Property allows to setup suggestions related context menu options.

WordSeparators

Property allows to override spell checker WordSeparators for specific control.

 

Also, TLMDSpellObject provides a number of methods, which allows to work with spell error under caret, and navigate caret to the first/next spell error:

 

MoveToFirstError

Move caret to the first spell error in the control.

MoveToNextError

Move caret to the first spell error in the control.

Suggest

Suggest words for replacement if misspelled word under the caret.

FillPopupMenu

Allow to fill user popup menu with current suggestions.

IsOnError

Returns boolean value, indicating whether the caret is on spell error.

GetError

Allows to get current spell error text.

SetError

Allows to replace error text.

 

TLMDEditView (syntax edit) also provides SyntaxTokens published property, which additionally allows to specify tokens that should be checked by spell checker. For example, we can choose to check spelling of comments and string literals only:

 

clip0007

 

Standard Controls (beta)

 

LMD SpellPack partially supports standard Delphi's TEdit and TMemo text fields. Since declaration of additional properties is not possible for standard Delphi's classes, TLMDSpellChecker provides another way to enable/disable spell checking using EnableControl, DisableControl and TryGetSettings static methods:

 

procedure TMainForm.FormCreate(Sender: TObject);

begin

  TLMDSpellChecker.EnableControl(Edit1, SpellChecker);

  TLMDSpellChecker.EnableControl(Memo1, SpellChecker);

end;

 

procedure TMainForm.SetLiveSpell(AValue: Boolean);

var

  so: TLMDSpellObject;

begin

  if TLMDSpellChecker.TryGetSettings(Edit1, so) then

    so.LiveSpell := AValue;

  if TLMDSpellChecker.TryGetSettings(Memo1, so) then

    so.LiveSpell := AValue;

end;

 

Despite the way is explicitly implemented for standard Delphi's controls, it will work with any supported controls. TLMDSpellChecker component provides Global static method to simplify linking with spell checker component is there is exactly one component used in application. Also, simplified EnableControl method version is provided to enable spell checking of control in mentioned global spell checker:

 

procedure TMainForm.FormCreate(Sender: TObject);

begin

  TLMDSpellChecker.EnableControl(Edit1);

  TLMDSpellChecker.EnableControl(Memo1);

end;

 

Suggestions in context menu feature is not yet implemented.

 

Spelling API

 

TLMDSpellChecker component provides public methods for spell related analysis, which can be used in custom code. These methods works on word based basis, checking single word at a time. Methods takes word as a string or a PChar. The later variant is just a performance optimization and implies that word is ended by terminating zero character. The following methods are provided:

 

Spell - checks spelling of specified word.

Suggest - generates a list of suggestions for replacement of misspelling word.

Analyze - word morphological analysis, please refer to "hunspell" library documentation.

Stem - word stemming, please refer to "hunspell" library documentation.

Hyphenate - word hyphenation.

 

Only Spell and Suggest are really used in LMD SpellPack internal code for live spelling. Other methods are provided "as is".

 

Multiple Active Dictionaries

 

As noted above, the component can have multiple active dictionaries at the same time. User defined dictionaries order (priorities) are not supported currently. Instead if several spell dictionaries is active the component re-order them automatically based on last checked word to optimize performance. The  list of suggestions is combined from all active dictionaries.