Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

How text is split into tokens

After this lesson: I can split text into tokens.

50 min lesson3 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

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

WordWhat it meansExample
Tokenone piece of text that a program treats as a single unit
Tokenisationsplitting text into tokens
Cleaningmaking text more regular before it is splitlower case
Stop worda very common word that tells you littlethe
Vocabularythe 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

Code: python
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

Command
docker exec jupyter head -n 1 hires.csv

Shown 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

Command
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

Code: python
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

Command
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

UnitTokens for "bikes"NumberGood for
Wordbikes1Counting and searching
Characterb, i, k, e, s5Never an unknown token
Subwordbike, s2Rare 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.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.