Difference between revisions of "Documentation/DevGuide/OfficeDev/Using Hyphenator"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Initial author Sun Microsystems, Inc.)
 
m
 
(6 intermediate revisions by 4 users not shown)
Line 6: Line 6:
 
|NextPage=Documentation/DevGuide/OfficeDev/Using Thesaurus
 
|NextPage=Documentation/DevGuide/OfficeDev/Using Thesaurus
 
}}
 
}}
{{DISPLAYTITLE:Using Hyphenator}}
+
{{Documentation/DevGuideLanguages|Documentation/DevGuide/OfficeDev/{{SUBPAGENAME}}}}
 +
{{DISPLAYTITLE:Using Hyphenator}}
 
The interface used for hyphenation is <idl>com.sun.star.linguistic2.XHyphenator</idl>. Accessing the hyphenator through the <code>LinguServiceManager</code> and initializing the <code>mxHyph</code> object is done by:  
 
The interface used for hyphenation is <idl>com.sun.star.linguistic2.XHyphenator</idl>. Accessing the hyphenator through the <code>LinguServiceManager</code> and initializing the <code>mxHyph</code> object is done by:  
 
<!--[SOURCE:OfficeDev/Linguistic/LinguisticExamples.java]-->
 
<!--[SOURCE:OfficeDev/Linguistic/LinguisticExamples.java]-->
 
+
<syntaxhighlight lang="java">
 
   /** Get the Hyphenator to be used.
 
   /** Get the Hyphenator to be used.
 
   */
 
   */
Line 20: Line 21:
 
       return mxHyph != null;
 
       return mxHyph != null;
 
   }
 
   }
 
+
</syntaxhighlight>
 
=== Relevant properties ===
 
=== Relevant properties ===
  
Line 56: Line 57:
 
The following example shows a word hypenated:  
 
The following example shows a word hypenated:  
 
<!--[SOURCE:OfficeDev/Linguistic/LinguisticExamples.java]-->
 
<!--[SOURCE:OfficeDev/Linguistic/LinguisticExamples.java]-->
 
+
<syntaxhighlight lang="java">
 
   // maximum number of characters to remain before the hyphen  
 
   // maximum number of characters to remain before the hyphen  
 
   // character in the resulting word of the hyphenation
 
   // character in the resulting word of the hyphenation
Line 71: Line 72:
 
               + " in " + xHyphWord.getHyphenatedWord() );
 
               + " in " + xHyphWord.getHyphenatedWord() );
 
   }
 
   }
 
+
</syntaxhighlight>
 
If the hyphenator implementation is working correctly, it reports a ''valid'' hyphenation position of 4 that is after the 'horse' part. Experiment with other values for nMaxLeading and other words. For example, if you set it to 4, no ''valid'' hyphenation position is found since there is no hyphenation position in the word 'horseback' before and including the 's'.
 
If the hyphenator implementation is working correctly, it reports a ''valid'' hyphenation position of 4 that is after the 'horse' part. Experiment with other values for nMaxLeading and other words. For example, if you set it to 4, no ''valid'' hyphenation position is found since there is no hyphenation position in the word 'horseback' before and including the 's'.
  
For a description of the return types interface, refer to<idl>com.sun.star.linguistic2.XHyphenatedWord</idl>.
+
For a description of the return types interface, refer to <idl>com.sun.star.linguistic2.XHyphenatedWord</idl>.
  
 
The example below shows querying for an alternative spelling. In some languages, for example German in the old (pre-reform) spelling, there are words where the spelling of changes when they are hyphenated at specific positions. To inquire about the existence of alternative spellings, the <code>queryAlternativeSpelling()</code> function is used:  
 
The example below shows querying for an alternative spelling. In some languages, for example German in the old (pre-reform) spelling, there are words where the spelling of changes when they are hyphenated at specific positions. To inquire about the existence of alternative spellings, the <code>queryAlternativeSpelling()</code> function is used:  
 
<!--[SOURCE:OfficeDev/Linguistic/LinguisticExamples.java]-->
 
<!--[SOURCE:OfficeDev/Linguistic/LinguisticExamples.java]-->
 
+
<syntaxhighlight lang="java">
 
   //! Note: 'aProps' needs to have set 'IsGermanPreReform' to true!
 
   //! Note: 'aProps' needs to have set 'IsGermanPreReform' to true!
 
   xHyphWord = mxHyph.queryAlternativeSpelling( "Schiffahrt",  
 
   xHyphWord = mxHyph.queryAlternativeSpelling( "Schiffahrt",  
Line 93: Line 94:
 
               + " in " + xHyphWord.getHyphenatedWord() );
 
               + " in " + xHyphWord.getHyphenatedWord() );
 
   }
 
   }
 
+
</syntaxhighlight>
 
The return types interface is the same as in the above example (<idl>com.sun.star.linguistic2.XHyphenatedWord</idl>).
 
The return types interface is the same as in the above example (<idl>com.sun.star.linguistic2.XHyphenatedWord</idl>).
  
 
The next example demonstrates getting possible hyphenation positions. To determine all possible hyphenation positions in a word, do this:  
 
