Package opennlp.tools.tokenize
Class BPETokenizerTrainer
java.lang.Object
opennlp.tools.tokenize.BPETokenizerTrainer
- All Implemented Interfaces:
Trainer<Parameters>
Learns BPE merge operations from a training corpus and
produces a
BPEModel.
Implements the BPE learning algorithm from Sennrich et al. (2016):
- Build a vocabulary of character-level symbol sequences from the corpus, where each word is split into individual characters with an end-of-word marker.
- Count all adjacent symbol pairs across the vocabulary, weighted by word frequency.
- Merge the most frequent pair into a single new symbol.
- Repeat until the desired number of merges
(
numMerges) is reached.
The number of merges controls the granularity of the resulting vocabulary: fewer merges produce finer-grained (more character-level) tokens, while more merges produce coarser (more word-level) tokens. A typical value ranges from a few thousand to tens of thousands, depending on the corpus size and language.
Usage:
List<String> corpus = List.of(
"the cat sat on the mat",
"the dog sat on the log"
);
BPETokenizerTrainer trainer = new BPETokenizerTrainer();
BPEModel model = trainer.train(corpus, 10000, "en");
// Persist the model
model.serialize(Path.of("bpe-en.bin"));
// Use it for tokenization
BPETokenizer tokenizer = new BPETokenizer(model);
String[] tokens = tokenizer.tokenize("the cat");
For reference see:
- Sennrich, R., Haddow, B., & Birch, A. (2016). Neural Machine Translation of Rare Words with Subword Units. https://arxiv.org/abs/1508.07909
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapplyMerge(List<String> symbols, BPETokenizer.SymbolPair pair) Applies a single merge operation to a symbol sequence.voidinit(Parameters trainParams, Map<String, String> reportMap) voidinit(Parameters trainParams, Map<String, String> reportMap, TrainingConfiguration config) private List<BPETokenizer.SymbolPair> learnMerges(Iterable<String> corpus, int numMerges) Learns BPE merge operations from the given corpus.splitToSymbols(String word) Learns BPE merge operations from a training corpus and returns aBPEModel.
-
Field Details
-
trainingParameters
-
reportMap
-
trainingConfiguration
-
-
Constructor Details
-
BPETokenizerTrainer
public BPETokenizerTrainer()Creates a newBPETokenizerTrainer.
-
-
Method Details
-
init
- Specified by:
initin interfaceTrainer<Parameters>- Parameters:
trainParams- TheParametersto use.reportMap- TheMapinstance used as report map.
-
init
public void init(Parameters trainParams, Map<String, String> reportMap, TrainingConfiguration config) - Specified by:
initin interfaceTrainer<Parameters>- Parameters:
trainParams- TheParametersto use.reportMap- TheMapinstance used as report map.config- TheTrainingConfigurationto use. Ifnull, suitable defaults will be used.
-
train
Learns BPE merge operations from a training corpus and returns aBPEModel.- Parameters:
corpus- An iterable of text strings (e.g., sentences or documents). Must not benull.numMerges- The number of merge operations to learn. Must be positive.languageCode- The ISO language code (e.g., "en", "de"). Must not benull.- Returns:
- A trained
BPEModelcontaining the learned merge operations. - Throws:
IllegalArgumentException- ifnumMergesis not positive, or ifcorpusorlanguageCodeisnull.
-
learnMerges
Learns BPE merge operations from the given corpus.The algorithm proceeds as follows:
- Builds a word frequency map from the corpus using whitespace tokenization.
- Converts each word into a character-level symbol sequence with an end-of-word marker on the final character.
- Iteratively counts all adjacent symbol pairs (weighted by word frequency), selects the most frequent pair, records it as a merge operation, and applies the merge to all vocabulary entries.
- Stops after
numMergesiterations or when no further pairs exist.
- Parameters:
corpus- The training corpus, where each element is a text string.numMerges- The maximum number of merge operations to learn.- Returns:
- An ordered list of learned
BPETokenizer.SymbolPairmerge operations.
-
splitToSymbols
-
applyMerge
Applies a single merge operation to a symbol sequence. Scans the list for adjacent symbols matching the given pair and replaces each occurrence with a single concatenated symbol.- Parameters:
symbols- The current symbol sequence for a word.pair- TheBPETokenizer.SymbolPairto merge.- Returns:
- A new list with all occurrences of the pair merged.
-