Skip to content

mihirzoting/Rule-Based-AI-Chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Raze 🤖

A rule-based AI chatbot — DecodeLabs Project 1


Overview

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.


Features

  • 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, or exit
  • Fallback response when no rule matches
  • Random response selection to avoid repetition

Concepts Used

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()

Project Structure

raze/
├── raze_chatbot.py   # Main chatbot script
└── README.md         # This file

Getting Started

Prerequisites

  • Python 3.6 or higher
  • No external libraries needed (uses random and time from the standard library)

Run the chatbot

python raze_chatbot.py

Example Session

=======================================================
   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.
=======================================================

Chatbot Screenshot

Raze Chatbot Demo


Supported Inputs

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

How It Works

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

Bug Fix — Whole-Word Matching

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 False

Extending Raze

To 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.


Author

Built as part of DecodeLabs — Project 1
Focus: control flow, logical decision-making, conversational AI basics


License

MIT — free to use, modify, and learn from.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages