Understanding AI · lesson 37 of 60 · about 5 minutes
Tokens
Chatbots don't read words the way you do. See how text is chopped into tokens before AI can work with it.
What you'll learn
- Explain what a token is
- Split a sentence into words with Python
- Explain why long or rare words are split into pieces
Chopping up text
Before a chatbot can do anything with your message, it chops the text into pieces called tokens. A token is often a whole word, but long or unusual words get split into smaller pieces: unhappiness might become un, happi and ness.
Each token then gets a number, so the text becomes a list of numbers.
Why pieces?
No list could hold every word that exists, including names, typos and new slang. Pieces solve that: with a few tens of thousands of pieces, the model can build any word at all, even ones it has never seen.
The simplest way to split text into words: `split()` breaks it at the spaces.
text = "the cat sat on the mat"
words = text.split()
print(words)
print(len(words))Output
['the', 'cat', 'sat', 'on', 'the', 'mat']
6Tip
Real chatbots use cleverer splitting into pieces, but the idea is the same: text in, list of tokens out.
Now try it yourself
This lesson is free in Codiqo, with 6 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.