-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
191 lines (148 loc) · 4.11 KB
/
Copy pathchatbot.py
File metadata and controls
191 lines (148 loc) · 4.11 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
Project 1: Rule-Based AI Chatbot
DecodeLabs Industrial Training Program – Batch 2026
Author: Syed Faran Ali
Description:
A rule-based chatbot that demonstrates fundamental Artificial
Intelligence concepts using predefined rules, decision-making
logic, and continuous user interaction.
Concepts Covered:
- Rule-Based AI
- Control Flow (if-elif-else)
- Functions
- Dictionaries
- Loops
- Input Validation
- String Manipulation
- Modular Programming
"""
from datetime import datetime
# ==========================================================
# Configuration
# ==========================================================
BOT_NAME = "RuleBot"
EXIT_COMMANDS = {"exit", "quit", "bye"}
RESPONSES = {
"hello": "Hello! How can I assist you today?",
"hi": "Hi! Nice to meet you.",
"hey": "Hey! How can I help you?",
"good morning": "Good morning! Have a productive day.",
"good afternoon": "Good afternoon!",
"good evening": "Good evening!",
"how are you": "I'm doing great. Thanks for asking!",
"what is your name": (
f"I'm {BOT_NAME}, a Rule-Based AI Chatbot."
),
"who are you": (
f"I'm {BOT_NAME}, a Rule-Based AI Chatbot."
),
"who created you": (
"I was developed by Syed Faran Ali "
"during the DecodeLabs AI Internship."
),
"thanks": "You're welcome! Happy to help.",
"thank you": "You're welcome! Happy to help.",
"joke": (
"Why do programmers prefer Python? "
"Because it makes coding simple and enjoyable!"
),
}
HELP_MESSAGE = """
Available Commands
------------------
Greetings:
hello
hi
hey
good morning
good afternoon
good evening
General:
how are you
what is your name
who are you
who created you
joke
date
time
Utility:
help
exit
quit
bye
"""
# ==========================================================
# Helper Functions
# ==========================================================
def display_banner() -> None:
"""Display the chatbot welcome banner."""
print("=" * 60)
print(" DecodeLabs AI Internship")
print(" Project 1 - Rule-Based AI Chatbot")
print("=" * 60)
print(f"Welcome! I'm {BOT_NAME}.")
print("Type 'help' to view available commands.")
print("Type 'exit' to end the conversation.")
print("=" * 60)
def sanitize_input(user_input: str) -> str:
"""
Normalize user input.
Parameters:
user_input (str): Raw user input.
Returns:
str: Cleaned input.
"""
return user_input.strip().lower()
def get_date() -> str:
"""Return current date."""
return datetime.now().strftime("%d %B %Y")
def get_time() -> str:
"""Return current time."""
return datetime.now().strftime("%I:%M %p")
def generate_response(user_input: str) -> tuple[str, bool]:
"""
Process user input and return chatbot response.
Returns:
tuple:
response (str)
exit_chat (bool)
"""
command = sanitize_input(user_input)
# Exit Commands
if command in EXIT_COMMANDS:
return (
"Goodbye! Thank you for chatting with me. Have a wonderful day!",
True,
)
# Help
if command == "help":
return HELP_MESSAGE, False
# Date
if command == "date":
return f"Today's date is {get_date()}.", False
# Time
if command == "time":
return f"The current time is {get_time()}.", False
# Dictionary Lookup
if command in RESPONSES:
return RESPONSES[command], False
# Unknown Command
return (
"Sorry, I couldn't understand your request.\n"
"Type 'help' to see the list of available commands.",
False,
)
def chatbot() -> None:
"""Main chatbot conversation loop."""
display_banner()
while True:
user_input = input("\nYou: ")
response, exit_chat = generate_response(user_input)
print(f"{BOT_NAME}: {response}")
if exit_chat:
break
# ==========================================================
# Entry Point
# ==========================================================
if __name__ == "__main__":
chatbot()