Repository navigation
Expand file tree
/
Copy pathPyBot.py
More file actions
472 lines (397 loc) · 14.8 KB
/
Copy pathPyBot.py
File metadata and controls
472 lines (397 loc) · 14.8 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# CodeAlpha Python Programming Internship
# TASK: Basic ChatBot
# Author: Ahmed Nadeem
# Project Name: PyBot
import random
import string
import os
# --------------------------------------------------
# BASIC PROJECT INFORMATION
# --------------------------------------------------
BOT_NAME = "PyBot"
AUTHOR_NAME = "Ahmed Nadeem"
exit_commands = [
"bye",
"goodbye",
"exit",
"quit",
"stop",
"bye bye"
]
# --------------------------------------------------
# RESPONSE LISTS
# PyBot will randomly chooses one reply from each list.
# --------------------------------------------------
greeting_replies = [
"Hey there! Ready to explore Python and AI?",
"Hi! It is great to have you here.",
"Hello, future programmer! What would you like to know?"
]
how_are_you_replies = [
"I am functioning perfectly and ready to help!",
"I am great! I hope you are doing well too.",
"I am always ready to chat and share something useful."
]
about_replies = [
"I am PyBot, your smart Python and AI companion.",
"My name is PyBot. I am a rule-based chatbot created using Python.",
"I am PyBot, developed for the CodeAlpha Python Programming Internship.",
]
python_replies = [
"Python is a powerful and beginner-friendly programming language.",
"Python is a high-level programming language known for its simple and readable syntax.",
"Python is widely used in automation, web development, data science, and artificial intelligence."
]
python_fact_replies = [
"Python was named after the comedy group Monty Python, not the snake!",
"Python was created by Guido van Rossum and first released in 1991.",
"Python is widely used in artificial intelligence, automation, and data science.",
]
ai_replies = [
"Artificial intelligence allows computers to perform tasks that normally require human intelligence.",
"AI is the ability of machines to analyze information, recognize patterns, and make decisions.",
"Artificial intelligence is used in areas such as healthcare, banking, transport, and education."
]
ai_fact_replies = [
"Some AI systems can analyze large amounts of data much faster than humans.",
"AI is already used in recommendation systems, virtual assistants, and self-driving technology.",
"AI can support humans, but responsible human supervision is still important."
]
programming_tip_replies = [
"Do not only read code. Write it, run it, make mistakes, and fix those mistakes.",
"Break a large problem into smaller steps before writing your code.",
"Read error messages carefully because they often explain what went wrong.",
]
joke_replies = [
"Why did the Python programmer wear glasses? Because they could not C!",
"Why was the computer cold? Because it left its Windows open!",
"When I wrote this code, only God and I understood it. Now, only God knows"
]
general_fact_replies = [
"The first computer bug was an actual moth found inside a computer.",
"The first computer mouse was made of wood.",
"The first website is still available on the internet."
]
motivation_replies = [
"Every expert was once a beginner. Keep learning and keep building.",
"Do not be afraid of errors. Every error teaches you something new.",
"Success comes from consistency, patience, and continuous improvement.",
]
thanks_replies = [
"You are welcome! I am glad I could help.",
"My pleasure! Feel free to ask another question.",
"Glad to be helpful!"
]
goodbye_replies = [
"Goodbye! Keep learning and keep coding.",
"It was great chatting with you. Have a wonderful day!",
"Thank you for using PyBot. See you again!"
]
unknown_replies = [
"I am still learning. Could you ask that in another way?",
"I did not understand that question. Type 'help' to see suggestions.",
"That topic is not available in my knowledge base yet.",
]
suggestion_tips = [
"Try asking: Tell me a fun fact about Python.",
"Try asking: What is artificial intelligence?",
"Try asking: Tell me a joke.",
]
# --------------------------------------------------
# CLEAN THE USER'S MESSAGE
# Example: "Hello!" becomes "hello"
# --------------------------------------------------
def clean_message(message):
message = message.lower()
# Remove punctuation marks such as ? ! , .
message = message.translate(
str.maketrans("", "", string.punctuation)
)
# Remove unnecessary spaces
message = " ".join(message.split())
return message
# --------------------------------------------------
# HELP MENU
# --------------------------------------------------
def show_help():
print(" ==========================================")
print(" PyBot HELP MENU")
print(" ==========================================")
print("You can try asking:")
print("1. Hello")
print("2. How are you?")
print("3. What is your name?")
print("4. Who created you?")
print("5. Are you a real AI?")
print("6. What is Python?")
print("7. Is Python difficult?")
print("8. Tell me a fun fact about Python")
print("9. What is artificial intelligence?")
print("10. Tell me a fun fact about AI")
print("11. Will AI replace humans?")
print("12. What is machine learning?")
print("13. Tell me about CodeAlpha")
print("14. Tell me a joke")
print("15. Motivate me")
print("16. Thank you")
print("17. Bye")
print(" ==========================================")
# --------------------------------------------------
# MAIN REPLY FUNCTION
# This function will check the message and returns a reply.
# --------------------------------------------------
def get_reply(message):
words = message.split()
# Greetings
if message in [
"hello",
"hi",
"hey",
"good morning",
"good evening"
]:
return random.choice(greeting_replies)
# General conversation
elif message == "how are you":
return random.choice(how_are_you_replies)
elif (
message in ["what is your name", "whats your name"]
or "tell me about yourself" in message
or "tell me more about yourself" in message
):
return random.choice(about_replies)
elif message == "who created you":
return (
f"I was created by {AUTHOR_NAME} for the "
"CodeAlpha Python Programming Internship."
)
elif message == "what can you do":
return (
"I can answer questions about Python, AI, programming, "
"internships, jokes, fun facts, and motivation."
)
elif message in [
"are you a real ai",
"are you real ai"
]:
return (
"I am a rule-based chatbot. I do not think like an advanced "
"AI model, but I can reply using predefined rules and "
"keyword matching."
)
# Python questions
elif message in [
"what is python",
"define python"
]:
return random.choice(python_replies)
elif message == "why should i learn python":
return (
"Python is beginner-friendly and is widely used in "
"automation, web development, data science, and "
"artificial intelligence."
)
elif message == "is python difficult":
return (
"Python is considered one of the easiest programming "
"languages for beginners, especially when you practise "
"one concept at a time."
)
elif (
"fun fact about python" in message
or "python fun fact" in message
or "python fact" in message
or "fun fact about pyhton" in message
):
return random.choice(python_fact_replies)
# Artificial intelligence questions
elif message in [
"what is artificial intelligence",
"what is ai",
"define ai"
]:
return random.choice(ai_replies)
elif (
"fun fact about ai" in message
or "ai fun fact" in message
or "ai fact" in message
):
return random.choice(ai_fact_replies)
elif message == "will ai replace humans":
return (
"AI may automate some tasks, but human creativity, "
"judgment, communication, and responsibility will "
"remain very important."
)
elif message == "what is machine learning":
return (
"Machine learning is a part of AI that allows computers "
"to learn patterns from data instead of following only "
"fixed instructions."
)
# Programming questions
elif message == "what is programming":
return (
"Programming is the process of giving instructions to "
"a computer so it can perform specific tasks."
)
elif message == "how can I become a programmer":
return (
"Start with one language, learn the fundamentals, "
"practise regularly, and build small projects."
)
elif (
"give me a programming tip" in message
or "give me a coding tip" in message
or message == "give me a tip"
):
return random.choice(programming_tip_replies)
elif message == "what is a variable":
return (
"A variable is a named place used to store "
"information in a program."
)
elif message == "what is a loop":
return (
"A loop repeats a block of code until a condition "
"is met or for a fixed number of times."
)
elif message == "what is a function":
return (
"A function is a reusable block of code created "
"to perform a particular task."
)
# CodeAlpha and Internship project questions
elif message == "tell me about codealpha":
return (
"CodeAlpha provides internship opportunities where "
"students complete practical projects and improve "
"their technical skills."
)
elif message in [
"tell me about your project",
"what is your project"
]:
return (
"This is a rule-based Python chatbot developed for "
"the CodeAlpha Python Programming Internship."
)
elif message == "what skills does this project use":
return (
"This project uses Python functions, loops, conditions, "
"lists, strings, user input, keyword matching, and "
"the random module."
)
elif message == "why are projects important":
return (
"Projects help you apply theoretical knowledge, improve "
"problem-solving skills, and demonstrate your abilities."
)
# Jokes
elif "joke" in words:
return random.choice(joke_replies)
# General fun facts
elif message in [
"tell me a fun fact",
"fun fact",
"tell me something interesting"
]:
return random.choice(general_fact_replies)
# Motivation
# Motivation
elif (
message in ["motivate me", "inspire me", "give me motivation"]
or "motivate" in message
or "motivation" in message
):
return random.choice(motivation_replies)
elif message in [
"i feel discouraged",
"i am discouraged",
"i feel sad cheer me up"
]:
return (
"Learning something new can be difficult, but struggling "
"is part of the process. Take one small step and continue."
)
elif message in [
"i made an error",
"my code has an error"
]:
return (
"Errors are a normal part of programming. Read the "
"message carefully, check the relevant line, and try again."
)
# Thank-you messages
elif (
message in [
"thank you",
"thanks",
"thankyou",
"that was helpful"
]
or "thank" in words
):
return random.choice(thanks_replies)
# Exit messages
elif message in exit_commands:
return random.choice(goodbye_replies)
# Unknown question
else:
return random.choice(unknown_replies)
# Clear the terminal before displaying PyBot
os.system("cls")
# --------------------------------------------------
# WELCOME SCREEN
# --------------------------------------------------
print(" ==================================================")
print(" WELCOME TO PyBot")
print(" Your Smart Python Companion")
print(" ==================================================")
print()
print("Hello! I am PyBot.")
print("I can answer questions about Python, AI, and programming.")
print("You can also ask me for jokes, fun facts, and motivation.")
print("Type 'help' to see suggested questions.")
print("Type 'bye' when you want to end the conversation.")
print(" ====================================================")
# --------------------------------------------------
# MAIN CHAT LOOP
# --------------------------------------------------
message_count = 0
while True:
user_message = input("\nYou: ")
# Clean the user's message
cleaned_message = clean_message(user_message)
# Check whether the user entered nothing
if cleaned_message == "":
print(f"{BOT_NAME}: Please type something!")
continue
# Count every valid message
message_count = message_count + 1
# Show the help menu
if cleaned_message in [
"i cant understand i need help",
"commands",
"what can i ask"
]:
show_help()
else:
# Get PyBot's reply
bot_reply = get_reply(cleaned_message)
# Display PyBot's reply
print(f"{BOT_NAME}: {bot_reply}")
# Close the chatbot
if cleaned_message in exit_commands:
print(
f"\nYou and {BOT_NAME} had "
f"{message_count} message exchange(s)."
)
print(f"Thank you for using {BOT_NAME}!")
print("=" * 58)
break
# Display a suggestion after every five messages
if message_count % 5 == 0:
print(
f"\nTip: {random.choice(suggestion_tips)}"
)