import random import re import time class SimpleBot: def __init__(self): self.greeting_inputs = ["hello", "hi", "hey", "greetings", "sup", "what's up", "hey there"] self.greeting_responses = ["Hi there!", "Hello!", "Hey!", "Hi! How can I help you today?"] self.farewell_inputs = ["bye", "goodbye", "see you", "see ya", "farewell", "quit", "exit"] self.farewell_responses = ["Goodbye!", "See you later!", "Bye!", "Take care!"] self.unknown_responses = [ "I'm not sure I understand. Could you rephrase that?", "I don't have an answer for that yet.", "I'm still learning. Could you try asking something else?", "Interesting question! I don't have that information right now." ] # Define response patterns self.responses = { r'my name is (.*)|i am (.*)': [ "Nice to meet you, {}!", "Hello, {}! How can I help you today?" ], r'how are you|how are you doing': [ "I'm doing well, thank you for asking!", "I'm great! How about you?" ], r'what is your name|who are you': [ "I'm SimpleBot, your friendly chatbot assistant!", "My name is SimpleBot. How can I help you today?" ], r'what can you do|help': [ "I can answer simple questions and have basic conversations. Try asking me something!", "I'm a simple chatbot that can chat with you. I'm still learning though!" ], r'what time is it|what is the time': [ lambda: f"The current time is {time.strftime('%H:%M:%S')}." ], r'what day is it|what is the date': [ lambda: f"Today is {time.strftime('%A, %B %d, %Y')}." ], r'thank you|thanks': [ "You're welcome!", "Happy to help!", "Anytime!" ] } def respond(self, user_input): # Convert to lowercase user_input = user_input.lower().strip() # Check for greetings if any(greeting in user_input for greeting in self.greeting_inputs): return random.choice(self.greeting_responses) # Check for farewells if any(farewell in user_input for farewell in self.farewell_inputs): return random.choice(self.farewell_responses) # Check for other patterns for pattern, responses in self.responses.items(): match = re.search(pattern, user_input) if match: response = random.choice(responses) # If response is a function, call it if callable(response): return response() # Otherwise, format it with the captured group if any captured = next((g for g in match.groups() if g is not None), "") return response.format(captured) # Default response return random.choice(self.unknown_responses) def chat(self): print("SimpleBot: Hi! I'm SimpleBot. Type 'quit' or 'exit' to end our conversation.") while True: user_input = input("You: ") if user_input.lower() in self.farewell_inputs: print(f"SimpleBot: {random.choice(self.farewell_responses)}") break response = self.respond(user_input) print(f"SimpleBot: {response}") # Create and run the bot if __name__ == "__main__": bot = SimpleBot() bot.chat()