Encode Text with Learned BPE Merges

medium
Unit 24 · Calibration0/3 to milestone

Once a BPE tokenizer is trained, its merge list (ranked pairs) defines how to encode any new word. Starting from single characters, repeatedly find the adjacent pair with the lowest rank (earliest learned) and merge every occurrence of it, scanning left to right without overlaps. Stop when no adjacent pair is in the merge list.

Implement:

  • encode_word(word, merges): merges is a list of (a, b) pairs in rank order. Return the list of tokens.
  • encode(text, merges, vocab): split text on single spaces, encode each word, and map tokens to ids with vocab. Tokens missing from the vocabulary map to vocab["<unk>"]. Between words, insert the id of the " " token.
  • decode(ids, vocab): map ids back to tokens and concatenate them.
#tokenizers#bpe#nlp2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Encode Text with Learned BPE Merges · TechGradify