AI chatbot in Python - NAYCode.com 1

AI chatbot in Python – NAYCode.com

#**************** For more tutorial, visit NAYCode.com

from nltk.chat.util import Chat, reflections

pairs = [

    [

        r"my name is (.*)",

        ["Hello %1, How are you today ?",]

    ],

    ]

def MyChatbot_test():

    print("Hi, I am your chatbot.")

    chatbot=Chat(pairs, reflections)

    chatbot.converse()

    if __name__ == '__main__':

        MyChatbot_test()

Here is what the above code is Doing:
1. We are importing the Chat class from the nltk.chat.util module.
2. We are also importing the reflections dictionary from the same module.
3. We are creating a list of lists called pairs. Each list in the list contains a regular expression and a list of responses.
4. We are creating a function called MyChatbot_test().
5. We are printing a message to the console.
6. We are creating an instance of the Chat class and passing the pairs and reflections as parameters.
7. We are calling the converse() method on the chatbot instance.
8. We are calling the MyChatbot_test() function.
9. We are checking if the name of the module is main. If it is, we are calling the MyChatbot_test() function.

Similar Posts