Using Hyphenator
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). |