Files
g10-m2/repo_chat_bots/rule_base_bot.py
2025-11-28 13:22:30 +03:00

206 lines
7.3 KiB
Python

# -*- coding: utf-8 -*-
"""Rule base bot.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1yZbZOlU0H2FHl_yldihvWMczyhoZ4nK-
"""
import random
import re
class RuleBot:
# Potential negative Response
negative_responses = ("no", "nope", "nah", "naw", "not a chance", "sorry")
# Exit conversation keywords
exit_commands = ("quit", "pause", "exit", "good bye", "bye", "later")
# Random starter questions
random_questions = (
"why are you here? ",
"are there many humans like you? ",
"what do you consume for sustenance? ",
"is there intelligent life on this planet? ",
"does earth have a leader? ",
"what planets have you visited? ",
"what technologies do you have on this planet? "
)
def __init__(self):
self.alienbabble = {
'describe_planet_intent': r'.*your planet.*',
'answer_why_intent': r'why\sare.*',
'about_intellipat': r'.*intellipaat.*'
}
def greet(self):
self.name = input("What is your name?\n")
will_help = input(f"Hi {self.name}, I am Rule Bot. Will you help me learn about your planet?\n")
if will_help.lower() in self.negative_responses:
print("Okay, have a nice Earth day!")
return
self.chat()
def make_exit(self, reply):
for command in self.exit_commands:
if reply.lower() == command:
print("Okay, have a nice Earth day!")
return True
return False
def chat(self):
reply = input(random.choice(self.random_questions))
while not self.make_exit(reply):
reply = input(self.match_reply(reply))
def match_reply(self, reply):
for key, value in self.alienbabble.items():
intent = key
regex_pattern = value
found_match = re.match(regex_pattern, reply, re.IGNORECASE)
if found_match and intent == 'describe_planet_intent':
return self.describe_planet_intent()
elif found_match and intent == 'answer_why_intent':
return self.answer_why_intent()
elif found_match and intent == 'about_intellipat':
return self.about_intellipat()
return self.no_match_intent()
def describe_planet_intent(self):
responses = (
"My planet is a utopia of diverse organisms and species.\n",
"I am from Opidipus, the capital of the Wayward Galaxies.\n"
)
return random.choice(responses)
def answer_why_intent(self):
responses = (
"I come in peace\n",
"I am here to collect data on your planet and its inhabitants\n",
"I heard the coffee is good\n"
)
return random.choice(responses)
def about_intellipat(self):
responses = (
"Intellipaat is the world's largest professional educational company\n",
"Intellipaat will help you learn concepts in a unique way\n",
"Intellipaat is where your career takes off\n"
)
return random.choice(responses)
def no_match_intent(self):
responses = (
"Please tell me more\n",
"Tell me more\n",
"Why do you say that?\n",
"I see. Can you elaborate?\n",
"Interesting. Can you tell me more?\n",
"I see. How do you think?\n",
"Why do you say that?\n"
)
return random.choice(responses)
AlienBot = RuleBot()
AlienBot.greet()
import random
import re
class CustomerServiceBot:
# Potential negative Response
negative_responses = ("no", "nope", "nah", "naw", "not a chance", "sorry")
# Exit conversation keywords
exit_commands = ("quit", "pause", "exit", "goodbye", "bye", "later")
# Random starter questions
random_questions = (
"How can I assist you today? ",
"What can I help you with? ",
"How can I make your day better? ",
"What do you need help with? ",
)
def __init__(self):
self.service_responses = {
'product_info': r'.*\bproduct\b.*',
'service_info': r'.*\bservice\b.*',
'support_info': r'.*\bsupport\b.*',
'pricing_info': r'.*\bprice\b.*'
}
def greet(self):
self.name = input("Welcome to TechSolutions! What is your name?\n")
will_help = input(f"Hi {self.name}, I am TechBot. How can I assist you today?\n")
if will_help.lower() in self.negative_responses:
print("Okay, have a great day!")
return
self.chat()
def make_exit(self, reply):
for command in self.exit_commands:
if reply.lower() == command:
print("Thank you for visiting TechSolutions. Have a great day!")
return True
return False
def chat(self):
reply = input(random.choice(self.random_questions))
while not self.make_exit(reply):
reply = input(self.match_reply(reply))
def match_reply(self, reply):
for key, value in self.service_responses.items():
intent = key
regex_pattern = value
found_match = re.match(regex_pattern, reply, re.IGNORECASE)
if found_match and intent == 'product_info':
return self.product_info()
elif found_match and intent == 'service_info':
return self.service_info()
elif found_match and intent == 'support_info':
return self.support_info()
elif found_match and intent == 'pricing_info':
return self.pricing_info()
return self.no_match_intent()
def product_info(self):
responses = (
"We offer a wide range of products including laptops, smartphones, and accessories.\n",
"Our latest products include the TechPro Laptop and the TechPhone 12.\n"
)
return random.choice(responses)
def service_info(self):
responses = (
"We provide various services such as tech support, product repairs, and software installations.\n",
"Our services include on-site support and remote assistance for all your tech needs.\n"
)
return random.choice(responses)
def support_info(self):
responses = (
"Our support team is available 24/7 to assist you with any issues.\n",
"You can reach our support team via phone, email, or live chat.\n"
)
return random.choice(responses)
def pricing_info(self):
responses = (
"Our pricing varies depending on the product and service. Please visit our website for detailed pricing information.\n",
"For the latest pricing on our products and services, please contact our sales team.\n"
)
return random.choice(responses)
def no_match_intent(self):
responses = (
"Can you please provide more details?\n",
"I'm not sure I understand. Can you elaborate?\n",
"Can you tell me more about what you need?\n",
"Interesting. Can you tell me more?\n",
"I see. How can I assist you further?\n",
"Why do you ask that?\n"
)
return random.choice(responses)
TechBot = CustomerServiceBot()
TechBot.greet()