-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
67 lines (50 loc) · 2.33 KB
/
Copy pathchatbot.py
File metadata and controls
67 lines (50 loc) · 2.33 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
import re
# 1. Chatbot Knowledge Base (Structured Responses)
# Key: Regular Expression pattern
# Value: Corresponding response
KNOWLEDGE_BASE = {
# Greeting / Start
r"^(hi|hello|hey|hola)[\s!.]*$": "Hello there! How may I assist you today?",
# Status / Well-being
r"how are you|how's it going": "I am an AI, so I don't have feelings, but I'm functioning perfectly! Thanks for asking!",
# Identity / Name
r"your name|who are you": "I am a simple **Pattern-Matching Chatbot**, created using Python for the CodeAlpha task.",
# Capability / Function
r"what can you do|help me": "I can answer basic questions and chat with you. Try asking me about my name!",
# Gratitude
r"thank you|thanks": "You're most welcome! Is there anything else I can help with?",
# Exit / End
r"^(bye|exit|quit|stop)[\s!.]*$": "Goodbye! It was great chatting with you. Come back soon!"
}
def get_chatbot_response(user_input):
"""
Checks user input against defined patterns using Regular Expressions.
"""
# Convert input to lowercase for case-insensitive matching
user_input = user_input.lower()
# Iterate through the knowledge base
for pattern, response in KNOWLEDGE_BASE.items():
# Check if the user input matches the regular expression pattern
# re.search looks for the pattern anywhere in the string
if re.search(pattern, user_input):
return response
# Default fallback response if no pattern matches
return "Hmm, I couldn't quite understand that. Could you rephrase your question?"
def main_chat_loop():
"""Main loop for the interactive chat session."""
print("--- 👋 Welcome to the Pattern-Matching Chatbot! ---")
print("Ask me anything simple, or type 'bye' to exit.")
print("-" * 45)
while True:
user_message = input("You: ")
# Check for exit commands explicitly before processing
if re.search(r"^(bye|exit|quit|stop)", user_message.lower()):
# The response will be handled by the KNOWLEDGE_BASE lookup,
# but we use the input here to trigger the loop break.
response = get_chatbot_response(user_message)
print("Bot:", response)
break
response = get_chatbot_response(user_message)
print("Bot:", response)
if __name__ == "__main__":
main_chat_loop()