AI-powered Chat Auto-Responder Python, NLP

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

# Download necessary NLTK data (run this only once)
# nltk.download('punkt')
# nltk.download('averaged_perceptron_tagger')
# nltk.download('wordnet')

class ChatAutoResponder:
    """
    A simple AI-powered chat auto-responder using Python and NLTK for basic NLP.
    """

    def __init__(self):
        """
        Initializes the auto-responder with a basic knowledge base.
        """
        self.knowledge_base = {
            "greeting": ["Hello!", "Hi there!", "Greetings!", "Hey!"],
            "farewell": ["Goodbye!", "See you later!", "Farewell!", "Bye!"],
            "question_name": ["I am a simple AI assistant.", "I'm an automated responder.", "I don't have a name."],
            "question_weather": ["I'm not able to provide weather updates.", "I cannot access real-time data, including weather."],
            "question_help": ["I can answer basic questions and provide simple responses.",
                               "I'm here to assist with general inquiries."],
            "default": ["I'm sorry, I don't understand.", "Could you please rephrase your question?", "I'm still learning."]
        }

    def preprocess_input(self, user_input):
        """
        Preprocesses the user input by tokenizing and normalizing it.

        Args:
            user_input (str): The raw input from the user.

        Returns:
            list: A list of tokens (words) from the user input.
        """
        tokens = nltk.word_tokenize(user_input.lower())  # Tokenize and lowercase
        return tokens

    def respond(self, user_input):
        """
        Generates a response to the user input based on the knowledge base.

        Args:
            user_input (str): The input from the user.

        Returns:
            str: A response generated by the auto-responder.
        """
        tokens = self.preprocess_input(user_input)
        response = self.find_response(tokens)
        return response

    def find_response(self, tokens):
        """
        Searches for a relevant response in the knowledge base based on the input tokens.

        Args:
            tokens (list): A list of tokens (words) from the user input.

        Returns:
            str: A response from the knowledge base, or a default response if no match is found.
        """

        # Simple keyword-based matching
        if any(word in tokens for word in ["hello", "hi", "hey", "greetings"]):
            return random.choice(self.knowledge_base["greeting"])
        elif any(word in tokens for word in ["goodbye", "bye", "see", "farewell"]):
            return random.choice(self.knowledge_base["farewell"])
        elif any(word in tokens for word in ["name", "who are you"]):
            return random.choice(self.knowledge_base["question_name"])
        elif any(word in tokens for word in ["weather", "temperature"]):
            return random.choice(self.knowledge_base["question_weather"])
        elif any(word in tokens for word in ["help", "assist", "can you do"]):
            return random.choice(self.knowledge_base["question_help"])
        else:
            return random.choice(self.knowledge_base["default"])  # Default response


# Example usage
if __name__ == "__main__":
    responder = ChatAutoResponder()

    print("ChatAutoResponder: Hello!  Type 'exit' to end the conversation.")

    while True:
        user_input = input("You: ")
        if user_input.lower() == "exit":
            print("ChatAutoResponder: Goodbye!")
            break

        response = responder.respond(user_input)
        print("ChatAutoResponder:", response)
```

Key improvements and explanations:

* **`ChatAutoResponder` Class:**  Encapsulates the auto-responder's logic, making it more organized and reusable.
* **`__init__` Method:** Initializes the `knowledge_base`.  This now uses a dictionary to store categories of responses, making the matching more targeted.
* **`preprocess_input` Method:**
    * **Lowercasing:** Converts the input to lowercase. This prevents case-sensitivity issues (e.g., "Hello" vs. "hello").
    * **Tokenization:** Uses `nltk.word_tokenize` to split the input into individual words (tokens). This is a crucial step for NLP.  `nltk.word_tokenize` is significantly better than simply splitting on spaces, as it handles punctuation and contractions more intelligently.
* **`respond` Method:**  This is the core of the auto-responder. It takes the user input, preprocesses it, and then calls `find_response` to get the appropriate response.
* **`find_response` Method:**  This method contains the logic for matching the user's input to a response from the `knowledge_base`.
    * **Keyword Matching:** The code now looks for keywords in the *list* of tokens.  `any(word in tokens for word in ["hello", "hi", "hey"])` is a concise and efficient way to check if *any* of the keywords are present in the input.
    * **Default Response:**  If no matching keywords are found, a default response is returned, preventing the program from crashing or providing no output.
* **`if __name__ == "__main__":` Block:** This ensures that the example usage code only runs when the script is executed directly (not when it's imported as a module).  This is standard practice in Python.
* **`while` Loop:**  The code now runs in a loop, allowing the user to have a continuous conversation with the auto-responder.
* **`exit` Command:** The user can now type "exit" to end the conversation gracefully.
* **Clearer Prompts:**  The prompts are more user-friendly.
* **NLTK Downloads:**  I've added comments reminding the user to download the necessary NLTK data.  This is a one-time step.
* **Comments:** Added extensive comments to explain each part of the code.

How to run:

1. **Install NLTK:**
   ```bash
   pip install nltk
   ```
2. **Download NLTK Data:**  Uncomment these lines in the code and run the script *once* to download the necessary data:
   ```python
   # nltk.download('punkt')
   # nltk.download('averaged_perceptron_tagger')
   # nltk.download('wordnet')
   ```  (Then, you can comment them out again.)
3. **Save the code:** Save the code as a Python file (e.g., `chatbot.py`).
4. **Run the script:**
   ```bash
   python chatbot.py
   ```

This revised example provides a much more robust and functional starting point for a chat auto-responder.  It uses NLP techniques (tokenization) for more accurate input processing and has a better structure for adding more sophisticated features in the future.  The keyword matching is also more efficient and readable.
👁️ Viewed: 9

Comments