A rule-based AI chatbot — DecodeLabs Project 1
Raze is a simple rule-based chatbot built using core Python concepts:
if-else conditions, a continuous while loop, and predefined responses.
It demonstrates the fundamentals of conversational AI logic without any machine learning.
- Handles greetings (
hello,hi,hey, ...) - Responds to common questions (
how are you,what is your name, ...) - Tells jokes, handles thanks, weather queries, and more
- Graceful exit on
bye,quit, orexit - Fallback response when no rule matches
- Random response selection to avoid repetition
| Concept | Where Used |
|---|---|
while True loop |
Keeps the chat running continuously |
if-elif-else chain |
Core rule engine in get_response() |
break statement |
Exits the loop on farewell commands |
random.choice() |
Picks a response from a predefined list |
str.lower() |
Normalizes input for case-insensitive matching |
| Functions | main(), get_response(), bot_reply() |
raze/
├── raze_chatbot.py # Main chatbot script
└── README.md # This file
- Python 3.6 or higher
- No external libraries needed (uses
randomandtimefrom the standard library)
python raze_chatbot.py=======================================================
Raze — Rule-Based AI Chatbot
=======================================================
Concepts: while loop · if-else logic · rules
Type 'bye' or 'exit' to end the chat.
=======================================================
Bot: Hello! I'm Raze. How can I help?
You: hello
Bot: Hi there! Nice to meet you.
You: tell me a joke
Bot: Why do programmers prefer dark mode? Because light attracts bugs!
You: what is your name
Bot: I'm Raze — a rule-based AI built with pure if-else logic.
You: bye
Bot: Goodbye! The loop has been broken. Come back anytime.
=======================================================
Chat session ended. Loop terminated.
=======================================================
| Input | Rule Triggered |
|---|---|
hello, hi, hey |
Greeting handler |
how are you |
Status query |
what is your name |
Identity query |
what can you do, help |
Capability query |
tell me a joke |
Joke handler |
thanks, thank you |
Gratitude handler |
weather, forecast |
Weather fallback |
bye, exit, quit |
Exit → break |
| (anything else) | else fallback |
User types input
│
▼
while True:
│
├── if exit command → print goodbye → break
├── elif greeting → greeting response
├── elif how are you → status response
├── elif name query → identity response
├── elif help → capability response
├── elif joke → joke response
├── elif thanks → gratitude response
├── elif weather → weather response
└── else → fallback response
Earlier versions used "hi" in text style substring checks, which caused false matches — e.g. "what" contains "hi" (w-hi-ch... wait, "while", "which"), triggering the greeting rule incorrectly.
The fix uses re.search(r'\bkeyword\b', text) — \b is a word boundary anchor, so "hi" only matches the standalone word hi, not as part of a longer word.
def word_match(keywords, text):
for kw in keywords:
pattern = r'\b' + re.escape(kw) + r'\b'
if re.search(pattern, text):
return True
return FalseTo add a new rule, do two things:
1. Add responses to the RESPONSES dictionary in raze_chatbot.py:
"my_rule": [
"Response option 1.",
"Response option 2.",
],2. Add an elif branch inside get_response():
elif "my keyword" in text:
bot_reply("my_rule")That's it — no retraining, no models, just logic.
Built as part of DecodeLabs — Project 1
Focus: control flow, logical decision-making, conversational AI basics
MIT — free to use, modify, and learn from.
