Using Spellchecker

From Apache OpenOffice Wiki
< Documentation‎ | DevGuide
Revision as of 14:15, 9 August 2021 by DiGro (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search



The interface used for spell checking is com.sun.star.linguistic2.XSpellChecker. Accessing the spellchecker through the LinguServiceManager and initializing the mxSpell object is done by:

  /** Get the SpellChecker to be used.
   */
  public boolean GetSpell()
      throws com.sun.star.uno.Exception,
      com.sun.star.uno.RuntimeException
  {
      if (mxLinguSvcMgr != null)
          mxSpell = mxLinguSvcMgr.getSpellChecker();
      return mxSpell != null;
  }

Relevant properties

The properties of the LinguProperties service evaluated by the spell checker are:

Spell-checking Properties of com.sun.star.linguistic2.LinguProperties Description
IsIgnoreControlCharacters Defines if control characters should be ignored or not.
IsUseDictionaryList Defines if the dictionary-list should be used or not.
IsGermanPreReform Defines if the new German spelling rules should be used for German language text or not.
IsSpellUpperCase Defines if words with only uppercase letters should be subject to spellchecking or not.
IsSpellWithDigits Defines if words containing digits or numbers should be subject to spellchecking or not.
IsSpellCapitalization Defines if the capitalization of words should be checked or not.

Changing the values of these properties in the LinguProperties affect all subsequent calls to the spell checker. Instantiate a com.sun.star.linguistic2.LinguProperties instance and change it by calling setPropertyValue(). The changes affect the whole office unless another modifies the properties again. This is done implicitly when changing the linguistic settings through Tools → Options → Language Settings → Writing Aids.

The following example shows verifying single words:

  // test with correct word
  String aWord = "horseback";
  boolean bIsCorrect = mxSpell.isValid( aWord, aLocale, aEmptyProps );
  System.out.println( aWord + ": " + bIsCorrect );
 
  // test with incorrect word
  aWord = "course";
  bIsCorrect = mxSpell.isValid( aWord, aLocale , aEmptyProps );
  System.out.println( aWord + ": " + bIsCorrect );

The following example shows spelling a single word and retrieving possible corrections:

  aWord = "house";
  XSpellAlternatives xAlt = mxSpell.spell( aWord, aLocale, aEmptyProps );
  if (xAlt == null)
      System.out.println( aWord + " is correct." );
  else
  {
      System.out.println( aWord + " is not correct. A list of proposals follows." );
      String[] aAlternatives = xAlt.getAlternatives();
      if (aAlternatives.length == 0)
          System.out.println( "no proposal found." );
      else
      {
          for (int i = 0; i < aAlternatives.length; ++i)
              System.out.println( aAlternatives[i] );
      }
  }

For a description of the return types interface, refer to com.sun.star.linguistic2.XSpellAlternatives.

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