-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
67 lines (49 loc) · 2.06 KB
/
Copy pathchatbot.py
File metadata and controls
67 lines (49 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Chatbot:
"""A simple rule-based chatbot."""
def __init__(self, name="PyBot"):
self.name = name
self.conversation_history = []
def respond(self, user_input):
"""Generate a response based on user input."""
user_input = user_input.strip().lower()
if not user_input:
return "Please enter a message."
if user_input in ["hello", "hi", "hey"]:
response = f"Hello! I'm {self.name}. How can I help you?"
elif "your name" in user_input:
response = f"My name is {self.name}."
elif "how are you" in user_input:
response = "I'm doing great! Thanks for asking."
elif "python" in user_input:
response = (
"Python is a powerful programming language used "
"in web development, automation, data science, and AI."
)
elif "help" in user_input:
response = (
"You can ask me about my name, Python, or how I am. "
"You can also use the 'history' command."
)
elif user_input == "history":
response = self.get_history()
elif user_input in ["bye", "goodbye", "exit", "quit"]:
response = "Goodbye! Have a great day."
else:
response = (
"I'm not sure how to respond to that. "
"Try typing 'help' to see what I can do."
)
self.conversation_history.append(("User", user_input))
self.conversation_history.append((self.name, response))
return response
def get_history(self):
"""Return the conversation history."""
if not self.conversation_history:
return "There is no conversation history yet."
history = ["Conversation History:"]
for speaker, message in self.conversation_history:
history.append(f"{speaker}: {message}")
return "\n".join(history)
def clear_history(self):
"""Clear the stored conversation history."""
self.conversation_history.clear()