Created
February 22, 2024 03:57
-
-
Save joao141141/a938d4652bb29b2811cea62e1e7a21a8 to your computer and use it in GitHub Desktop.
EUREKA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import random | |
class AlienBot: | |
def __init__(self): | |
self.name = None | |
self.alienbabble = { | |
'describe_planet_intent': r'.*(how|tell.*about|interest.*in).*your.*planet', | |
'answer_why_intent': r'why are you.*(here|asking me?\s?so?\s?many questions)\??', | |
'cubed_intent': r'can.*you.*cube.*number (\d+)?', | |
'alien_interest_intent': r'what.*about.*you\??' | |
} | |
self.responses = { | |
'describe_planet_intent': [ | |
"My planet is a utopia of diverse organisms and species.", | |
"I am from Opidipus, the capital of the Wayward Galaxies." | |
], | |
'answer_why_intent': [ | |
"I come in peace.", | |
"I am here to collect data on your planet and its inhabitants.", | |
"I heard the coffee is good." | |
], | |
'alien_interest_intent': [ | |
"I don't like talking about myself", | |
"I'd like to keep that to me.", | |
"Sorry, you are asking for too much information." | |
] | |
} | |
self.exit_commands = ["exit", "quit", "bye"] | |
self.random_questions = [ | |
"Tell me about your planet.", | |
"Why are you here?", | |
"Can you cube a number for me?", | |
"What about you?" | |
] | |
def greet(self): | |
self.name = input("Hello stranger, what is your name? ").lower() | |
will_help = input(f"Hi {self.name}, I'm Etcetera. I'm not from this planet. Will you help me learn about this planet? ").lower() | |
if will_help in ["no", "n", "nah", "nope"]: | |
print("Ok, have a nice Earth day!") | |
else: | |
self.chat() | |
def make_exit(self, reply): | |
if reply.strip().lower() in self.exit_commands: | |
print("Ok, have a nice day!") | |
return True | |
return False | |
def chat(self): | |
reply = input(random.choice(self.random_questions)).lower() | |
while not self.make_exit(reply): | |
reply = input(self.match_reply(reply)).lower() | |
def match_reply(self, reply): | |
for intent, regex_pattern in self.alienbabble.items(): | |
found_match = re.match(regex_pattern, reply) | |
if found_match: | |
if intent == 'cubed_intent': | |
return self.cubed_intent(found_match.group(1)) | |
else: | |
return random.choice(self.responses.get(intent)) | |
return self.no_match_intent() | |
def cubed_intent(self, number): | |
if number: | |
number = int(number) | |
return f"The cube of {number} is {number**3}. Isn't that cool?" | |
else: | |
return "Please provide a number to cube." | |
def no_match_intent(self): | |
responses = ["Please, tell me more.", "Tell me more!", "Why do you say that?"] | |
return random.choice(responses) | |
# Create an instance of AlienBot | |
Alienbot = AlienBot() | |
Alienbot.greet() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment