Package opennlp.tools.tokenize.uax29
Class WordTokenizer
java.lang.Object
opennlp.tools.tokenize.uax29.WordTokenizer
- All Implemented Interfaces:
Tokenizer
A word tokenizer built on the Unicode Text Segmentation algorithm
(UAX #29).
It finds segments with
WordSegmenter, keeps the ones that are words (letters, digits,
ideographs, kana, Hangul, Southeast-Asian script, or emoji), drops whitespace and punctuation,
and classifies each kept token with a WordType.
Emoji here means any Extended_Pictographic code point, so symbol-like characters such as
the copyright, trademark, and double-exclamation signs are kept (typed WordType.EMOJI)
rather than dropped as punctuation.
A token longer than maxTokenLength is emitted as consecutive pieces, never splitting a
surrogate pair. The tokenizer reports offset Spans, so the original text and its character
offsets are preserved for downstream normalization.
It implements Tokenizer: tokenize(String) returns the token strings and
tokenizePos(String) their offsets. tokenizeTyped(CharSequence) additionally
carries each token's WordType, and tokenize(CharSequence, TokenHandler) streams
tokens with no per-token allocation. Instances are immutable and thread-safe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceReceives each word token as a character range and its type, with no allocation. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default maximum token length: 255.private final int -
Constructor Summary
ConstructorsConstructorDescriptionInstantiates a tokenizer with the default maximum token length.WordTokenizer(int maxTokenLength) Instantiates a tokenizer with the given maximum token length. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidemit(CharSequence text, int start, int end, WordType type, WordTokenizer.TokenHandler handler) voidtokenize(CharSequence text, WordTokenizer.TokenHandler handler) Streams the word tokens oftexttohandlerin order, allocating nothing.String[]Returns the word tokens oftextas strings, in order.Span[]tokenizePos(String text) Returns the offset spans of the word tokens oftext, in order.tokenizeSpans(CharSequence text) Computes the offset spans of the word tokens intext, in order.tokenizeTyped(CharSequence text) Returns the word tokens intext, each carrying itsWordType, in order.
-
Field Details
-
DEFAULT_MAX_TOKEN_LENGTH
public static final int DEFAULT_MAX_TOKEN_LENGTHThe default maximum token length: 255.- See Also:
-
maxTokenLength
private final int maxTokenLength
-
-
Constructor Details
-
WordTokenizer
public WordTokenizer()Instantiates a tokenizer with the default maximum token length. -
WordTokenizer
public WordTokenizer(int maxTokenLength) Instantiates a tokenizer with the given maximum token length.- Parameters:
maxTokenLength- The maximum number of characters in a token; longer tokens are chopped into consecutive pieces. Must be at least1.- Throws:
IllegalArgumentException- Thrown ifmaxTokenLengthis equal to or less than0.
-
-
Method Details
-
tokenize
Streams the word tokens oftexttohandlerin order, allocating nothing.- Parameters:
text- The text to tokenize.handler- The receiver of the tokens.- Throws:
IllegalArgumentException- Thrown iftextorhandlerisnull.
-
tokenize
Returns the word tokens oftextas strings, in order.- Specified by:
tokenizein interfaceTokenizer- Parameters:
text- The text to tokenize; an empty text yields no tokens.- Returns:
- The token strings.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
tokenizePos
Returns the offset spans of the word tokens oftext, in order.- Specified by:
tokenizePosin interfaceTokenizer- Parameters:
text- The text to tokenize; an empty text yields no spans.- Returns:
- The token spans.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
tokenizeSpans
Computes the offset spans of the word tokens intext, in order.- Parameters:
text- The text to tokenize; an empty text yields no spans.- Returns:
- The word-token spans.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
tokenizeTyped
Returns the word tokens intext, each carrying itsWordType, in order.- Parameters:
text- The text to tokenize; an empty text yields no tokens.- Returns:
- The typed word tokens.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
emit
private void emit(CharSequence text, int start, int end, WordType type, WordTokenizer.TokenHandler handler)
-