Path: blob/master/Natural Language Processing using Python/simple Chatbot using Nltk.ipynb
3074 views
Kernel: Python 3 (ipykernel)
In [1]:
import random from nltk.chat.util import Chat, reflections # Define additional reflections for generating variations additional_reflections = { "i am": "you are", "i'm": "you're", "i": "you", "my": "your", "you are": "I am", "you're": "I'm", "you": "me", "your": "my", } # Responses for the counselling assistant counselling_responses = [ (r"I feel (.*)", ["Why do you feel {0}?", "I'm sorry to hear that. Can you tell me more about why you feel {0}?"]), (r"(.*) (depressed|sad|unhappy)", ["I'm here to listen. What's been bothering you?", "It's okay to feel down sometimes. What's been on your mind?"]), (r"I need someone to talk to", ["I'm here to listen. You can share anything with me."]), ] def generate_pairs(num_pairs, responses): generated_pairs = [] for _ in range(num_pairs): pattern, responses_list = random.choice(responses) generated_pairs.append((pattern, random.choice(responses_list))) return generated_pairs # Generate 500 pairs generated_pairs = generate_pairs(500, counselling_responses) # Save generated pairs to a file filename = "generated_chat_responses.txt" with open(filename, 'w') as file: for pattern, response in generated_pairs: file.write(f"{pattern}::{response}\n") # Write pattern and response to the file # Function to read pattern-response pairs from a file def read_responses_from_file(filename): responses = [] with open(filename, 'r') as file: for line in file: pattern, response = line.strip().split("::") responses.append((pattern, response)) return responses # File to read generated pattern-response pairs response_file = filename # Create chatbot counselling_chatbot = Chat(read_responses_from_file(response_file), reflections) # Test the chatbot print(counselling_chatbot.respond("I feel sad"))
Out[1]:
l
In [2]:
import tkinter as tk from nltk.chat.util import Chat, reflections # Function to read pattern-response pairs from a file def read_responses_from_file(filename): responses = [] with open(filename, 'r') as file: for line in file: pattern, response = line.strip().split("::") responses.append((pattern, response)) return responses # File containing generated pattern-response pairs response_file = "generated_chat_responses.txt" # Create CounsellingBot counselling_bot = Chat(read_responses_from_file(response_file), reflections) # Function to handle sending a message and getting a response def send_message(): user_input = user_entry.get() user_entry.delete(0, tk.END) if user_input.lower() == "none": bot_response.config(text="Sure, take your time. I'll be here when you're ready to talk.") elif user_input.lower() == "exit": root.destroy() else: response = counselling_bot.respond(user_input) bot_response.config(text=response) # Create the main window root = tk.Tk() root.title("CounsellingBot") # Create widgets user_entry = tk.Entry(root, width=50) send_button = tk.Button(root, text="Send", command=send_message) bot_response = tk.Label(root, text="", wraplength=400) # Place widgets on the grid user_entry.grid(row=0, column=0, padx=5, pady=5) send_button.grid(row=0, column=1, padx=5, pady=5) bot_response.grid(row=1, column=0, columnspan=2, padx=5, pady=5) # Start the main loop root.mainloop()
In [ ]: