Class WordpieceTokenizer
java.lang.Object
opennlp.tools.tokenize.WordpieceTokenizer
- All Implemented Interfaces:
Tokenizer
A
Tokenizer implementation which performs tokenization
using word pieces.
Adapted under MIT license from https://github.com/robrua/easy-bert.
Note that this tokenizer performs only the wordpiece (subword) stage
of BERT tokenization. It does not normalize the input text: no lower casing,
no accent stripping, no control character removal. Text that does not match
the vocabulary's casing - for uncased models that includes every capitalized
word - is mapped to the unknown token. Use WordpieceEncoder for the
BERT normalization and wordpiece segmentation stages.
Runs of punctuation are split into individual tokens. A word that cannot be
fully represented by vocabulary pieces becomes one unknown token.
tokenizePos(String) is not supported.
For reference see:
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringBERT classification token:[CLS].static final StringBERT separator token:[SEP].static final StringBERT unknown token:[UNK].static final StringRoBERTa classification token:<s>.static final StringRoBERTa separator token.static final StringRoBERTa unknown token. -
Constructor Summary
ConstructorsConstructorDescriptionWordpieceTokenizer(Set<String> vocabulary) Initializes aWordpieceTokenizerwith avocabularyand a default maximum token length of 100 Unicode code points.WordpieceTokenizer(Set<String> vocabulary, int maxTokenLength) WordpieceTokenizer(Set<String> vocabulary, String classificationToken, String separatorToken, String unknownToken) Initializes aWordpieceTokenizerwith avocabularyand custom special tokens.WordpieceTokenizer(Set<String> vocabulary, String classificationToken, String separatorToken, String unknownToken, int maxTokenLength) Initializes aWordpieceTokenizerwith avocabulary, custom special tokens and a custommaxTokenLength. -
Method Summary
Modifier and TypeMethodDescriptionintString[]Splits a string into its atomic parts.Span[]tokenizePos(String text) Not supported: wordpiece tokens (subwords,##continuations and special tokens) have no faithful character spans in the original text.
-
Field Details
-
BERT_CLS_TOKEN
-
BERT_SEP_TOKEN
-
BERT_UNK_TOKEN
-
ROBERTA_CLS_TOKEN
-
ROBERTA_SEP_TOKEN
-
ROBERTA_UNK_TOKEN
-
-
Constructor Details
-
WordpieceTokenizer
Initializes aWordpieceTokenizerwith avocabularyand a default maximum token length of 100 Unicode code points.- Parameters:
vocabulary- A set of tokens considered the vocabulary; must not benullor containnullor empty entries.- Throws:
IllegalArgumentException- Thrown ifvocabularyis invalid.
-
WordpieceTokenizer
- Parameters:
vocabulary- A set of tokens considered the vocabulary; must not benullor containnullor empty entries.maxTokenLength- The non-negative maximum number of Unicode code points in one token.- Throws:
IllegalArgumentException- Thrown ifvocabularyis invalid ormaxTokenLengthis negative.
-
WordpieceTokenizer
public WordpieceTokenizer(Set<String> vocabulary, String classificationToken, String separatorToken, String unknownToken) Initializes aWordpieceTokenizerwith avocabularyand custom special tokens. This allows support for models like RoBERTa that use different special tokens instead of the BERT defaults.- Parameters:
vocabulary- The vocabulary; must not benullor containnullor empty entries.classificationToken- The CLS token; must not benullor empty.separatorToken- The SEP token; must not benullor empty.unknownToken- The UNK token; must not benullor empty.- Throws:
IllegalArgumentException- Thrown if an argument is invalid.
-
WordpieceTokenizer
public WordpieceTokenizer(Set<String> vocabulary, String classificationToken, String separatorToken, String unknownToken, int maxTokenLength) Initializes aWordpieceTokenizerwith avocabulary, custom special tokens and a custommaxTokenLength.- Parameters:
vocabulary- The vocabulary; must not benullor containnullor empty entries.classificationToken- The CLS token; must not benullor empty.separatorToken- The SEP token; must not benullor empty.unknownToken- The UNK token; must not benullor empty.maxTokenLength- The non-negative maximum number of Unicode code points in one token.- Throws:
IllegalArgumentException- Thrown if an argument is invalid.
-
-
Method Details
-
tokenizePos
Not supported: wordpiece tokens (subwords,##continuations and special tokens) have no faithful character spans in the original text.- Specified by:
tokenizePosin interfaceTokenizer- Parameters:
text- The string to be tokenized.- Returns:
- The
spans (offsets intofor each token as the individuals array elements.s) - Throws:
UnsupportedOperationException- Always.
-
tokenize
Splits a string into its atomic parts.- Specified by:
tokenizein interfaceTokenizer- Parameters:
text- The string to be tokenized.- Returns:
- The String[] with the individual tokens as the array elements.
- Throws:
IllegalArgumentException- Thrown iftextisnull.
-
getMaxTokenLength
public int getMaxTokenLength()- Returns:
- The maximum token length.
-