Where a language program starts
Tokenisation (also spelt tokenization) is splitting a piece of text into small pieces called tokens. It is the first step of natural language processing, the part of computing that works with human language.
A program cannot read a sentence the way you do. Before it can count, compare or search anything, it has to decide where one piece ends and the next begins.
By the end of this lesson
Note
I can split text into tokens.
Key words
| Word | What it means | Example |
|---|---|---|
| Token | one piece of text that a program treats as a single unit | |
| Tokenisation | splitting text into tokens | |
| Cleaning | making text more regular before it is split | lower case |
| Stop word | a very common word that tells you little | the |
| Vocabulary | the list of different tokens in a text |
The cells students fill in are left blank in this preview.
A token is a piece of text
A token is one piece of text that a program treats as a single unit. You choose what counts as one piece. The usual choice for a simple program is a word.
Splitting on spaces is the easiest rule. Python does it with split(). The result is a list, and each item in the list is a token.
Split a sentence into tokens
sentence = "The shop opens at nine and closes at six"
tokens = sentence.split()
print(tokens)
print(len(tokens), "tokens")
# Add "on Sunday" to the sentence. How many tokens now?
Look at the first line of a file
docker exec jupyter head -n 1 hires.csvShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
The rule decides the tokens
The first line of hires.csv has seven names joined by commas. A space rule would find one token. A comma rule finds seven: date, month, weekday, weather, temp_c, rain_mm and hires.
The same text gives different tokens under a different rule. So you always say which rule you used.
Cleaning makes tokens match
Split "The shop opens at nine." on spaces and the last token is nine. with the full stop stuck to it. A program sees nine. and nine as two different tokens.
Cleaning fixes that before the split. Change everything to lower case, so "The" and "the" match. Remove the punctuation marks, such as the full stop and the comma.
Split without cleaning first
docker exec jupyter python3 -c "print('The shop opens at nine.'.lower().split())"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Clean a sentence, then split it
text = "Late again, the bus is full!"
text = text.lower()
for mark in [".", ",", "!", "?"]:
text = text.replace(mark, "")
tokens = text.split()
print(tokens)
# Add a question mark to the text. Is it still removed?
A common mistake
Warning
Many people think that splitting on spaces is all tokenising needs. Punctuation stays stuck to the last word, and capitals make copies of the same word. Clean the text first, then split it, whenever you plan to count words.
Stop words carry little meaning
Some words appear in almost every sentence: the, a, at, and, is. These are stop words. They tell you little about what a text is about.
Many programs remove them after splitting, to keep the tokens that matter. Whether to remove them is a choice. For some tasks, such as answering a question, they matter, so keep them.
Words, characters and subwords
A word is not the only unit. A token can be one character, or a part of a word. Large language models use subwords: common words stay whole and rare words are cut into pieces.
The vocabulary is the list of different tokens. A word you never saw is an unknown token, and character tokens avoid that problem.
Split a word into characters
docker exec jupyter python3 -c "print(list('shop'), len('shop'))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Worked example: three ways to split one word
| Unit | Tokens for "bikes" | Number | Good for |
|---|---|---|---|
| Word | bikes | 1 | Counting and searching |
| Character | b, i, k, e, s | 5 | Never an unknown token |
| Subword | bike, s | 2 | Rare words and word endings |
The cells students fill in are left blank in this preview.
Going further
Take a sentence of your own about your week and tokenise it by hand, then with the code above. Do the two lists match?
Next, find a sentence where removing stop words changes the meaning, such as "not on time". Explain what the program would lose.