how you can create a chatbot using the NLTK library in Python:
Here is the simple python code that can get you started, follow step by step and if you find this helpful don't forget to leave a comment.
import nltk
from nltk.chat.util import Chat, reflections
# Define patterns and responses for the chatbot
patterns = [
(r'hi|hello|hey', ['Hello!', 'Hi there!', 'Hey!']),
(r'how are you?', ['I am doing well, thank you!', 'I\'m fine, thanks!']),
(r'what is your name?', ['My name is ChatBot.', 'You can call me ChatBot.']),
(r'bye|goodbye', ['Goodbye!', 'Bye!', 'Take care!']),
]
# Create a chatbot
chatbot = Chat(patterns, reflections)
# Define a function to chat with the bot
def chat_with_bot():
print("Welcome to ChatBot. Type 'quit' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() == 'quit':
print("ChatBot: Goodbye!")
break
response = chatbot.respond(user_input)
print("ChatBot:", response)
# Start chatting with the bot
if __name__ == "__main__":
chat_with_bot()
This code defines a simple chatbot that responds to basic greetings, inquiries about its name, and farewells. It uses the NLTK library to match patterns in user input and generate responses.
You can expand the patterns and responses to include more complex interactions, integrate with other APIs for more dynamic responses, or even train the chatbot with more data for better conversational abilities.
.jpeg)
Comments
Post a Comment