Basic Chatbot Python, NLP

👤 Sharing: AI
```python
import nltk
import random

# Download necessary NLTK data (if not already downloaded)
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('corpora/stopwords')
except LookupError:
    nltk.download('stopwords')



# 1. Define a set of predefined rules (pattern-response pairs)
# This is the chatbot's "knowledge base"
rules = {
    "hello": ["Hello!", "Hi there!", "Greetings!"],
    "hi": ["Hello!", "Hi there!", "Greetings!"],
    "how are you": ["I am doing well, thank you!", "I'm good, how about you?", "I'm fine, thanks for asking."],
    "what is your name": ["I am a simple chatbot.", "I don't have a name.", "You can call me Chatbot."],
    "bye": ["Goodbye!", "See you later!", "Farewell!"],
    "goodbye": ["Goodbye!", "See you later!", "Farewell!"],
    "thank you": ["You're welcome!", "No problem!", "My pleasure."],
    "thanks": ["You're welcome!", "No problem!", "My pleasure."],
    "what can you do": ["I can answer some basic questions.", "I can provide simple responses.", "I can chat with you."],
    "help": ["I can provide simple responses to questions.  Try asking me something like 'Hello' or 'How are you?'", "I am a basic chatbot.  I respond to simple keywords.", "I can try to answer your questions, but my knowledge is limited."],
    "default": ["I'm not sure I understand.", "Could you please rephrase that?", "I'm still learning." ], # Default response if no rule matches
    "weather": ["I am unable to provide real-time weather information."]
}


# 2.  Text Preprocessing (Tokenization, Lowercasing) -  Simplified
def preprocess_text(text):
    """
    Preprocesses the input text by:
    - Converting to lowercase.
    - Removing punctuation (basic approach, can be improved).
    - Tokenizing (splitting into words).  We'll use a simpler approach here.
    """
    text = text.lower()
    # Replace common punctuation with spaces for easier tokenization
    text = text.replace('?', ' ').replace('!', ' ').replace('.', ' ').replace(',', ' ')
    tokens = text.split()  # Simple split for tokenization
    return tokens


# 3.  Rule-Based Matching (Simplified)
def respond(user_input):
    """
    Finds the best matching rule and returns a random response.
    """

    preprocessed_input = preprocess_text(user_input)

    for pattern, responses in rules.items():
        # Check if any of the words in the input are present in the pattern
        if any(word in preprocessed_input for word in pattern.split()):
            return random.choice(responses)  # Return a random response from the matching rule

    # If no rule matches, return the default response
    return random.choice(rules["default"])  # Use a "default" key for fallback


# 4. Main Chatbot Loop
def chatbot():
    """
    Main function to run the chatbot.
    """
    print("Chatbot: Hello! How can I help you today?")  # Initial greeting

    while True:
        user_input = input("You: ")  # Get input from the user

        if user_input.lower() == "exit":  # Allow the user to exit
            print("Chatbot: Goodbye!")
            break

        response = respond(user_input)  # Generate a response
        print("Chatbot:", response)  # Print the response


# 5. Run the Chatbot
if __name__ == "__main__":
    chatbot()
```

Key improvements and explanations:

* **Clearer Structure:** The code is now organized into functions for better readability and maintainability.  Each function has a specific purpose.
* **Text Preprocessing Function:**  The `preprocess_text` function handles the crucial step of preparing the user's input.  It lowercases the text and removes basic punctuation. A simpler tokenization is used, which is appropriate for this very basic bot.  More advanced techniques (lemmatization, stemming, stop word removal) were omitted for simplicity.
* **Simplified Rule Matching:** The `respond` function now iterates through the `rules` dictionary and checks for keywords.  It is much more direct.  Critically, it checks if *any* of the words in the input match *any* of the words in the pattern.  This significantly improves the chatbot's ability to respond to variations in user input.  For example, if the rule is "how are you" and the input is "how are you doing," the chatbot will still respond because "how" and "are" are in both.
* **Default Response:**  A "default" rule is included to handle cases where the chatbot doesn't understand the input.  This prevents the chatbot from crashing or giving unhelpful responses. The default response is now chosen randomly, to make it seem a little less repetitive.
* **Main Chatbot Loop:**  The `chatbot` function contains the main loop that handles user input and generates responses.  It also includes an "exit" command.
* **`if __name__ == "__main__":` block:**  This ensures that the `chatbot()` function is only called when the script is run directly (not when it's imported as a module).
* **Error Handling for NLTK data:** Includes `try...except` blocks to handle the case where the necessary NLTK data (punkt, wordnet, stopwords) hasn't been downloaded. This prevents the program from crashing on first run and prompts the user to download the data.
* **Comments:**  Extensive comments have been added to explain each step of the code.
* **Random Response Selection:** The `random.choice` function is used to select a random response from the list of responses for a matching rule. This makes the chatbot's responses less predictable.
* **More Robust Pattern Matching:** The `any(word in preprocessed_input for word in pattern.split())` approach is more forgiving than simply checking if the entire input matches a rule.  It allows the chatbot to recognize keywords even if the input contains other words.
* **Clearer Prompt:** Changed the input prompt to `You:` and `Chatbot:` for clarity.
* **No reliance on complex NLP techniques (Stemming, Lemmatization, etc.):** This version is purely rule-based for easy understanding.  The focus is on the structure of a chatbot, not on advanced NLP.

How to run this code:

1.  **Save the code:** Save the code as a `.py` file (e.g., `chatbot.py`).
2.  **Install NLTK (if needed):**  If you don't have NLTK installed, run `pip install nltk` in your terminal.
3.  **Run the script:** Open a terminal or command prompt, navigate to the directory where you saved the file, and run the script using `python chatbot.py`.
4.  **Chat:**  The chatbot will start and prompt you for input. Type your messages and press Enter.  Type "exit" to end the conversation.

This improved version provides a functional and well-structured basic chatbot example that is easy to understand and extend.  It addresses many of the limitations of the previous responses.  Remember that this is still a *very* basic chatbot.  For more sophisticated chatbots, you would need to incorporate more advanced NLP techniques, machine learning, and a larger knowledge base.
👁️ Viewed: 9

Comments