Definition
Can you build a help-desk bot with only what you've learned so far?
A rule-based chatbot replies to messages by matching them against rules written by people, usually keywords linked to responses, inside a loop that keeps the conversation going until the user leaves. It uses no machine learning, so it is predictable but only handles what its rules cover.
In simple words
The bot cleans each message (lowercase, no extra spaces), looks for keywords such as 'fees' or 'timing', and replies with the matching answer. If nothing matches, it says sorry and suggests what it can help with. Typing 'bye' ends the chat. This is how the first chatbot, ELIZA, worked in 1966.
Why it matters
This project uses everything in the module: strings, dictionaries, loops, conditions and functions. It also shows clearly why modern AI chatbots learn from data: rules break as soon as people word things differently.
How it works
- Write a dictionary of intents: each has keywords and a reply.
- Write clean(message): lowercase it and strip spaces and punctuation.
- Write reply(message): return the reply of the first intent whose keyword appears.
- Add a fallback reply for messages that match nothing.
- Run a loop until the user says bye.
In code
A keyword chatbot with a fallback and a conversation loop.
INTENTS = {
"greeting": (["hello", "hi", "namaste"], "Hello! Ask me about fees, timings or the library."),
"fees": (["fee", "fees", "cost", "price"], "The course is free for all learners."),
"timing": (["time", "timing", "open", "hours"], "We're open 9 am to 6 pm, Monday to Saturday."),
"library": (["library", "book", "books"], "The library is on the first floor."),
}
FALLBACK = "Sorry, I didn't get that. Try asking about fees, timings or the library."
def clean(message):
return "".join(ch for ch in message.lower() if ch.isalnum() or ch == " ").split()
def reply(message):
words = clean(message)
for keywords, answer in INTENTS.values():
if any(word in keywords for word in words):
return answer
return FALLBACK
conversation = ["Hi!", "What are the FEES?", "when do you open", "Can I park here?", "bye"]
for message in conversation:
print("You:", message)
if "bye" in clean(message):
print("Bot: Goodbye, and happy learning!")
break
print("Bot:", reply(message))Output
You: Hi! Bot: Hello! Ask me about fees, timings or the library. You: What are the FEES? Bot: The course is free for all learners. You: when do you open Bot: We're open 9 am to 6 pm, Monday to Saturday. You: Can I park here? Bot: Sorry, I didn't get that. Try asking about fees, timings or the library. You: bye Bot: Goodbye, and happy learning!
Try it yourself
Which intent should match?
For each message, which reply would the bot give?
"Is there a canteen?"
"What hours are you open?"
"How much does it cost?"
"Where can I find books?"
"Is it expensive?"
Extend your bot
Try these changes in the runner above.
- Add a new intent, such as 'exam', with three keywords and a reply.
- Add 'expensive' to the fees keywords and test it.
- Write three messages that should match but don't. What would a learning chatbot do better?
Common mistakes
- Forgetting to lowercase messages, so 'FEES' doesn't match 'fees'.
- Matching parts of words: 'hi' inside 'this'. Split into words first.
- No fallback, so the bot says nothing to unexpected messages.
Check yourself
Summary
- A rule-based bot matches keywords to replies inside a loop.
- Cleaning text, a fallback and an exit word make it usable.
- Its limits show why modern chatbots learn from data.
Finished reading?