Class BPETokenizerTrainer

java.lang.Object
opennlp.tools.tokenize.BPETokenizerTrainer
All Implemented Interfaces:
Trainer<Parameters>

public final class BPETokenizerTrainer extends Object implements Trainer<Parameters>
Learns BPE merge operations from a training corpus and produces a BPEModel.

Implements the BPE learning algorithm from Sennrich et al. (2016):

  1. 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.
  2. Count all adjacent symbol pairs across the vocabulary, weighted by word frequency.
  3. Merge the most frequent pair into a single new symbol.
  4. 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:

See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • init

      public void init(Parameters trainParams, Map<String,String> reportMap)
      Conducts the initialization of an Trainer via Parameters and a report map.
      Specified by:
      init in interface Trainer<Parameters>
      Parameters:
      trainParams - The Parameters to use.
      reportMap - The Map instance used as report map.
    • init

      public void init(Parameters trainParams, Map<String,String> reportMap, TrainingConfiguration config)
      Conducts the initialization of a Trainer via Parameters, report map and TrainingConfiguration
      Specified by:
      init in interface Trainer<Parameters>
      Parameters:
      trainParams - The Parameters to use.
      reportMap - The Map instance used as report map.
      config - The TrainingConfiguration to use. If null, suitable defaults will be used.
    • train

      public BPEModel train(Iterable<String> corpus, int numMerges, String languageCode)
      Learns BPE merge operations from a training corpus and returns a BPEModel.
      Parameters:
      corpus - An iterable of text strings (e.g., sentences or documents). Must not be null.
      numMerges - The number of merge operations to learn. Must be positive.
      languageCode - The ISO language code (e.g., "en", "de"). Must not be null.
      Returns:
      A trained BPEModel containing the learned merge operations.
      Throws:
      IllegalArgumentException - if numMerges is not positive, or if corpus or languageCode is null.
    • learnMerges

      private List<BPETokenizer.SymbolPair> learnMerges(Iterable<String> corpus, int numMerges)
      Learns BPE merge operations from the given corpus.

      The algorithm proceeds as follows:

      1. Builds a word frequency map from the corpus using whitespace tokenization.
      2. Converts each word into a character-level symbol sequence with an end-of-word marker on the final character.
      3. 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.
      4. Stops after numMerges iterations 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.SymbolPair merge operations.
    • splitToSymbols

      private List<String> splitToSymbols(String word)
    • applyMerge

      private List<String> applyMerge(List<String> symbols, BPETokenizer.SymbolPair pair)
      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 - The BPETokenizer.SymbolPair to merge.
      Returns:
      A new list with all occurrences of the pair merged.