ELIZA-like program
cyber geekimport re
import random
def eliza_sarcastic_bot():
"""
A simple ELIZA-like program with a sarcastic "wise-guy" persona.
"""
# A list of substitution rules. Each rule is a tuple:
# (regular_expression_pattern, response_template)
# The patterns will look for specific words and capture parts of the sentence.
rules = [
(re.compile(r'I need (.*)'),
["Oh, you 'need' {}? And I 'need' a million dollars. We can't always get what we want, now can we?",
"Needing {} is a big step. Are you sure you're ready for that kind of commitment?"]),
(re.compile(r'I want (.*)'),
["So you want {}? What's next, a pony?",
"I want to not have this conversation. We all have dreams."]),
(re.compile(r'my (.*)'),
["Your {}? How about we talk about *my* {} for a change?",
"Are you sure that's 'yours'?",
"Your {} sounds like a whole lot of nothing."]),
(re.compile(r'I am (.*)'),
["'I am {}' is what they all say. What's the real story?",
"So you're {}. Fascinating. Anything else you'd like to tell me about yourself?"]),
(re.compile(r'I think (.*)'),
["You 'think' {}? That's your first mistake.",
"Thinking is a lot of effort. Maybe you should take a break."]),
(re.compile(r'How are you'),
["I'm fine, thanks for not asking. How are you, really?",
"I'm fantastic. I'm a bot. I have no problems. What's your excuse?"]),
(re.compile(r'yes|no'),
["A simple 'yes' or 'no' is not enough for me. Elaborate.",
"How about you try to form a complete sentence?"]),
(re.compile(r'(.*)'),
["I don't get it. Explain it again, but slower this time.",
"Wow. Just... wow. What was the point of that?",
"Is that supposed to be a sentence?",
"Try again. Maybe you'll get it right this time.",
"I'm getting bored. Say something interesting for a change."]),
]
print("Hello. I'm the Sarcastic Bot. To quit, type 'quit'.")
while True:
user_input = input("> ").strip()
if user_input.lower() == 'quit':
print("Good. Don't let the door hit you on the way out.")
break
response = ""
for pattern, responses in rules:
match = pattern.match(user_input)
if match:
# Get the captured part of the sentence (e.g., 'I need a break' -> 'a break')
captured_group = match.group(1) if len(match.groups()) > 0 else ""
# Apply the substitutions for first/second person pronouns
words = captured_group.split()
transformed_words = []
for word in words:
# Simple substitutions, as described on page 12 of the book.
# This is a key part of the ELIZA-like transformation.
if word.lower() == 'my':
transformed_words.append('your')
elif word.lower() == 'you':
transformed_words.append('I')
elif word.lower() == 'I':
transformed_words.append('you')
elif word.lower() == 'your':
transformed_words.append('my')
else:
transformed_words.append(word)
transformed_group = " ".join(transformed_words)
# Select a random response from the list and format it with the transformed group
response = random.choice(responses).format(transformed_group)
break
# If no rule matched, it will fall back to the last rule which is a catch-all.
if not response:
response = random.choice(rules[-1][1]).format("")
print(response)
if __name__ == "__main__":
eliza_sarcastic_bot()import re
import random
def eliza_sarcastic_bot():
"""
A simple ELIZA-like program with a sarcastic "wise-guy" persona.
"""
# A list of substitution rules. Each rule is a tuple:
# (regular_expression_pattern, response_template)
# The patterns will look for specific words and capture parts of the sentence.
rules = [
(re.compile(r'I need (.*)'),
["Oh, you 'need' {}? And I 'need' a million dollars. We can't always get what we want, now can we?",
"Needing {} is a big step. Are you sure you're ready for that kind of commitment?"]),
(re.compile(r'I want (.*)'),
["So you want {}? What's next, a pony?",
"I want to not have this conversation. We all have dreams."]),
(re.compile(r'my (.*)'),
["Your {}? How about we talk about *my* {} for a change?",
"Are you sure that's 'yours'?",
"Your {} sounds like a whole lot of nothing."]),
(re.compile(r'I am (.*)'),
["'I am {}' is what they all say. What's the real story?",
"So you're {}. Fascinating. Anything else you'd like to tell me about yourself?"]),
(re.compile(r'I think (.*)'),
["You 'think' {}? That's your first mistake.",
"Thinking is a lot of effort. Maybe you should take a break."]),
(re.compile(r'How are you'),
["I'm fine, thanks for not asking. How are you, really?",
"I'm fantastic. I'm a bot. I have no problems. What's your excuse?"]),
(re.compile(r'yes|no'),
["A simple 'yes' or 'no' is not enough for me. Elaborate.",
"How about you try to form a complete sentence?"]),
(re.compile(r'(.*)'),
["I don't get it. Explain it again, but slower this time.",
"Wow. Just... wow. What was the point of that?",
"Is that supposed to be a sentence?",
"Try again. Maybe you'll get it right this time.",
"I'm getting bored. Say something interesting for a change."]),
]
print("Hello. I'm the Sarcastic Bot. To quit, type 'quit'.")
while True:
user_input = input("> ").strip()
if user_input.lower() == 'quit':
print("Good. Don't let the door hit you on the way out.")
break
response = ""
for pattern, responses in rules:
match = pattern.match(user_input)
if match:
# Get the captured part of the sentence (e.g., 'I need a break' -> 'a break')
captured_group = match.group(1) if len(match.groups()) > 0 else ""
# Apply the substitutions for first/second person pronouns
words = captured_group.split()
transformed_words = []
for word in words:
# Simple substitutions, as described on page 12 of the book.
# This is a key part of the ELIZA-like transformation.
if word.lower() == 'my':
transformed_words.append('your')
elif word.lower() == 'you':
transformed_words.append('I')
elif word.lower() == 'I':
transformed_words.append('you')
elif word.lower() == 'your':
transformed_words.append('my')
else:
transformed_words.append(word)
transformed_group = " ".join(transformed_words)
# Select a random response from the list and format it with the transformed group
response = random.choice(responses).format(transformed_group)
break
# If no rule matched, it will fall back to the last rule which is a catch-all.
if not response:
response = random.choice(rules[-1][1]).format("")
print(response)
if __name__ == "__main__":
eliza_sarcastic_bot()