Implementing a Spell Checker

From Apache OpenOffice Wiki
Jump to: navigation, search



A sample implementation of a spell checker is found in the examples for linguistics.

The spell checker implements the following interfaces:

and

To implement a spell checker of your own, modify the sample in the following ways:

Choose a unique service implementation name to distinguish your service implementation from any other. To do this, edit the string in the line

>
  public static String _aSvcImplName = "com.sun.star.linguistic2.JavaSamples.SampleSpellChecker";

Then, specify the list of languages supported by your service. Edit the

  public Locale[] getLocales()

function and modify the

  public boolean hasLocale( Locale aLocale )

function accordingly. The next step is to change the

  private short GetSpellFailure(...)

as required. This function determines if a word is spelled correctly in a given language. If the word is OK return -1, otherwise return an appropriate value of the type com.sun.star.linguistic2.SpellFailure.

Check if you need to edit or remove the

  private boolean IsUpper(...)

and

  private boolean HasDigits(...)

functions. Consider this only if you are planning to support non-western languages and need sophisticated versions of those, or do not need them at all. Do not forget to change the code at the end of

  public boolean isValid(...)

accordingly.

Supply your own version of

  private XSpellAlternatives GetProposals(...)

It provides the return value for the

  public XSpellAlternatives spell(...)

function call if the word was found to be incorrect. The main purpose is to provide proposals for how the word might be written correctly. Note the list may be empty.

Next, edit the text in

  public String getServiceDisplayName(...)

It should be unique but it is not necessary. If you are developing a set of services, that is, spellchecker, hyphenator and thesaurus, it should be the same for all of them. This text is displayed in dialogs to show a more meaningful text than the service implementation name.

Now, have a look in the constructor

  public SampleSpellChecker()

at the property names. Remove the entries for the properties that are not relevant to your service implementation. If you make modification, also look in the file PropChgHelper_Spell.java in the function

  public void propertyChange(...)

and change it accordingly.

Set the values of bSCWA and bSWWA to true only for those properties that are relevant to your implementation, thus avoiding sending unnecessary com.sun.star.linguistic2.LinguServiceEvent events, that is, avoid triggering spell-checking in clients if there is no requirement.

Finally, after registration of the service (see Deployment Options for Components) it has to be activated to be used by the LinguServiceManager. After restarting Apache OpenOffice, this is done in the following manner:

Open the dialog Tools → Options → Language Settings → Writing Aids. In the section Writing Aids, in the box Available Language Modules, a new entry with text of the Service Display Name that you chose is displayed in the implementation. Check the empty checkbox to the left of that entry. If you want to use your module, uncheck any other listed entry. If you want to make more specific settings per language, press the  Edit  button next to the modules box and use that dialog.

The Context menu of the Writer that pops up when pressing the right-mouse button over an incorrectly spelled word currently has a bug that may crash the program when the Java implementation of a spell checker is used. The spell check dialog is functioning.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages