Text as data
Before any model can work with language, text has to become numbers. This module shows the simplest ways to do that — and the choices that matter.
Learning objectives
- Name common business NLP tasks
- Tokenise and normalise text
- Build a bag-of-words matrix
- Use stop words and n-grams appropriately
- Show how vectoriser settings change the features
- Explain where classic NLP is still the better tool
This course uses Python, pandas and scikit-learn. Every code block is a live cell: press Run and it runs in your browser, with nothing to install (the first run on each page takes longer while the libraries load). If you're new to them, take Python for AI and Machine Learning Fundamentals first. Modules 3 to 5 include examples that call an AI model; on these pages they run against a small built-in practice model, so you don't need an API key.
1.1 What is natural language processing?
Natural language processing (NLP) is the set of techniques that let computers work with human language: classifying messages, finding information, measuring sentiment, searching by meaning, translating, summarising and generating text. Large language models are the newest and most powerful NLP tools — but classic techniques remain fast, cheap and surprisingly strong for many business tasks.
| Task | Business example |
|---|---|
| Classification | Route each customer email to billing, delivery or product teams |
| Sentiment analysis | Track whether reviews are getting more negative |
| Information extraction | Pull order numbers, dates and amounts out of messages |
| Semantic search | Find help articles that answer a question, even with different wording |
| Topic discovery | Find out what thousands of survey comments are about |
| Generation | Draft replies or summaries (usually with an LLM) |
1.2 The practice dataset
You'll use 400 synthetic customer messages, each labelled delivery, product or billing. About 8% of the labels are deliberately wrong — real labels are never perfect.
Python · practice data: synthetic customer messagesimport random
import re
import numpy as np
import pandas as pd
random.seed(7)
templates = {
"delivery": ["My parcel arrived {d} days late and the box was crushed",
"Delivery took {d} days and the courier left it in the rain",
"Still waiting for my order after {d} days, no update",
"The tracking link never updated and delivery was {d} days late"],
"product": ["The {p} broke after a week",
"Love the {p}, great quality and well made",
"The {p} is much smaller than the photos suggest",
"Excellent {p}, works perfectly"],
"billing": ["I was charged twice for order {o}",
"Refund for order {o} still not received",
"The discount code did not apply at checkout",
"Invoice for order {o} shows the wrong amount"],
}
products = ["chopping board", "kettle", "desk lamp", "backpack", "water bottle"]
rows = []
for i in range(400):
cat = random.choice(list(templates))
msg = random.choice(templates[cat]).format(
d=random.randint(2, 9), p=random.choice(products), o="GFY-%05d" % random.randint(10000, 99999))
label = cat if random.random() > 0.08 else random.choice(list(templates)) # ~8% noisy labels
rows.append({"text": msg, "category": label})
messages = pd.DataFrame(rows)
print(messages.sample(5, random_state=1).to_string())
print(messages["category"].value_counts())
1.3 From text to tokens
Computers can't do arithmetic on sentences, so the first step is to break text into tokens — usually words or pieces of words — and often to normalise them (lowercase, remove punctuation).
def simple_tokens(text):
text = text.lower()
text = re.sub(r"[^a-z0-9\s-]", " ", text) # keep letters, digits, spaces, hyphens
return text.split()
print(simple_tokens("Refund for order GFY-12345 still NOT received!"))
print(simple_tokens("I can't believe it's broken"))
Choices like these matter. Removing punctuation helps matching but can destroy meaning ("not" vs "not!" is harmless; "can't" becoming "can t" is not, as the second line shows). Lowercasing merges "Apple" the company with "apple" the fruit. There's no universally right answer — check what your task needs.
1.4 Bag of words
The simplest way to turn text into numbers is a bag of words: count how often each word appears, ignoring order. Each message becomes a row; each word in the vocabulary becomes a column.
from sklearn.feature_extraction.text import CountVectorizer
docs = ["parcel arrived late", "refund not received", "parcel late again"]
cv = CountVectorizer()
counts = cv.fit_transform(docs)
print(pd.DataFrame(counts.toarray(), columns=cv.get_feature_names_out()))
Bag of words loses word order — "not good, bad" and "not bad, good" look identical — but for many classification tasks it works remarkably well.
1.5 Stop words and n-grams
Stop words are very common words ("the", "and", "for") that often add little meaning. N-grams keep short sequences of words together, so "not received" or "charged twice" become features in their own right — partly recovering word order.
cv2 = CountVectorizer(stop_words="english", ngram_range=(1, 2), min_df=3)
X_counts = cv2.fit_transform(messages["text"])
print(X_counts.shape) # (messages, features)
top = np.asarray(X_counts.sum(axis=0)).ravel()
print(pd.Series(top, index=cv2.get_feature_names_out()).sort_values(ascending=False).head(10))
Be careful with stop-word lists: scikit-learn's English list includes "not" and "no", which flip meaning in sentiment tasks.
1.6 Every choice changes the numbers
There is no single "bag of words" for a set of messages. Each setting produces a different table for the model to learn from. Compare four set-ups on the same 400 messages:
Python · how settings change the vocabularyoptions = {
"plain words": CountVectorizer(),
"keep capital letters": CountVectorizer(lowercase=False),
"drop stop words": CountVectorizer(stop_words="english"),
"words + pairs of words": CountVectorizer(ngram_range=(1, 2)),
}
for name, vectoriser in options.items():
print(f"{name:24} {vectoriser.fit_transform(messages['text']).shape[1]:5} features")
Adding word pairs more than doubles the number of features; dropping stop words removes about twenty. The order numbers matter more than you might expect: the digits of every "GFY-12345" become a feature of their own, so more than half of the 170 "words" are order numbers that appear once and can teach a model nothing. That is what min_df=3 in the previous cell was for: it ignores anything seen in fewer than three messages.
1.7 Where NLP stands now
For twenty years, everything in this module was the starting point of every text project. Large language models have changed that. For many tasks — summarising, translating, answering questions, pulling fields out of messy text — the first thing to try is now a prompt, not a pipeline.
Classic NLP hasn't gone away. It has moved to the places where it is still the better tool:
- Volume and cost — sorting ten million messages a day costs almost nothing with TF-IDF and real money with an LLM.
- Speed — a classic model answers in under a millisecond, which matters for search-as-you-type or live routing.
- Privacy and control — a small model runs on your own machine, and gives the same answer every time.
- Inside LLM systems — keyword search, embeddings and similarity (Module 3) are how AI assistants find the documents they answer from.
The skill this course builds is choosing: knowing what each approach is good at, measuring both on your own data, and often combining them (Module 5).
Using cv2, find the five most frequent features within each category. Do they match what you'd expect?
Your turn# Write your code here, then press RunShow a solution
for cat, group in messages.groupby("category"):
m = cv2.transform(group["text"])
freq = pd.Series(np.asarray(m.sum(axis=0)).ravel(), index=cv2.get_feature_names_out())
print(cat, list(freq.sort_values(ascending=False).head(5).index))Fit one CountVectorizer with stop_words="english" and one without on the two reviews in the starter code. Print the features each produces. Why is the first a problem for sentiment analysis?
Your turnreviews = ["The kettle is good", "The kettle is not good"]
# Write your code here, then press RunShow a solution
reviews = ["The kettle is good", "The kettle is not good"]
with_stop = CountVectorizer(stop_words="english").fit(reviews)
without = CountVectorizer().fit(reviews)
print("Stop words removed:", list(with_stop.get_feature_names_out()))
print("All words kept: ", list(without.get_feature_names_out()))
# With stop words removed, both reviews reduce to the same words: 'not' has gone,
# so a model can no longer tell praise from complaint.Key takeaways
- NLP covers classification, extraction, search, topics and generation.
- Tokenisation and normalisation choices change meaning — test them.
- Bag of words counts words and ignores order.
- N-grams recover some word order; stop-word lists can remove 'not'.
- Every tokenising choice produces different features; min_df removes one-off noise such as order numbers.
- Classic NLP wins on volume, speed, privacy and predictability; LLMs win on flexibility.