Skip to content

Commit

Permalink
Added new text analysis function getNextLineBreakPosition()
Browse files Browse the repository at this point in the history
  • Loading branch information
contrext committed Oct 19, 2016
1 parent bb754b2 commit 13a3339
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
19 changes: 19 additions & 0 deletions plugin/xsl/i18n-utils.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,23 @@
<xsl:sequence select="$result"/>
</xsl:function>

<!-- Get the next line break position in the line. If the line break
position is the end of the line then returns the length of the
line.
-->
<xsl:function name="dci18n:nextLineBreakPosition" as="xs:integer">
<xsl:param name="text" as="xs:string"/>
<xsl:param name="lang" as="xs:string"/>
<xsl:param name="debug" as="xs:boolean"/>

<xsl:variable name="breakPos" as="xs:integer"
select="textAnalyzer:nextLineBreakPosition($text, $lang, $debug)"
/>

<xsl:variable name="result" as="xs:integer"
select="if ($breakPos = -1) then string-length($text) else $breakPos"
/>
<xsl:sequence select="$result"/>
</xsl:function>

</xsl:stylesheet>
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public static ArrayList<Item> splitLine(String text, String langCode, boolean de
}

Locale locale = Locale.forLanguageTag(langCode);
System.out.println("+ [DEBUG] splitLine(): text=\"" + text + "\", debug=" + debug);
if (debug) {
System.out.println("+ [DEBUG] splitLine(): text=\"" + text + "\"");
}

LineSplittingSequenceIterator iterator =
LineSplittingSequenceIterator.getInstanceForLocale(locale, text, debug);
Expand All @@ -91,4 +93,31 @@ public static ArrayList<Item> splitLine(String text, String langCode, boolean de
return items;

}

/**
* Get the position of the next line break opportunity in the specified text, not counting the start of the
* text, which is always a line break opportunity.
* <p>
* The value is the number of characters from the start
* of the text to the break point. So if the first break opportunity is between the first and second
* characters the return value will be "1".
* @param text The text to analyze
* @param langCode Language code for the locale to use
* @param debug Set to true to turn on debug messages
* @return The zero-indexed position of the first line break opportunity in the text, which may be after the
* end of the text.
* @throws Exception
*/
public static int nextLineBreakPosition(String text, String langCode, boolean debug) throws Exception {
Locale locale = Locale.forLanguageTag(langCode);
if (debug) {
System.out.println("+ [DEBUG] nextLineBreakPosition(): text=\"" + text + "\"");
}

BreakIterator iterator = RuleBasedBreakIterator.getLineInstance(locale);
iterator.setText(text);
iterator.next(); // Skip the start of the text, which is always the first line break
return iterator.next();

}
}

0 comments on commit 13a3339

Please sign in to comment.