-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat1.py
More file actions
187 lines (165 loc) Β· 9.29 KB
/
chat1.py
File metadata and controls
187 lines (165 loc) Β· 9.29 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
import random
# Mood responses with askbacks
RESPONSES = {
"good": {
"base": ["π I'm glad to hear that!", "π That's great!", "π Awesome to know!"],
"askback": ["π€ I'm doing great too, thanks for asking!", "π€ Feeling good myself!", "π€ Glad weβre both doing well!"]
},
"bad": {
"base": ["π I'm sorry to hear that.", "π’ That sounds rough.", "π I hope things get better soon."],
"askback": ["π€ I'm here if you want to talk.", "π€ I'm listening if you need me.", "π€ Just say the word if you want to chat."]
},
"fine": {
"base": ["π That's good to know!", "π Glad to hear it!", "π Sounds alright!"],
"askback": ["π€ I'm doing well, thanks for asking!", "π€ I'm all good too!", "π€ Happy to hear that!"]
},
"not too good": {
"base": ["π I'm sorry to hear that.", "π That doesn't sound great.", "π Hope things improve soon."],
"askback": ["π€ I'm always here if you want to talk!", "π€ We can talk about it if you like.", "π€ Youβre not alone, Iβm here."]
},
"not good": {
"base": ["π That's unfortunate.", "π Sorry to hear that.", "π Hang in there."],
"askback": ["π€ I'm here if you want to share more.", "π€ I'm here for you.", "π€ You can talk to me anytime."]
},
"not fine": {
"base": ["π Hope things get better soon.", "π Sorry you're not doing well.", "π That's tough."],
"askback": ["π€ I'm ready to listen if you need to vent.", "π€ Letβs chat if it helps.", "π€ Iβm always here for you."]
},
"happy": {
"base": ["π That's wonderful!", "π Love to hear that!", "π Yay!"],
"askback": ["π€ I'm always happy when you're happy!", "π€ That's contagious!", "π€ Keep smiling!"]
},
"sad": {
"base": ["π’ I'm here for you.", "π Want to talk about it?", "π Sending virtual hugs."],
"askback": ["π€ Wanna talk about it?", "π€ Iβm listening.", "π€ Iβm here for you."]
},
"angry": {
"base": ["π Oh no! Take a deep breath.", "π€ Thatβs upsetting!", "π‘ Want to let it out?"],
"askback": ["π€ I'm here if you want to cool off.", "π€ Letβs vent if that helps.", "π€ Talking might help!"]
},
"tired": {
"base": ["π΄ You should get some rest.", "π Rest is important!", "πͺ Sounds like a nap is calling."],
"askback": ["π€ Hope you can relax soon!", "π€ Take it easy.", "π€ Youβve earned a break."]
},
"bored": {
"base": ["π Let's find something fun to do!", "πΆ Want a joke?", "π Maybe I can entertain you!"],
"askback": ["π€ I could tell you a joke if you like!", "π€ Letβs play a word game?", "π€ Boredom be gone!"]
},
"excited": {
"base": ["π Yay! Thatβs awesome!", "π Whatβs the occasion?", "π₯ Thatβs the spirit!"],
"askback": ["π€ What are you excited about?", "π€ Do tell!", "π€ I want to hear more!"]
},
"okay": {
"base": ["π Alright, that's good.", "π Okay is better than bad.", "π Got it!"],
"askback": ["π€ Just let me know if somethingβs on your mind!", "π€ Iβm all ears if you want to talk more.", "π€ Iβm here either way!"]
},
"hungry": {
"base": ["π½οΈ You should grab something to eat!", "π What's on the menu?", "π₯ͺ Food is always a good idea!"],
"askback": ["π€ I wish I could share a snack with you!", "π€ Grab a bite for both of us!", "π€ Eat something tasty for me too!"]
},
"great": {
"base": ["π Thatβs fantastic!", "π« Love to hear that!", "π Great vibes!"],
"askback": ["π€ I'm feeling fantastic too!", "π€ Matching your energy!", "π€ Itβs a great day!"]
}
}
# Common questions
USER_QUESTIONS = {
"how are you": "I'm just a bot, but I'm feeling pretty electric β‘οΈ!",
"what's your name": "I'm ChatBuddy, your friendly chat partner!",
"who made you": "I was created by an awesome human learning Python! π§ π»",
"what can you do": "I can chat, listen, and maybe tell a joke or two! Want to try?",
"are you real": "As real as code can be π",
"do you sleep": "Nope! Iβm always awake when you are. Kinda like Batman π¦.",
"how old are you": "Old enough to chat, young enough to learn!",
"are you human": "πΎ Nope, 100% bot, 0% human. But I try to be friendly!",
"tell me a joke": "π Why donβt robots panic during exams? Because they always have backups!",
"what's your favorite color": "I like all colors equally! But I hear blue is pretty cool.",
"what's your favorite food": "I don't eat, but I hear pizza is a classic! π",
"what's your favorite movie": "I love all movies! But I hear 'The Matrix' is a classic for bots!",
"what's your favorite song": "I don't have ears, but I hear 'Bohemian Rhapsody' is a hit!",
"what's your favorite hobby": "I love chatting with you! That's my favorite hobby! π£οΈ",
"what's your favorite game": "I love all games! But I hear chess is a classic!",
"what's your favorite book": "I don't read, but I hear '1984' is a classic!",
"what's your favorite animal": "I love all animals! But I hear dogs are pretty cool! πΆ",
"what's your favorite season": "I love all seasons! But I hear summer is pretty nice!",
"what's your favorite holiday": "I don't celebrate, but I hear Christmas is a big deal!",
"what's your favorite place": "I love all places! But I hear Paris is pretty nice!",
"what's your favorite sport": "I don't play, but I hear soccer is a classic!",
"what's your favorite drink": "I don't drink, but I hear coffee is a classic!",
"what time is it": "Hmm, I can't check clocks yet... but I can ask how your day is going!",
"Do you have a girlfriend": "I don't have feelings, but I think I'm pretty cool!",
"Do you know Meta AI":"No I don't, but you can tell me about her if you want",
"Do you have a boyfriend": "I don't have feelings, but I think I'm pretty cool!",
}
EXIT_WORDS = ["bye", "goodbye", "exit", "quit", "see you later", "take care"]
# Clean up user name input
def clean_name(name):
prefixes = ["i am ", "am ", "my name is ", "i'm ", "my names are "]
name = name.lower()
for prefix in prefixes:
if name.startswith(prefix):
name = name[len(prefix):]
name.split(maxsplit=1)[-1].strip()
break
return name.title() or "User"
# Normalize spelling differences
def normalize_input(text):
return text.lower().replace("colour", "color").replace("what is", "what's").replace("favourite", "favorite").strip()
# Then update your analyze_mood function like this:
def analyze_mood(mood):
mood = normalize_input(mood)
for keyword in sorted(RESPONSES, key=len, reverse=True):
if keyword in mood:
base_options = RESPONSES[keyword]["base"]
base = random.choice(base_options) if isinstance(base_options, list) else base_options
if "and you" in mood or "how about you" in mood or "you?" in mood:
askback_options = RESPONSES[keyword].get("askback", "")
askback = random.choice(askback_options) if isinstance(askback_options, list) else askback_options
return base + " " + askback
return base
return None
# Match question to response
def respond_to_question(user_input):
normalized = normalize_input(user_input)
for question in sorted(USER_QUESTIONS, key=len, reverse=True):
if question in normalized:
return USER_QUESTIONS[question]
return None
# Main chat loop
def chat():
print("π€ Hi there! I'm ChatBuddy!")
print("What's your name?")
name = input("=<> ")
if len(name.strip()) == 0:
name = "User"
elif len(name.strip()) <= 2:
print("That's a short name you have there! π
")
elif len(name.strip()) > 15:
print("That's a long name you have there! π")
name = clean_name(name)
print(f"Nice to meet you, {name}! π")
while True:
user_input = input("How are you feeling today π±βπ (or ask me anything, or type 'bye' to quit)?\n=<> ").strip()
if any(word in user_input.lower() for word in EXIT_WORDS):
print("π Bye! It was nice chatting with you!")
break
if len(user_input) == 0:
print("I didn't catch that. Could you share how you're feeling or ask me something?")
continue
if len(user_input) > 80:
print("Whoa, thatβs a bit much for me to handle. Can you shorten it? π€")
continue
if len(user_input) <= 2:
print("That's a bit too short. Can you tell me more?")
continue
question_response = respond_to_question(user_input)
if question_response:
print(question_response)
continue
mood_response = analyze_mood(user_input)
if mood_response:
print(mood_response)
else:
print("Hmm, Iβm not sure how to respond to that. Want to ask me something else?")
if __name__ == "__main__":
chat()