The next example demonstrates getting possible hyphenation positions. To determine all possible hyphenation positions in a word, do this:  
 
<!--[SOURCE:OfficeDev/Linguistic/LinguisticExamples.java]-->
 
<!--[SOURCE:OfficeDev/Linguistic/LinguisticExamples.java]-->
 
+
<syntaxhighlight lang="java">
 
   XPossibleHyphens xPossHyph = mxHyph.createPossibleHyphens( "waterfall", aLocale, aEmptyProps );
 
   XPossibleHyphens xPossHyph = mxHyph.createPossibleHyphens( "waterfall", aLocale, aEmptyProps );
 
   if (xPossHyph == null)
 
   if (xPossHyph == null)
Line 104: Line 105:
 
   else
 
   else
 
       System.out.println( xPossHyph.getPossibleHyphens() );  
 
       System.out.println( xPossHyph.getPossibleHyphens() );  
 
+
</syntaxhighlight>
 
For a description of the return types interface, refer to <idl>com.sun.star.linguistic2.XPossibleHyphens</idl>.
 
For a description of the return types interface, refer to <idl>com.sun.star.linguistic2.XPossibleHyphens</idl>.
  
 
{{PDL1}}
 
{{PDL1}}
[[Category: Office Development]]
+
 
 +
[[Category:Documentation/Developer's Guide/Office Development]]

Latest revision as of 11:52, 3 January 2021



The interface used for hyphenation is com.sun.star.linguistic2.XHyphenator. Accessing the hyphenator through the LinguServiceManager and initializing the mxHyph object is done by:

  /** Get the Hyphenator to be used.
   */
  public boolean GetHyph()
      throws com.sun.star.uno.Exception,
      com.sun.star.uno.RuntimeException
  {
      if (mxLinguSvcMgr != null)
          mxHyph = mxLinguSvcMgr.getHyphenator();
      return mxHyph != null;
  }

Relevant properties

The properties of the LinguProperties service evaluated by the hyphenator are:

Hyphenating Properties of com.sun.star.linguistic2.LinguProperties
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.
HyphMinLeading The minimum number of characters of a hyphenated word to remain before the hyphenation character.
HyphMinTrailing The minimum number of characters of a hyphenated word to remain after the hyphenation character.
HyphMinWordLength The minimum length of a word to be hyphenated.

Changing the values of these properties in the Lingu-Properties affect all subsequent calls to the hyphenator.

A valid hyphenation position is a possible one that meets the restrictions given by the HyphMinLeading, HyphMinTrailing and HyphMinWordLength values.

For example, if HyphMinWordLength is 7, "remove" does not have a valid hyphenation position. Also, this is the case when HyphMinLeading is 3 or HyphMinTrailing is 5.

The following example shows a word hypenated:

  // maximum number of characters to remain before the hyphen 
  // character in the resulting word of the hyphenation
  short nMaxLeading = 6;
 
  XHyphenatedWord xHyphWord = mxHyph.hyphenate( "horseback", aLocale, nMaxLeading , aEmptyProps );
  if (xHyphWord == null)
      System.out.println( "no valid hyphenation position found" );
  else
  {
      System.out.println( "valid hyphenation pos found at " + xHyphWord.getHyphenationPos() 
              + " in " + xHyphWord.getWord() );
      System.out.println( "hyphenation char will be after char " + xHyphWord.getHyphenPos() 
              + " in " + xHyphWord.getHyphenatedWord() );
  }

If the hyphenator implementation is working correctly, it reports a valid hyphenation position of 4 that is after the 'horse' part. Experiment with other values for nMaxLeading and other words. For example, if you set it to 4, no valid hyphenation position is found since there is no hyphenation position in the word 'horseback' before and including the 's'.

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

The example below shows querying for an alternative spelling. In some languages, for example German in the old (pre-reform) spelling, there are words where the spelling of changes when they are hyphenated at specific positions. To inquire about the existence of alternative spellings, the queryAlternativeSpelling() function is used:

  //! Note: 'aProps' needs to have set 'IsGermanPreReform' to true!
  xHyphWord = mxHyph.queryAlternativeSpelling( "Schiffahrt", 
                      new Locale("de","DE",""), (short)4, aProps );
  if (xHyphWord == null)
      System.out.println( "no alternative spelling found at specified position." );
  else
  {
      if (xHyphWord.isAlternativeSpelling())
          System.out.println( "alternative spelling detectetd!" );
      System.out.println( "valid hyphenation pos found at " + xHyphWord.getHyphenationPos() 
              + " in " + xHyphWord.getWord() );
      System.out.println( "hyphenation char will be after char " + xHyphWord.getHyphenPos() 
              + " in " + xHyphWord.getHyphenatedWord() );
  }

The return types interface is the same as in the above example (com.sun.star.linguistic2.XHyphenatedWord).

The next example demonstrates getting possible hyphenation positions. To determine all possible hyphenation positions in a word, do this:

  XPossibleHyphens xPossHyph = mxHyph.createPossibleHyphens( "waterfall", aLocale, aEmptyProps );
  if (xPossHyph == null)
      System.out.println( "no hyphenation positions found." );
  else
      System.out.println( xPossHyph.getPossibleHyphens() );

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

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