Natural Language Processing · Beginner level

Beginner level · Module 4: Tokenization and Text Normalization · Lesson 1 of 3

Tokenising Words and Sentences with spaCy

Learners compare naive split() tokenisation with spaCy's rule-based tokeniser and sentence segmentation on real-looking messages.

In this lesson you will

  • Explain what tokens are
  • Tokenise text and split sentences with spaCy

Key concepts

tokentokeniserspacy.blank('en') / en_core_web_smdoc.sentstoken attributes

The lesson

A token is a unit the computer treats as one item, usually a word, number or punctuation mark. text.split() splits on spaces only, so 'fees,' and 'fees' become different tokens. A proper tokeniser separates punctuation and handles cases like "don't" and 'U.N.'.

spaCy's English tokeniser is rule-based and fast. nlp = spacy.load('en_core_web_sm') loads a small English pipeline (install it once while online; it then works offline). doc = nlp(text) gives tokens with useful attributes such as token.text, token.is_punct, token.like_num and token.lemma_.

Sentences matter for summaries and search. for sent in doc.sents iterates over sentences. Messages without punctuation, common in SMS, are hard to split into sentences, which is a limitation to note.

Step by step

  1. Compare split() and spaCy tokens for five messages.
  2. Print each token with is_punct and like_num.
  3. Split a short report paragraph into sentences with doc.sents.
  4. Find one case where spaCy's tokenisation is wrong for local text.

Practice activity

Tokenise 20 project messages with spaCy and document three differences from split().

Expected result: Correct spaCy tokens and a clear note of the differences and one limitation.

Check your understanding

  1. Why is split() a weak tokeniser?

    Show answer

    It splits only on spaces, so punctuation stays attached to words.

  2. What does doc.sents provide?

    Show answer

    The sentences that spaCy detected in the text.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.