-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQ1-ELIZA-DRAFT-JUUSH.py
32 lines (28 loc) · 1.06 KB
/
Q1-ELIZA-DRAFT-JUUSH.py
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
import re
import random
# Dictionary of patterns and corresponding responses
patterns = {
r'(.*)\bhello\b(.*)': ['Hello!', 'Hi there!', 'Greetings!'],
r'(.*)\bbye\b(.*)': ['Goodbye!', 'Farewell!', 'Take care!'],
r'(.*)\b(?:i\'?m|i am)\s(.*)': ['How do you do, %2?', 'Nice to meet you, %2!'],
# Add more patterns and responses here
}
def eliza_chatbot():
print("ELIZA: Hello, how can I help you today?")
while True:
user_input = input("User: ")
if user_input.lower() == 'bye':
print("ELIZA: Goodbye!")
break
else:
response = generate_response(user_input)
print("ELIZA:", response)
def generate_response(user_input):
for pattern, responses in patterns.items():
match = re.match(pattern, user_input.lower())
if match:
response = random.choice(responses)
return re.sub(r'%(\d)', lambda m: match.group(int(m.group(1))), response)
return "I'm sorry, I don't understand. Can you please rephrase?"
# Start the conversation with ELIZA
eliza_chatbot()