A simple rule-based chatbot built with Python. This project demonstrates how to use a JSON file to define conversational intents and a Python script to process user input and generate responses.
-
intents.json
The "brain" of the chatbot. This file contains a structured list of intents, which are categories of user messages. Each intent includes:tag: A unique name for the intent (e.g.,"greeting","goodbye").patterns: Example phrases a user might type that correspond to this intent.responses: Possible replies the chatbot can give for this intent.
-
chatbot.py
The main Python script containing the chatbot logic. It loads the intents, processes user input, and selects an appropriate response.
-
Imports
import json import random import re
json: Loads and parses theintents.jsonfile.random: Randomly selects a response from the list of possible responses for a given intent.re: Uses regular expressions for robust word matching in user messages.
-
SimpleChatbotClass-
Encapsulates all chatbot functionality.
-
__init__(self, intents_path)
Loads the intents from the specified JSON file. -
_load_intents(self, filepath)
Opens and loads the JSON file. Handles:FileNotFoundError: Ifintents.jsonis missing.json.JSONDecodeError: If the JSON file has a syntax error.
-
get_response(self, user_message)
Processes user input:- Converts the message to lowercase and trims whitespace.
- Iterates through each intent and its patterns.
- Uses
re.search()to check if a pattern exists as a whole word in the user's message (prevents partial matches, e.g.,"hi"in"this"). - If a match is found, selects a random response from the intent's responses.
- If no match is found, returns a default "I don't understand" message.
-
-
main()Function- Entry point for running the chatbot from the command line.
- Creates a
SimpleChatbotinstance. - Prints a welcome message.
- Enters a loop to continuously listen for user input.
- If the user types
"quit", the loop breaks and the program ends. - Otherwise, calls
chatbot.get_response()and prints the bot's reply.
-
if __name__ == "__main__":- Ensures the
main()function runs only when the script is executed directly (not when imported as a module).
- Ensures the
-
Make sure you have both
chatbot.pyandintents.jsonin the same directory. -
Open a terminal or command prompt.
-
Navigate to the directory where you saved the files.
-
Run the following command:
python chatbot.py -
Start chatting with the bot!
Typequitto exit.