Project: Build a Smart Assistant
Combine everything: build a rule-based chatbot, then upgrade it with a machine-learning classifier.
What you will learn
- Build a working rule-based assistant
- Add an ML model that learns intents from examples
- Reflect on rules vs learning in one project
What you will build
A simple text assistant that answers questions — first with hand-written rules, then upgraded so it learns to understand what the user means. You will experience both kinds of AI in one project.
Phase 1 — a rule-based assistant
Start with the expert-system idea from Unit 3: match keywords to replies.
def assistant(message):
text = message.lower()
if 'hello' in text or 'hi' in text:
return 'Hello! How can I help?'
if 'time' in text:
return 'I cannot check the clock, but your computer can!'
if 'bye' in text:
return 'Goodbye! '
return "Sorry, I did not understand that."
print(assistant('Hi there'))
print(assistant('what is the time'))Note: Output: Hello! How can I help? I cannot check the clock, but your computer can! It works — but only for words you anticipated. “Hey” or “good morning” would fail. That is the limit of rules.
Phase 2 — let it learn intents
Now upgrade it: instead of keywords, train a small model to recognise the intent (what the user is trying to do — say hello, ask the time, say goodbye) behind a message from examples — exactly the supervised pipeline from Unit 4.
Two new scikit-learn tools do the heavy lifting here, and the code uses each one once:
- CountVectorizer — turns each sentence into numbers by counting its words (models cannot read text directly, only numbers). “hi there” becomes a little row of word-counts.
- MultinomialNB — a Naive Bayes classifier, a simple, fast model that is great at sorting text into categories from word counts. It learns which words point to which intent.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
# Example messages and their intent (label)
texts = ['hi', 'hello there', 'hey', 'good morning',
'bye', 'goodbye', 'see you', 'what is the time']
intents = ['greet','greet','greet','greet',
'bye','bye','bye','time']
# Turn text into numbers, then train a classifier
vec = CountVectorizer()
X = vec.fit_transform(texts)
model = MultinomialNB().fit(X, intents)
def smart_assistant(message):
intent = model.predict(vec.transform([message]))[0]
replies = {'greet':'Hello! How can I help?',
'bye':'Goodbye! ',
'time':'Your computer can tell the time!'}
return replies.get(intent, 'I am not sure yet.')
print(smart_assistant('hey there')) # never seen exactly, but learns "greet"
print(smart_assistant('see you later'))Note: Output: Hello! How can I help? Goodbye! The model handles wordings it never saw exactly (“hey there”, “see you later”) because it learned the pattern of each intent — something the rule version could not do.
Walking through that code line by line:
textsandintentsare our training data — each example message paired with its correct intent label (the X and y from Unit 4).vec = CountVectorizer()creates the word-counter;vec.fit_transform(texts)learns the vocabulary and turns every message into a row of numbers, stored inX.MultinomialNB().fit(X, intents)creates the Naive Bayes model and trains it on those numbers and labels — this is the.fit()step you already know.- Inside
smart_assistant,vec.transform([message])converts the new message into numbers the same way, andmodel.predict(...)[0]returns the predicted intent (e.g.greet). replies.get(intent, ...)looks up the matching reply, falling back to a polite default if the intent is unknown.
Your tasks
- Get Phase 1 working with at least 4 keyword rules.
- Build Phase 2 and add a new intent (e.g. “weather”) with a few example messages.
- Test both with wordings you did not put in the rules and compare.
- Reflect: when were rules enough, and when did learning win?
Tip: Build in small steps and run after each change. Add one intent, test it, then add the next.
Note: When this works you have built both classic (rule-based) and modern (machine-learning) AI in one project — and felt the exact trade-off this course is about. Add it to your portfolio!
✍️ Practice
- Add a “thanks” intent to the ML assistant with three example messages.
- Break the rule-based version on purpose with an unusual greeting, then show the ML version handles it.
🏠 Homework
- Write a short note (5–6 lines) explaining, with your project as evidence, when you would choose rules and when you would choose machine learning.