Class RuleBasedBreakIterator
- java.lang.Object
-
- com.ibm.icu.text.BreakIterator
-
- com.ibm.icu.text.RuleBasedBreakIterator
-
- All Implemented Interfaces:
java.lang.Cloneable
public class RuleBasedBreakIterator extends BreakIterator
Rule Based Break Iterator This is a port of the C++ class RuleBasedBreakIterator from ICU4C.
-
-
Field Summary
-
Fields inherited from class com.ibm.icu.text.BreakIterator
DONE, KIND_CHARACTER, KIND_LINE, KIND_SENTENCE, KIND_TITLE, KIND_WORD, WORD_IDEO, WORD_IDEO_LIMIT, WORD_KANA, WORD_KANA_LIMIT, WORD_LETTER, WORD_LETTER_LIMIT, WORD_NONE, WORD_NONE_LIMIT, WORD_NUMBER, WORD_NUMBER_LIMIT
-
-
Constructor Summary
Constructors Constructor Description RuleBasedBreakIterator(java.lang.String rules)
Construct a RuleBasedBreakIterator from a set of rules supplied as a string.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected static void
checkOffset(int offset, java.text.CharacterIterator text)
Throw IllegalArgumentException unless begin <= offset < end.java.lang.Object
clone()
Clones this iterator.static void
compileRules(java.lang.String rules, java.io.OutputStream ruleBinary)
Compile a set of source break rules into the binary state tables used by the break iterator engine.int
current()
Returns the current iteration position.void
dump(java.io.PrintStream out)
Deprecated.This API is ICU internal only.boolean
equals(java.lang.Object that)
Returns true if both BreakIterators are of the same class, have the same rules, and iterate over the same text.int
first()
Sets the current iteration position to the beginning of the text.int
following(int startPos)
Sets the iterator to refer to the first boundary position following the specified position.static RuleBasedBreakIterator
getInstanceFromCompiledRules(java.io.InputStream is)
Create a break iterator from a precompiled set of break rules.static RuleBasedBreakIterator
getInstanceFromCompiledRules(java.nio.ByteBuffer bytes)
Deprecated.This API is ICU internal only.int
getRuleStatus()
Return the status tag from the break rule that determined the most recently returned break position.int
getRuleStatusVec(int[] fillInArray)
Get the status (tag) values from the break rule(s) that determined the most recently returned break position.java.text.CharacterIterator
getText()
Return a CharacterIterator over the text being analyzed.int
hashCode()
Compute a hashcode for this BreakIteratorboolean
isBoundary(int offset)
Returns true if the specified position is a boundary position.int
last()
Sets the current iteration position to the end of the text.int
next()
Advances the iterator to the next boundary position.int
next(int n)
Advances the iterator either forward or backward the specified number of steps.int
preceding(int offset)
Sets the iterator to refer to the last boundary position before the specified position.int
previous()
Moves the iterator backwards, to the boundary preceding the current one.void
setText(java.text.CharacterIterator newText)
Set the iterator to analyze a new piece of text.java.lang.String
toString()
Returns the description (rules) used to create this iterator.-
Methods inherited from class com.ibm.icu.text.BreakIterator
getAvailableLocales, getAvailableULocales, getBreakInstance, getCharacterInstance, getCharacterInstance, getCharacterInstance, getLineInstance, getLineInstance, getLineInstance, getLocale, getSentenceInstance, getSentenceInstance, getSentenceInstance, getTitleInstance, getTitleInstance, getTitleInstance, getWordInstance, getWordInstance, getWordInstance, registerInstance, registerInstance, setText, setText, unregister
-
-
-
-
Method Detail
-
getInstanceFromCompiledRules
public static RuleBasedBreakIterator getInstanceFromCompiledRules(java.io.InputStream is) throws java.io.IOException
Create a break iterator from a precompiled set of break rules. Creating a break iterator from the binary rules is much faster than creating one from source rules. The binary rules are generated by the RuleBasedBreakIterator.compileRules() function. Binary break iterator rules are not guaranteed to be compatible between different versions of ICU.- Parameters:
is
- an input stream supplying the compiled binary rules.- Throws:
java.io.IOException
- if there is an error while reading the rules from the InputStream.- See Also:
compileRules(String, OutputStream)
-
getInstanceFromCompiledRules
@Deprecated public static RuleBasedBreakIterator getInstanceFromCompiledRules(java.nio.ByteBuffer bytes) throws java.io.IOException
Deprecated.This API is ICU internal only.Create a break iterator from a precompiled set of break rules. Creating a break iterator from the binary rules is much faster than creating one from source rules. The binary rules are generated by the RuleBasedBreakIterator.compileRules() function. Binary break iterator rules are not guaranteed to be compatible between different versions of ICU.- Parameters:
bytes
- a buffer supplying the compiled binary rules.- Throws:
java.io.IOException
- if there is an error while reading the rules from the buffer.- See Also:
compileRules(String, OutputStream)
-
clone
public java.lang.Object clone()
Clones this iterator.- Overrides:
clone
in classBreakIterator
- Returns:
- A newly-constructed RuleBasedBreakIterator with the same behavior as this one.
-
equals
public boolean equals(java.lang.Object that)
Returns true if both BreakIterators are of the same class, have the same rules, and iterate over the same text.- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
Returns the description (rules) used to create this iterator. (In ICU4C, the same function is RuleBasedBreakIterator::getRules())- Overrides:
toString
in classjava.lang.Object
-
hashCode
public int hashCode()
Compute a hashcode for this BreakIterator- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- A hash code
-
dump
@Deprecated public void dump(java.io.PrintStream out)
Deprecated.This API is ICU internal only.Dump the contents of the state table and character classes for this break iterator. For debugging only.
-
compileRules
public static void compileRules(java.lang.String rules, java.io.OutputStream ruleBinary) throws java.io.IOException
Compile a set of source break rules into the binary state tables used by the break iterator engine. Creating a break iterator from precompiled rules is much faster than creating one from source rules. Binary break rules are not guaranteed to be compatible between different versions of ICU.- Parameters:
rules
- The source form of the break rulesruleBinary
- An output stream to receive the compiled rules.- Throws:
java.io.IOException
- If there is an error writing the output.- See Also:
getInstanceFromCompiledRules(InputStream)
-
first
public int first()
Sets the current iteration position to the beginning of the text. (i.e., the CharacterIterator's starting offset).- Specified by:
first
in classBreakIterator
- Returns:
- The offset of the beginning of the text.
-
last
public int last()
Sets the current iteration position to the end of the text. (i.e., the CharacterIterator's ending offset).- Specified by:
last
in classBreakIterator
- Returns:
- The text's past-the-end offset.
-
next
public int next(int n)
Advances the iterator either forward or backward the specified number of steps. Negative values move backward, and positive values move forward. This is equivalent to repeatedly calling next() or previous().- Specified by:
next
in classBreakIterator
- Parameters:
n
- The number of steps to move. The sign indicates the direction (negative is backwards, and positive is forwards).- Returns:
- The character offset of the boundary position n boundaries away from the current one.
-
next
public int next()
Advances the iterator to the next boundary position.- Specified by:
next
in classBreakIterator
- Returns:
- The position of the first boundary after this one.
-
previous
public int previous()
Moves the iterator backwards, to the boundary preceding the current one.- Specified by:
previous
in classBreakIterator
- Returns:
- The position of the boundary position immediately preceding the starting position.
-
following
public int following(int startPos)
Sets the iterator to refer to the first boundary position following the specified position.- Specified by:
following
in classBreakIterator
- Parameters:
startPos
- The position from which to begin searching for a break position.- Returns:
- The position of the first break after the current position.
-
preceding
public int preceding(int offset)
Sets the iterator to refer to the last boundary position before the specified position.- Overrides:
preceding
in classBreakIterator
- Parameters:
offset
- The position to begin searching for a break from.- Returns:
- The position of the last boundary before the starting position.
-
checkOffset
protected static final void checkOffset(int offset, java.text.CharacterIterator text)
Throw IllegalArgumentException unless begin <= offset < end.
-
isBoundary
public boolean isBoundary(int offset)
Returns true if the specified position is a boundary position. As a side effect, leaves the iterator pointing to the first boundary position at or after "offset".- Overrides:
isBoundary
in classBreakIterator
- Parameters:
offset
- the offset to check.- Returns:
- True if "offset" is a boundary position.
-
current
public int current()
Returns the current iteration position. Note that UBRK_DONE is never returned from this function; if iteration has run to the end of a string, current() will return the length of the string while next() will return BreakIterator.DONE).- Specified by:
current
in classBreakIterator
- Returns:
- The current iteration position.
-
getRuleStatus
public int getRuleStatus()
Return the status tag from the break rule that determined the most recently returned break position. The values appear in the rule source within brackets, {123}, for example. For rules that do not specify a status, a default value of 0 is returned. If more than one rule applies, the numerically largest of the possible status values is returned.Of the standard types of ICU break iterators, only the word and line break iterator provides status values. The values are defined in class RuleBasedBreakIterator, and allow distinguishing between words that contain alphabetic letters, "words" that appear to be numbers, punctuation and spaces, words containing ideographic characters, and more. Call
getRuleStatus
after obtaining a boundary position fromnext()
,previous()
, or any other break iterator functions that returns a boundary position.- Overrides:
getRuleStatus
in classBreakIterator
- Returns:
- the status from the break rule that determined the most recently returned break position.
-
getRuleStatusVec
public int getRuleStatusVec(int[] fillInArray)
Get the status (tag) values from the break rule(s) that determined the most recently returned break position. The values appear in the rule source within brackets, {123}, for example. The default status value for rules that do not explicitly provide one is zero.The status values used by the standard ICU break rules are defined as public constants in class RuleBasedBreakIterator.
If the size of the output array is insufficient to hold the data, the output will be truncated to the available length. No exception will be thrown.
- Overrides:
getRuleStatusVec
in classBreakIterator
- Parameters:
fillInArray
- an array to be filled in with the status values.- Returns:
- The number of rule status values from rules that determined the most recent boundary returned by the break iterator. In the event that the array is too small, the return value is the total number of status values that were available, not the reduced number that were actually returned.
-
getText
public java.text.CharacterIterator getText()
Return a CharacterIterator over the text being analyzed. This version of this method returns the actual CharacterIterator we're using internally. Changing the state of this iterator can have undefined consequences. If you need to change it, clone it first.- Specified by:
getText
in classBreakIterator
- Returns:
- An iterator over the text being analyzed.
-
setText
public void setText(java.text.CharacterIterator newText)
Set the iterator to analyze a new piece of text. This function resets the current iteration position to the beginning of the text.- Specified by:
setText
in classBreakIterator
- Parameters:
newText
- An iterator over the text to analyze.
-
-