EN VI

Python - Why is my NLTK bot not working correctly?

2024-03-09 23:30:05
Python - Why is my NLTK bot not working correctly?

I am trying to write a chatbot using NLTK, now while the rest of my code works, the quit stop word "quit" returns "hello quit how may I assist you today" instead of "Bye, take care. See you soon!"

import nltk
nltk.download
from nltk.chat.util import Chat, reflections
 
 
pairs = [
 
    (
        r"hi",
        ["Hello what is your name?", ]
    ),
    (
        r"hello",
        ["Hello what is your name?", ]
    ),
    (
        r"(.*)",
        ["Hello %1, how can I help you today?", ]
    ),
    (
        r"my name is (.*)",
        ["Hello %1, how can I help you today?", ]
    ),
    (
        r"what is your name?",
        ["My name is ZeroBot and I'm here to assist you.", ]
    ),
    (
        r"how are you ?",
        ["I'm doing well, thank you!", ]
    ),
    (
        r"sorry (.*)",
        ["It's alright, no problem.", ]
    ),
    (
        r"quit",
        ["Bye, take care. See you soon!"]
    ),
]
 
 
chatbot = Chat(pairs, reflections)
 
 
def Chat():
    print("Hi, I'm ZeroBot. How can I assist you today? Type 'quit' to exit.")
    while True:
        user_input = input("You: ")
        response = chatbot.respond(user_input)
        print("ZeroBot:", response)
        if user_input == "quit":
            break
 
 
Chat()

where am I going Wrong? I am relatively new to both Python and the NLTK chat module so If possible id like for the answer to also explain what I was doing wrong as well as the fix

Solution:

The problem in your code lies in the order of the patterns you define in the pairs list. NLTK's Chat module processes patterns in order, and stops at the first pattern that matches the user's input. In your case, the pattern for "quit" is placed after the generic pattern that matches any input, that's why "quit" is caught by the generic pattern before it reaches the specific "quit" pattern.

To fix this, you need to reorder your pairs list with the more specific patterns placed before the more general patterns.

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

nltk.download("punkt")

pairs = [
    (
        r"hi|hello",
        ["Hello what is your name?", ]
    ),
    (
        r"my name is (.*)",
        ["Hello %1, how can I help you today?", ]
    ),
    (
        r"what is your name?",
        ["My name is ZeroBot and I'm here to help you.", ]
    ),
    (
        r"how are you?",
        ["I'm doing well, thank you!", ]
    ),
    (
        r"sorry (.*)",
        ["It's alright, no problem.", ]
    ),
    (
        r"quit",
        ["Bye, take care. See you soon!"]
    ),
    (
        r"(.*)",
        ["Hello %1, how can I help you today?", ]
    ),
]

chatbot = Chat(pairs, reflections)

def chat():
    print("Hi, I'm ZeroBot. How can I help you today? Type 'quit' to exit.")
    while True:
        user_input = input("You: ")
        response = chatbot.respond(user_input)
        print("ZeroBot:", response)
        if user_input.lower() == "quit":
            print("ZeroBot: Bye, take care. See you soon!")
            break

chat()
  • I rearranged the pattern so that the "quit" pattern comes before the generic pattern.
  • I also added nltk.download("punkt") to ensure the required NLTK data files are downloaded.
  • I modified the condition for checking "quit" to be case insensitive (user_input.lower() == "quit") to handle variations in the user input case.
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login