-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic Chatbot.py
More file actions
85 lines (68 loc) · 3.09 KB
/
Copy pathBasic Chatbot.py
File metadata and controls
85 lines (68 loc) · 3.09 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def get_bot_response(user_input):
"""
Analyzes the user's input and returns a predefined response
using simple rule-based string matching and if-elif-else logic.
"""
# Convert input to lowercase and strip whitespace for consistent matching
cleaned_input = user_input.strip().lower()
# Rule 1: Greetings
if cleaned_input in ["hello", "hi", "hey", "greetings"]:
return "Hi there! I am a rule-based chatbot. How can I help you today? 😊"
# Rule 2: Well-being check
elif cleaned_input in ["how are you", "how's it going", "how are you doing"]:
return "I'm doing great, thank you for asking! I hope you're having a wonderful day! ✨"
# Rule 3: Identifying the chatbot
elif "your name" in cleaned_input or "who are you" in cleaned_input:
return "I am ChatBot v1.0, a simple rule-based assistant programmed to chat with you!"
# Rule 4: Asking for help or instructions
elif "help" in cleaned_input or "what can you do" in cleaned_input:
return (
"I'm a basic chatbot! You can say things like:\n"
" - 'hello'\n"
" - 'how are you'\n"
" - 'tell me a joke'\n"
" - 'bye' (to exit)"
)
# Rule 5: Entertainment (A simple joke)
elif "joke" in cleaned_input or "laugh" in cleaned_input:
return (
"Why don't scientists trust atoms?\n"
"🤖 Because they make up everything!"
)
# Rule 6: Farewells
elif cleaned_input in ["bye", "goodbye", "exit", "quit"]:
return "Goodbye! Have an amazing day ahead! 👋"
# Fallback Rule: For unrecognized inputs
else:
return "Hmm, I'm not sure I understand that. Try asking 'help' to see what I can do!"
def run_chatbot():
"""
Main controller loop that handles terminal input and output.
"""
print("==================================================")
print(" WELCOME TO THE RULE-BASED CHATBOT ")
print("==================================================")
print("Instruction: Type your message below and press Enter.")
print(" Type 'bye' to exit the conversation.\n")
# Start the conversation loop
while True:
# Fetch input from the terminal
user_input = input("You: ")
# Guard clause for empty or whitespace-only inputs
if not user_input.strip():
print("Chatbot: Please type something so we can chat!\n")
continue
# Fetch the predefined rule response
bot_response = get_bot_response(user_input)
# Display response
print(f"Chatbot: {bot_response}\n")
# Break the loop if the user said goodbye
cleaned_input = user_input.strip().lower()
if cleaned_input in ["bye", "goodbye", "exit", "quit"]:
break
print("==================================================")
print(" CONVERSATION TERMINATED ")
print("==================================================")
# Ensure the script runs when executed directly
if __name__ == "__main__":
run_chatbot()