0% found this document useful (0 votes)
17 views

Rule Based Chatbot

This document outlines the implementation of a rule-based chatbot using the NLTK library in Python. It includes the installation of necessary libraries, the definition of conversation patterns and responses, and the creation of a chatbot class to handle user interactions. The chatbot can respond to various user inputs and continues the conversation until the user types 'quit'.

Uploaded by

bala22112004
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Rule Based Chatbot

This document outlines the implementation of a rule-based chatbot using the NLTK library in Python. It includes the installation of necessary libraries, the definition of conversation patterns and responses, and the creation of a chatbot class to handle user interactions. The chatbot can respond to various user inputs and continues the conversation until the user types 'quit'.

Uploaded by

bala22112004
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

# Install necessary libraries

!pip install nltk

# Import necessary modules


import nltk
import re
from nltk.chat.util import Chat, reflections

# Download NLTK data


nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')

# Define patterns and responses


pairs = [
[r"my name is (.*)", ["Hello %1, how can I assist you today?",]],
[r"hi|hey|hello", ["Hello, how can I help you?", "Hey there! What can I do for
you?", "Hi! How can I assist you today?"]],
[r"what is your name?", ["I am a chatbot created to assist you. You can call me
Chatbot.",]],
[r"how are you?", ["I'm a bot, so I don't have feelings, but I'm here to help
you!",]],
[r"can you help me with (.*)", ["Sure, I can help you with %1. Please provide
more details.",]],
[r"sorry (.*)", ["It's okay. How can I assist you?",]],
[r"thank you|thanks", ["You're welcome!", "No problem!", "Happy to help!"]],
[r"quit", ["Bye! Have a great day!", "Goodbye!"]],
[r"(.*)", ["I'm sorry, I don't understand that. Can you rephrase?", "Could you
please elaborate on that?"]]
]

# Define the chatbot class


class RuleBasedChatbot:
def __init__(self, pairs):
self.chat = Chat(pairs, reflections)

def respond(self, user_input):


return self.chat.respond(user_input)

# Initialize the chatbot


chatbot = RuleBasedChatbot(pairs)

# Function to chat with the bot


def chat_with_bot():
print("Hi, I'm your chatbot. Type 'quit' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() == 'quit':
print("Chatbot: Bye! Have a great day!")
break
response = chatbot.respond(user_input)
print(f"Chatbot: {response}")

# Start chatting with the bot


chat_with_bot()

You might also like