-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
108 lines (73 loc) · 2.75 KB
/
Copy pathchatbot.py
File metadata and controls
108 lines (73 loc) · 2.75 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
"""Rule-Based AI Chatbot
DecodeLabs Artificial Intelligence - Project 1
"""
BOT_NAME = "Nova"
EXIT_COMMANDS = {
"bye",
"exit",
"quit",
"goodbye",
}
FALLBACK_RESPONSE = (
"I don't understand that yet. Type 'help' to see what you can ask."
)
RESPONSES = {
"hello": "Hello! How can I help you today?",
"hi": "Hi there! What would you like to talk about?",
"hey": "Hey! Nice to meet you.",
"how are you":
"I'm running perfectly, thanks for asking. How are you?",
"what is your name":
f"My name is {BOT_NAME}. I'm a rule-based chatbot.",
"who are you":
f"I'm {BOT_NAME}, a chatbot powered by predefined rules.",
"what can you do":
"I can respond to greetings, introduce myself, explain this "
"project, answer predefined questions, and end the conversation.",
"what is artificial intelligence":
"Artificial intelligence is the field of building systems that "
"perform tasks that normally require human intelligence.",
"what is a rule based chatbot":
"A rule-based chatbot matches a user's input with predefined "
"rules and returns the response linked to the matching rule.",
"tell me about this project":
"This project demonstrates input sanitization, control flow, "
"dictionary lookup, fallback handling, and a continuous loop.",
"help":
"Try: hello, how are you, what is your name, what can you do, "
"what is artificial intelligence, what is a rule based chatbot, "
"tell me about this project, thanks, or exit.",
"thanks":
"You're welcome!",
"thank you":
"You're welcome. I'm glad I could help.",
}
def normalize_input(raw_input):
"""Convert input to lowercase and remove unnecessary spaces."""
return " ".join(raw_input.lower().strip().split())
def get_response(raw_input):
"""Generate a response for the given user input."""
clean_input = normalize_input(raw_input)
if not clean_input:
return "Please type a message so I can respond.", False
if clean_input in EXIT_COMMANDS:
return "Goodbye! Thanks for chatting with me.", True
response = RESPONSES.get(
clean_input,
FALLBACK_RESPONSE
)
return response, False
def run_chatbot():
"""Run the chatbot continuously until the user exits."""
print("=" * 55)
print(f"{BOT_NAME}: Hello! I'm {BOT_NAME}, your rule-based AI chatbot.")
print(f"{BOT_NAME}: Type 'help' for options or 'exit' to stop.")
print("=" * 55)
while True:
user_input = input("You: ")
response, should_exit = get_response(user_input)
print(f"{BOT_NAME}: {response}")
if should_exit:
break
if __name__ == "__main__":
run_chatbot()