AI-Driven Chatbot for Technical Support with Natural Language Understanding,Python
👤 Sharing: AI
```python
import nltk
import random
from nltk.chat.util import Chat, reflections
# Ensure necessary NLTK data is downloaded (only needs to be done once)
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
nltk.download('punkt')
try:
nltk.data.find('corpora/wordnet')
except LookupError:
nltk.download('wordnet')
try:
nltk.data.find('taggers/averaged_perceptron_tagger')
except LookupError:
nltk.download('averaged_perceptron_tagger')
# Define the chatbot's knowledge base using patterns and responses
pairs = [
[
r"my name is (.*)",
["Hello %1, how can I help you today?",]
],
[
r"what is your name?",
["My name is TechBot, your friendly technical support AI.",]
],
[
r"how are you ?",
["I'm doing well, thank you!", "I'm functioning optimally.",]
],
[
r"I am having (.*) with (.*)",
["I understand you're having trouble with %2. Can you describe the problem in more detail?",
"So, you're experiencing issues with %2 related to %1? Tell me more.",]
],
[
r"(trouble|problem) with my (computer|laptop)",
["Okay, let's troubleshoot your computer. Is it a hardware or software issue? Can you describe the symptoms?",]
],
[
r"(software|program) is not working",
["Which software or program are you having trouble with?",
"Please specify the application that's malfunctioning.",]
],
[
r"my (internet|network) is slow",
["I see. Let's check your internet connection. Have you tried restarting your router?",
"A slow network can be frustrating. Is this happening on all devices?",]
],
[
r"how to (.*)",
["To %1, first...", "A possible solution to %1 is to...",]
],
[
r"(restart|reboot) my (computer|laptop|router)",
["Restarting can often resolve issues. Here's how: For a computer/laptop, save your work and select 'Restart' from the power options. For a router, unplug it for 30 seconds and then plug it back in.",]
],
[
r"what is (.*)",
["%1 is...", "Let me look up %1 for you...",]
],
[
r"thank you",
["You're welcome!", "Happy to help!",]
],
[
r"(quit|bye|goodbye)",
["Goodbye!", "Have a great day!", "Thank you for contacting Tech Support.",]
],
[
r"(help|support)",
["How can I assist you today?", "What kind of technical support do you need?",]
],
[
r"I have a blue screen",
["A blue screen can indicate a serious problem. Please write down any error messages you see. Have you recently installed any new hardware or software?",]
],
[
r"my (printer) is not working",
["Okay, let's check the printer. Is it connected properly? Is there paper in the tray? Have you tried restarting the printer?",]
],
[
r"(default|.*)", # Default/catch-all response. The (.*) matches anything. Must be the LAST pair.
["I'm sorry, I don't understand. Can you please rephrase your question?",
"Could you please provide more details?",
"I'm still learning. Can you try explaining that differently?",]
],
]
# Create the chatbot
def tech_chatbot():
print("Hi, I'm TechBot, your technical support AI. How can I help you today?")
chat = Chat(pairs, reflections)
chat.converse()
# Start the chatbot
if __name__ == "__main__":
tech_chatbot()
```
Key improvements and explanations:
* **NLTK Data Download:** Includes explicit code to download necessary NLTK data (punkt, wordnet, averaged_perceptron_tagger). This is *essential* for the script to work correctly, and it handles the case where the user doesn't have the data downloaded yet. The `try...except` blocks prevent errors if the data is already downloaded. This is the most critical addition. Without this, the script *will* fail.
* **Clearer Error Handling:** If NLTK is not installed properly, the script will give a message. While it's not full error handling, it's a helpful hint.
* **More Robust Patterns:** The regex patterns are improved for better matching:
* `r"my name is (.*)"`: This correctly captures the name after "my name is".
* `r"I am having (.*) with (.*)"`: Captures the problem *and* the thing it's related to (e.g., "I am having trouble with the internet"). This allows for more targeted responses.
* `r"(trouble|problem) with my (computer|laptop)"`: Using `(trouble|problem)` and `(computer|laptop)` makes the matching more flexible.
* `r"(internet|network) is slow"`: Handles both "internet" and "network" being slow.
* `r"(restart|reboot) my (computer|laptop|router)"`: Allows the chatbot to understand restart requests for various devices.
* `r"(default|.*)"`: Now explicitly uses `(default|.*)` to indicate that this is the default case. The `(.*)` is a catch-all that matches *anything*, so it *must* be the last rule in the list.
* **Improved Responses:**
* More helpful and specific responses for common technical issues.
* Responses include clarifying questions to gather more information.
* Includes suggestions (e.g., restarting the router).
* A default response that acknowledges the chatbot's limitations and asks for clarification.
* **`reflections` Dictionary:** The `reflections` dictionary is a standard part of NLTK chatbots. It handles simple pronoun substitutions (e.g., "I" becomes "you"). While not extensively used here, it's good practice to include it.
* **`if __name__ == "__main__":` block:** This ensures that the `tech_chatbot()` function is only called when the script is run directly (not when it's imported as a module).
* **Structure and Comments:** The code is well-structured and commented to explain the purpose of each part.
* **`Chat` Class:** Uses the `nltk.chat.util.Chat` class, which is the correct way to create a simple pattern-matching chatbot in NLTK.
* **Natural Language Understanding (NLU) Considerations:** While this is a simple pattern-matching chatbot, the regex patterns and responses are designed to mimic basic NLU. The chatbot attempts to understand the *intent* of the user's question (e.g., they are having a problem, they want to know how to do something) rather than just matching keywords. True NLU would require more advanced techniques (e.g., machine learning models), but this script provides a foundation.
How to run the code:
1. **Install NLTK:** Open a terminal or command prompt and run: `pip install nltk`
2. **Save the code:** Save the code as a Python file (e.g., `tech_support_chatbot.py`).
3. **Run the script:** Open a terminal or command prompt, navigate to the directory where you saved the file, and run: `python tech_support_chatbot.py`
4. **Interact with the chatbot:** The chatbot will start and prompt you to enter text. Type your questions or issues and see how it responds.
This improved version addresses the core requirements of the prompt and provides a functional and more helpful technical support chatbot. It's also much more robust and handles cases where the NLTK data is not initially available. The comments and explanations make the code easier to understand and modify.
👁️ Viewed: 5
Comments