BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Tokenization and NLP15 of 38 in topic

359. Tokenize words with WordPiece greedy longest match

MediumTokenization and NLP
Asked atAnthropicxAI

BERT-style models split each word into subword pieces by repeatedly taking the longest piece in the vocabulary that matches at the current position. Pieces that continue a word carry a "##" prefix so the model knows they are not word starts.

Write wordpiece_tokenize(text, vocab). Split text on whitespace. For each word, start at position 0:
- Find the longest substring word[start:end] that is in vocab. For start > 0 the candidate is "##" + word[start:end].
- Emit it and move start to end.
- If no candidate matches at some position, the whole word becomes a single "[UNK]" (drop any pieces already found for that word).
vocab is a set of strings. Return the list of pieces.

Functiondef wordpiece_tokenize(text, vocab)

Example 1

Input: vocab = {"un", "##aff", "##able", "aff"}, text = "unaffable"

Output: ["un", "##aff", "##able"]

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

Try end from len(word) down to start + 1 and stop at the first hit.

Hint 2

Build the word's pieces in a temporary list and replace it with ['[UNK]'] on failure.

Common mistakes
  • Emitting [UNK] for one character and carrying on, as the buggy version does, produces a mix of real pieces and unknowns.
Understand the idea

WordPiece breaks a word into known pieces by always grabbing the longest piece that matches at the current position, then moving on. Any piece that is not at the start of a word gets a '##' prefix so the model knows it continues a word. If at some point nothing matches, the whole word becomes one [UNK], and pieces already found for it are thrown away.

Worked example

vocab = {'play', '##ing', '##in'}, text 'playing cats'. For 'playing': at position 0 the longest match is 'play'. At position 4, try '##ing' (matches) before shorter '##in', so emit '##ing'. For 'cats': no piece starting at 'c' exists, so the word becomes '[UNK]'. Result ['play', '##ing', '[UNK]'].

split on whitespace; for each word, from start, try end from the full length down to start + 1 and take the first candidate in vocab (with '##' added when start > 0); emit and jump to end; on failure replace the whole word with '[UNK]'

This is the tokenizer used by BERT and many embedding models.

Topics
Tokenization and NLP
Companies2
  • Anthropic a candidate reported this question Anthropic: Longest-match tokenizer (CircleCoder)
  • xAI a candidate reported this question xAI Machine Learning Engineer Interview Questions | Screna AI

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Do one merge step of a BPE tokenizerMedium
  • Compute Levenshtein edit distanceMedium
  • Build a vocabulary with special tokens, a minimum frequency and UNKMedium
  • Rank documents for a query with smoothed TF-IDF and cosine similarityMedium
All Tokenization and NLP problems
Report
359 / 903
Python 3solution.pyPython idle
1
2
3
4
def wordpiece_tokenize(text, vocab):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS