Class WordSegmenter

java.lang.Object
opennlp.tools.tokenize.uax29.WordSegmenter

public final class WordSegmenter extends Object
Finds word boundaries in text using the Unicode Text Segmentation algorithm (UAX #29), rules WB1 through WB999.

The implementation is a single forward cursor pass with O(1) WordBreakProperty lookups. It decodes each code point once, keeps only a constant amount of state, and allocates nothing per character. It implements the "ignore" semantics of WB4 (a base character absorbs following Extend, Format, and ZWJ), the look-ahead rules WB6/WB7/WB7b/WB12, the Hebrew quote rules WB7a-WB7c, the emoji zero-width-joiner rule WB3c, and regional-indicator pairing WB15/WB16. The look-ahead for the WB6/WB7b/WB12 rules is resolved lazily and only at mid-word punctuation, so the common case never scans ahead.

forEachSegment(CharSequence, SegmentConsumer) streams the segments with no allocation; boundaries(CharSequence) returns every boundary offset (always including 0 and the text length); segments(CharSequence) returns the spans between them.

  • Field Details

    • NO_BREAK

      private static final byte NO_BREAK
      See Also:
    • BREAK

      private static final byte BREAK
      See Also:
    • CONSULT

      private static final byte CONSULT
      See Also:
    • GO_SLOW

      private static final byte GO_SLOW
      See Also:
    • CLASSES

      private static final WordBreak[] CLASSES
    • CLASS_COUNT

      private static final int CLASS_COUNT
    • TRANSITION

      private static final byte[] TRANSITION
    • OTHER_ORDINAL

      private static final int OTHER_ORDINAL
    • CR_ORDINAL

      private static final int CR_ORDINAL
    • LF_ORDINAL

      private static final int LF_ORDINAL
    • NEWLINE_ORDINAL

      private static final int NEWLINE_ORDINAL
    • ZWJ_ORDINAL

      private static final int ZWJ_ORDINAL
    • WSEG_SPACE_ORDINAL

      private static final int WSEG_SPACE_ORDINAL
    • EXTEND_ORDINAL

      private static final int EXTEND_ORDINAL
    • FORMAT_ORDINAL

      private static final int FORMAT_ORDINAL
    • REGIONAL_INDICATOR_ORDINAL

      private static final int REGIONAL_INDICATOR_ORDINAL
    • SPECIAL

      private static final boolean[] SPECIAL
    • FAST

      private static final byte[] FAST
  • Constructor Details

    • WordSegmenter

      private WordSegmenter()
  • Method Details

    • buildSpecialTable

      private static boolean[] buildSpecialTable()
    • buildFastTable

      private static byte[] buildFastTable()
    • buildTransitionTable

      private static byte[] buildTransitionTable()
    • deriveDecision

      private static byte deriveDecision(WordBreak last, WordBreak current)
    • forEachSegment

      public static void forEachSegment(CharSequence text, WordSegmenter.SegmentConsumer consumer)
      Streams the word segments of text to consumer in order, allocating nothing. Each segment is delivered as the half-open character range [start, end); the segments are contiguous and together cover the whole text.
      Parameters:
      text - The text to segment; may be empty, in which case no segment is delivered.
      consumer - The receiver of the segment ranges.
      Throws:
      IllegalArgumentException - Thrown if text or consumer is null.
    • breakAtSpecial

      private static boolean breakAtSpecial(int prev, int current, int codePoint, int last, int secondLast, int regionalIndicatorRun, CharSequence text, int nextFrom, int length, WordBreakProperty.Data wbData, BitSet pictographs)
    • consult

      private static boolean consult(CharSequence text, int nextFrom, int length, int current, int last, int secondLast, int regionalIndicatorRun, WordBreakProperty.Data wbData)
    • boundaries

      public static int[] boundaries(CharSequence text)
      Returns the word boundary character offsets in text, in ascending order, including the boundaries at 0 and text.length().
      Parameters:
      text - The text to segment.
      Returns:
      The boundary offsets; for empty text, [0].
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • segments

      public static List<Span> segments(CharSequence text)
      Returns the word segments of text as spans between consecutive boundaries.
      Parameters:
      text - The text to segment.
      Returns:
      The segment spans, in order.
      Throws:
      IllegalArgumentException - Thrown if text is null.
    • nextSignificant

      private static WordBreak nextSignificant(CharSequence text, int from, int length, WordBreakProperty.Data wbData)
    • afterPrefix

      private static boolean afterPrefix(WordBreak current, WordBreak last, WordBreak secondLast, WordBreak next, int regionalIndicatorRun)
    • isIgnorable

      private static boolean isIgnorable(WordBreak value)
    • isIgnorable

      private static boolean isIgnorable(int ordinal)
    • isAhLetter

      private static boolean isAhLetter(WordBreak value)
    • isMidLetter

      private static boolean isMidLetter(WordBreak value)
    • isMidNumber

      private static boolean isMidNumber(WordBreak value)