Here’s an example of a simple chatbot written in Python using the library NLTK (Natural Language Toolkit):
import nltk
from nltk.chat.util import Chat, reflections
pairs = [
[
r"my name is (.*)",
["Hello %1, How are you today?"]
],
[
r"hi|hey|hello",
["Hello", "Hey there"]
],
[
r"what is your name?",
["You can call me a chatbot", "My name is Bot, nice to meet you!"]
],
[
r"how are you?",
["I'm doing good, How about You?"]
],
[
r"sorry (.*)",
["Its alright","Its OK, never mind"]
],
[
r"I am fine",
["Great to hear that!"]
],
[
r"quit",
["Bye bye take care. It was nice talking to you :) "]
],
]
chatbot = Chat(pairs, reflections)
chatbot.converse()
This chatbot uses a simple pattern matching approach to respond to user input. The pairs
list contains a set of regular expressions and corresponding responses. The chatbot uses NLTK's Chat
class to match user input against the regular expressions and respond with the corresponding response. The reflections
dictionary is used to handle common ways of rephrasing input. The converse()
method starts the chatbot and allows the user to interact with it.
This is just one example of how you could implement a chatbot in python, there are many other libraries like Rasa, Chatterbot, etc which can be used to build a chatbot.