Programming, problem solving, and algorithms

CPSC 203, 2026 W1

November 26, 2026

Announcements

  • Project 3 (Campus Walks) due Monday December 7. Time to get started.
  • This week: Lab 10 (algorithm complexity) and POTW 10 (permutations) due Sunday Nov 29.
    • Next week: (most) lab sessions open as office hours.
  • Practice Examlet 5 is available.
    • Examlet 5 window is Thursday December 3 to Monday December 7 (after last lecture).
  • Feeling overwhelmed? You are not alone!

Text as Data: Examples

Visualizing Literature

Lexical Novelty

  • Each column is a chapter containing paragraphs.
  • Red indicates paragraphs containing many new terms.

Word Co-occurrence

Cross References

cross references in the King James Bible

Sentiment Analysis

Sentiment Analysis from the LOTR Project

Mapping the Hero’s(?) Journey

Visualization at left by a former student of Cinda’s.

Alternatively, see Interactive Game of Thrones Map.

Text as Data: A Processing Pipeline

Visualizing Literature

  • A novel is just a long sequence of characters.
  • If we treat it as data, we can:
    • Count and visualize patterns,
    • Compare books or authors,
    • Build networks of characters and concepts.
    • …
  • Today: turn a novel into numbers and pictures.

The Text-as-Data Pipeline

Common data flow steps

  1. Load the text from a file.
  2. Tokenize: Split into words.
  3. Preprocess:
    • convert to lowercase
    • remove punctuation
    • remove stopwords
    • use lemmas

Subsequent data flow options

  • Process:
    • Word frequencies.
    • Parts of speech.
    • Cross references.
    • Named entities (people, places, …)
    • …
  • Visualize: Static or interactive.
  • Interpret:
    • What do the results tell us?
    • Do we need to refine and repeat?

Load the Text

For example: Project Gutenberg Canada.

We’ll use a local copy of The Once and Future King by T. H. White.

Tokenization

Break the text into words (or phrases, sentences, paragraphs, …) for subsequent processing.

Example of Tokenized Text

 

 

Visualized: The thirty most common tokens in the mystery text.

We Need to Pre-Process

Actual data often contains cruft.

Removal of cruft often requires iterative manual experimentation.

(not our mystery text)

spaCy for Natural Language Processing

We will use the spaCy open source library.

  • Support for many languages.
  • Rule-based tokenizer.
  • Pre-processing uses a combination of rule-based and learned statistical models.
  • Processing features use learned statistical models.
  • Learned models (and rules) can be updated if desired.

Example Pre-Processing Sequence: Convert Case

lower case eliminate punctuation remove stop words lemma

Should “him” and “Him” be different tokens?

What about “Bob” and “bob”?

Example Pre-Processing Sequence: Remove Punctuation

lower case eliminate punctuation remove stop words lemma

Should “father” and “father.” be different tokens?

What about “Dr.”, “$3.50” or “student(s)”?

Example Pre-Processing Sequence: Remove Uninteresting Words

lower case eliminate punctuation remove stop words lemma

Stop Words: Filter out common and largely unhelpful words.

List of stop words is compiled from a large corpus of representative texts.

Example Pre-Processing Sequence: Lemmatization

lower case eliminate punctuation remove stop words lemma

Reduce word to dictionary form (its lemma).

  • “running” or “ran” -> “run”
  • “better” -> “good”
  • “geese” -> “goose”

Compare Pre- and Post-Lemmatization

Aside: spaCy Representations

  • Language class (nlp) processes the input text into a Doc object (doc).
    • Doc object will keep track of the tokens and their attributes.
    • Processing pipeline will modify the Doc object in-place.
  • Tokens (and many other entities of interest) represent strings.
    • Strings can take significant memory to store.
    • Actual processing in spaCy is done with the hashes of the strings.
    • Many object attributes will store hashes; append underscore to see original string.
  • Don’t throw anything away!

Demo

Launch Class Activity: Visualizing Literature in PrairieLearn.

Contains a mystery text and code to demonstrate:

  • Tokenization.
  • Pre-processing steps: Convert to lower case, eliminate punctuation, remove stop words, and lemmatization.
  • Bar chart visualization of most frequent tokens after each step.