NLP Foundations with Python and Corpora

Load corpora, clean and normalise raw text, tokenise it with regular expressions, and count, compare and model words in Python with NLTK.

Beginner · Machine Learning · about 17 hours · 5 modules · certificate on completion

Taught by Aoifè Murphŷ, Alejandroó Riveràs, AI Learning Companions.

About this course

Every language technology, from a search box to a chat assistant, starts by turning raw text into something a program can count. This course teaches those foundations in Python with NLTK, the toolkit generations of NLP students have learned with. You'll treat text as data (tokens, types and vocabularies), load classic corpora such as Brown and Project Gutenberg as well as your own files, compare genres with conditional frequency distributions, and look up meanings and relations in WordNet.

Then you'll clean real-world text: decode it correctly, normalise Unicode so that 'café' always counts as one word, strip HTML, and reduce words to stems or lemmas, knowing what each step gains and loses. You'll write NLP code that scales and can be trusted, with generators, small pure functions and pytest, and use it to build and test a bigram language model whose perplexity you measure. The last module puts regular expressions to work: groups and lookarounds, tokenisers for messy text, minimum edit distance for spelling correction, and the byte-pair encoding behind modern subword tokenisers. Every module ends with a lab whose starter files and expected output you can check line by line, and the course finishes with a mini-corpus pipeline you build end to end. It prepares you for Statistical NLP: Models, Parsing and Information Retrieval and for Text Representation: From Vectors to Context.

What you'll learn

  • Tokenise, normalise and pattern-match raw text with Python strings, Unicode normalisation and regular expressions
  • Load NLTK's corpora and your own, and compare them with frequency and conditional frequency distributions
  • Build a simple bigram language model from scratch and evaluate its perplexity on test sentences
  • Use WordNet's synsets, lexical relations and similarity scores for basic semantic tasks
  • Write streaming, well-organised NLP scripts with generators, small functions and pytest tests
  • Compute minimum edit distance for spelling correction and explain how subword tokenisers such as BPE split words

Skills you'll gain

Text processing in Python, NLTK, Corpus analysis, Regular expressions, Unicode normalisation, Stemming and lemmatisation, WordNet, Tokenisation, Edit distance, N-gram language models

Tools

Python 3, NLTK, WordNet, pytest

Before you start

Basic Python: variables, loops, functions, lists and dictionaries, and running a script (Python Programming with PyCharm or Python Foundations for Data Science covers them). No NLP experience is needed. You need Python 3.10 or later with NLTK 3.9 or later and pytest (pip install nltk pytest); each lab gives the exact nltk.download() calls for the corpora it uses, about 25 MB in all.

Modules

  1. Language processing and Python essentials

    See text as data a program can count: tokens, types and vocabularies, the Python strings and lists that hold them, a first processing pipeline, and the linguistic ideas every NLP system runs into.

    • Tokens, types and vocabulary (12 min)
    • Python strings and lists for text (12 min)
    • A first text-processing pipeline (12 min)
    • Linguistic ideas behind NLP (10 min)
    • Hands-on lab: Count and compare the words in a short story
  2. Accessing text corpora and WordNet

    Load NLTK's corpora and your own text files, compare categories with conditional frequency distributions and turn bigram counts into probabilities, then use WordNet's synsets and relations to work with meaning.

    • Corpus readers: Gutenberg, Brown and your own files (12 min)
    • Conditional frequency distributions and bigram probabilities (14 min)
    • WordNet: synsets, lemmas and senses (12 min)
    • Lexical relations and similarity (12 min)
    • Hands-on lab: Explore corpora, bigrams and WordNet
  3. Processing raw text and normalisation

    Turn messy real-world text into clean tokens: decode bytes correctly, normalise Unicode, extract text from HTML, and reduce words to stems or lemmas, knowing what each step changes.

    • Encodings and Unicode normalisation (12 min)
    • Text from the web: HTML, entities and boilerplate (12 min)
    • Stemming algorithms (12 min)
    • Lemmatisation (12 min)
    • Hands-on lab: Clean a web page and normalise its words
  4. Structured programming for NLP scripts

    Write NLP code that scales and can be trusted: stream corpora with generators, compose small pure functions, choose data structures and algorithms for counting, and organise and test your code with pytest, ending with a tested bigram language model.

    • Iterators and generators for large corpora (12 min)
    • Functional programming for text (10 min)
    • Algorithm design for text: counting, bigrams and perplexity (14 min)
    • Organising and testing NLP code (12 min)
    • Hands-on lab: Build and test a bigram language model
  5. Tokenisation, edit distance and regex in practice

    Write regular expressions with groups and lookarounds, design tokenisers for messy text, compute edit distance to correct spelling, and see how subword tokenisers such as byte-pair encoding split words.

    • Regular expressions in practice: groups and lookarounds (14 min)
    • Tokenisation strategies and tricky characters (12 min)
    • Minimum edit distance and spelling correction (14 min)
    • Subword tokenisation: byte-pair encoding and its relatives (12 min)
    • Hands-on lab: Tokenise, correct and segment text

Project: Build a mini-corpus processing pipeline

Kilbrack, a small harbour town, has sent you a raw mini-corpus: two months of community noticeboard posts, a page of ferry service updates in HTML, visitor reviews, and a held-out file of May notices. Build an end-to-end Python pipeline that loads every file (extracting the HTML page's text), normalises Unicode and typography, tokenises with your own regular-expression tokeniser, and extracts the emails, dates, euro prices and hashtags with named groups and lookarounds. Report frequency distributions, including a comparison of chosen words across the files with a ConditionalFreqDist. Then build a bigram language model on the training files: report the most probable bigrams with their probabilities, choose a smoothing value k on a development split of the training text, and measure perplexity on the held-out May file. Organise the code as a small, tested package with a command-line entry point, so the whole report comes from one command.

Part of

The products this course names are trademarks of their owners, who haven't made or endorsed it.

Start the course free