Lesson 1 of 5 · 2 min
Tokens are not letters
A model reads tokens: chunks of text, often a word or part of one. It never sees individual letters unless a token happens to be one. That is why a model can write an essay and still miscount the letters in a word.
vocab = ["straw", "berry", " is", " red", "un", "believ", "able"]
def tokenize(text):
out = []
while text:
piece = max((v for v in vocab if text.startswith(v)), key=len, default=text[0])
out.append(piece)
text = text[len(piece):]
return out
print(tokenize("strawberry is red"))
print(tokenize("unbelievable"))
Real tokenizers learn their vocabulary from huge amounts of text, but the idea is the same.
