Added simple chat bot
This commit is contained in:
BIN
repo_chat_bots/.DS_Store
vendored
Normal file
BIN
repo_chat_bots/.DS_Store
vendored
Normal file
Binary file not shown.
211
repo_chat_bots/OOP_Chatbot.html
Normal file
211
repo_chat_bots/OOP_Chatbot.html
Normal file
@@ -0,0 +1,211 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>OOP ChatBot</title>
|
||||
<style>
|
||||
#chatbox {
|
||||
border: 1px solid #ccc;
|
||||
height: 300px;
|
||||
overflow-y: scroll;
|
||||
padding: 10px;
|
||||
margin-bottom: 10px;
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
.user {
|
||||
text-align: right;
|
||||
color: blue;
|
||||
margin: 5px 0;
|
||||
}
|
||||
.bot {
|
||||
text-align: left;
|
||||
color: green;
|
||||
margin: 5px 0;
|
||||
/* }
|
||||
.input-area {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
#userInput {
|
||||
flex: 1;
|
||||
padding: 8px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
}
|
||||
button {
|
||||
padding: 8px 16px;
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
button:hover {
|
||||
background-color: #0056b3;
|
||||
}*/
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h2>OOP ChatBot</h2>
|
||||
<div id="chatbox"></div>
|
||||
<div class="input-area">
|
||||
<input type="text" id="userInput" placeholder="Ask me something...">
|
||||
<button id="sendButton">Send</button>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Message Class - Represents a single message
|
||||
class Message {
|
||||
constructor(sender, text = new Date()) {
|
||||
this.sender = sender; // 'user' or 'bot'
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
getFormattedText() {
|
||||
return `${this.sender.charAt(0).toUpperCase() + this.sender.slice(1)}: ${this.text}`;
|
||||
}
|
||||
}
|
||||
|
||||
// MessageProcessor Class - Handles bot response logic
|
||||
class MessageProcessor {
|
||||
constructor() {
|
||||
this.rules = [
|
||||
{
|
||||
pattern: (input) => input.includes("hello") || input.includes("hi"),
|
||||
response: "Hello! How can I help you?"
|
||||
},
|
||||
{
|
||||
pattern: (input) => input.includes("bye") || input.includes("goodbye"),
|
||||
response: "Goodbye! Have a great day!"
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
processInput(input) {
|
||||
const cleanInput = input.toLowerCase().trim();
|
||||
|
||||
if (!cleanInput) {
|
||||
return "Please type something!";
|
||||
}
|
||||
|
||||
// Find the first matching rule
|
||||
const matchingRule = this.rules.find(rule => rule.pattern(cleanInput));
|
||||
|
||||
return matchingRule ? matchingRule.response : "I'm not sure how to answer that yet.";
|
||||
}
|
||||
|
||||
addRule(pattern, response) {
|
||||
this.rules.push({ pattern, response });
|
||||
}
|
||||
}
|
||||
|
||||
// ChatInterface Class - Manages UI interactions
|
||||
class ChatInterface {
|
||||
constructor() {
|
||||
this.chatbox = document.getElementById('chatbox');
|
||||
this.userInput = document.getElementById('userInput');
|
||||
this.sendButton = document.getElementById('sendButton');
|
||||
}
|
||||
|
||||
displayMessage(message) {
|
||||
const messageElement = document.createElement('div');
|
||||
messageElement.classList.add(message.sender);
|
||||
messageElement.textContent = message.getFormattedText();
|
||||
|
||||
this.chatbox.appendChild(messageElement);
|
||||
this.chatbox.scrollTop = this.chatbox.scrollHeight;
|
||||
}
|
||||
|
||||
getInputText() {
|
||||
return this.userInput.value;
|
||||
}
|
||||
|
||||
clearInput() {
|
||||
this.userInput.value = '';
|
||||
}
|
||||
|
||||
setupEventListeners(onSendMessage) {
|
||||
this.sendButton.addEventListener('click', onSendMessage);
|
||||
|
||||
this.userInput.addEventListener('keypress', (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
onSendMessage();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Main ChatBot Application Class
|
||||
class ChatBotApp {
|
||||
constructor() {
|
||||
this.interface = new ChatInterface();
|
||||
this.processor = new MessageProcessor();
|
||||
this.messageHistory = [];
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
init() {
|
||||
// Set up event listeners with bound context
|
||||
this.interface.setupEventListeners(() => this.handleUserMessage());
|
||||
|
||||
// Welcome message
|
||||
const welcomeMessage = new Message('bot', "Welcome! I'm your rule-based chatbot. Try saying 'hello'!");
|
||||
this.interface.displayMessage(welcomeMessage);
|
||||
this.messageHistory.push(welcomeMessage);
|
||||
}
|
||||
|
||||
handleUserMessage() {
|
||||
const userText = this.interface.getInputText();
|
||||
|
||||
if (!userText.trim()) return;
|
||||
|
||||
// Create and display user message
|
||||
const userMessage = new Message('user', userText);
|
||||
this.interface.displayMessage(userMessage);
|
||||
this.messageHistory.push(userMessage);
|
||||
|
||||
// Process and display bot response
|
||||
const botResponse = this.processor.processInput(userText);
|
||||
const botMessage = new Message('bot', botResponse);
|
||||
|
||||
// Small delay for more natural conversation flow
|
||||
setTimeout(() => {
|
||||
this.interface.displayMessage(botMessage);
|
||||
this.messageHistory.push(botMessage);
|
||||
}, 500);
|
||||
|
||||
this.interface.clearInput();
|
||||
}
|
||||
|
||||
// Public method to add custom rules
|
||||
addCustomRule(pattern, response) {
|
||||
this.processor.addRule(pattern, response);
|
||||
}
|
||||
|
||||
// Public method to get conversation history
|
||||
getConversationHistory() {
|
||||
return [...this.messageHistory];
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the chatbot when the page loads
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const chatBot = new ChatBotApp();
|
||||
|
||||
// Example of adding custom rules dynamically
|
||||
chatBot.addCustomRule(
|
||||
(input) => input.includes("weather"),
|
||||
"I don't have access to weather data, but I hope it's nice where you are!"
|
||||
);
|
||||
|
||||
chatBot.addCustomRule(
|
||||
(input) => input.includes("help"),
|
||||
"I can respond to greetings, questions about how I am, farewells, and more! Just try talking to me."
|
||||
);
|
||||
|
||||
// Make chatbot globally available for testing in console
|
||||
window.chatBot = chatBot;
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
155
repo_chat_bots/Simple-Chatbot_v2.0.html
Normal file
155
repo_chat_bots/Simple-Chatbot_v2.0.html
Normal file
@@ -0,0 +1,155 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Simple Chatbot</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
#chatbox {
|
||||
border: 2px solid #4CAF50;
|
||||
height: 300px;
|
||||
overflow-y: auto;
|
||||
padding: 15px;
|
||||
margin-bottom: 15px;
|
||||
border-radius: 10px;
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
|
||||
.user {
|
||||
text-align: right;
|
||||
color: #2196F3;
|
||||
margin: 10px 0;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.bot {
|
||||
text-align: left;
|
||||
color: #4CAF50;
|
||||
margin: 10px 0;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
#userInput {
|
||||
width: 70%;
|
||||
padding: 10px;
|
||||
border: 2px solid #4CAF50;
|
||||
border-radius: 5px;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 10px 20px;
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #333;
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Simple Chatbot</h1>
|
||||
|
||||
<div id="chatbox"></div>
|
||||
|
||||
<input type="text" id="userInput" placeholder="Type your message here...">
|
||||
<button onclick="sendMessage()">Send</button>
|
||||
|
||||
<script>
|
||||
// Get the chatbox and input elements
|
||||
const chatbox = document.getElementById('chatbox');
|
||||
const userInput = document.getElementById('userInput');
|
||||
|
||||
// Function to add a message to the chatbox
|
||||
function addMessage(sender, text) {
|
||||
// Create a new div for the message
|
||||
const messageDiv = document.createElement('div');
|
||||
|
||||
// Add CSS class based on sender (user or bot)
|
||||
messageDiv.className = sender;
|
||||
|
||||
// Set the message text
|
||||
messageDiv.textContent = text;
|
||||
|
||||
// Add the message to the chatbox
|
||||
chatbox.appendChild(messageDiv);
|
||||
|
||||
// Scroll to the bottom to show the latest message
|
||||
chatbox.scrollTop = chatbox.scrollHeight;
|
||||
}
|
||||
|
||||
// Function to generate bot responses
|
||||
function getBotResponse(userMessage) {
|
||||
// Convert to lowercase for easier matching
|
||||
const message = userMessage.toLowerCase();
|
||||
|
||||
// Check what the user said and respond accordingly
|
||||
if (message.includes('hello') || message.includes('hi')) {
|
||||
return "Hello! How can I help you today?";
|
||||
} else if (message.includes('how are you')) {
|
||||
return "I'm doing great, thanks for asking!";
|
||||
} else if (message.includes('bye') || message.includes('goodbye')) {
|
||||
return "Goodbye! Have a wonderful day!";
|
||||
} else if (message.includes('name')) {
|
||||
return "I'm a simple chatbot. You can call me ChatBot!";
|
||||
} else if (message.includes('help')) {
|
||||
return "I can respond to greetings, questions about how I am, and farewells. Try saying 'hello'!";
|
||||
} else {
|
||||
return "I'm not sure how to respond to that. Try asking something else!";
|
||||
}
|
||||
}
|
||||
|
||||
// Function to handle sending messages
|
||||
function sendMessage() {
|
||||
// Get the user's message
|
||||
const message = userInput.value;
|
||||
|
||||
// Don't send empty messages
|
||||
if (message.trim() === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add user message to chat
|
||||
addMessage('user', 'You: ' + message);
|
||||
|
||||
// Get and add bot response
|
||||
const botReply = getBotResponse(message);
|
||||
addMessage('bot', 'Bot: ' + botReply);
|
||||
|
||||
// Clear the input field
|
||||
userInput.value = '';
|
||||
|
||||
// Focus back on the input for the next message
|
||||
userInput.focus();
|
||||
}
|
||||
|
||||
// Allow sending messages by pressing Enter key
|
||||
userInput.addEventListener('keypress', function(event) {
|
||||
if (event.key === 'Enter') {
|
||||
sendMessage();
|
||||
}
|
||||
});
|
||||
|
||||
// Add a welcome message when the page loads
|
||||
window.onload = function() {
|
||||
addMessage('bot', 'Bot: Hello! I am a simple chatbot. Try saying "hello" or "how are you?"');
|
||||
};
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
206
repo_chat_bots/rule_base_bot.py
Normal file
206
repo_chat_bots/rule_base_bot.py
Normal file
@@ -0,0 +1,206 @@
|
||||
# -*- 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()
|
||||
BIN
repo_chat_bots/simple_bot/.DS_Store
vendored
Normal file
BIN
repo_chat_bots/simple_bot/.DS_Store
vendored
Normal file
Binary file not shown.
60
repo_chat_bots/simple_bot/simple_rule_based_bot.html
Normal file
60
repo_chat_bots/simple_bot/simple_rule_based_bot.html
Normal file
@@ -0,0 +1,60 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Simple Rule-Based Bot</title>
|
||||
<!--<link rel=
|
||||
"stylesheet" href="style.css">-->
|
||||
<style>
|
||||
#chatbox { border: 1px solid #ccc; height: 300px; overflow-y: scroll; padding: 10px; }
|
||||
.user { text-align: right; color: blue; }
|
||||
.bot { text-align: left; color: green; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="chatbox"></div>
|
||||
<input type="text" id="userInput" placeholder="Ask me something...">
|
||||
<button onclick="sendMessage()">Send</button>
|
||||
|
||||
<script>
|
||||
const chatbox = document.getElementById('chatbox');
|
||||
const userInput = document.getElementById('userInput');
|
||||
|
||||
function addMessage(sender, text) {
|
||||
const messageElement = document.createElement('div');
|
||||
messageElement.classList.add(sender);
|
||||
messageElement.textContent = text;
|
||||
chatbox.appendChild(messageElement);
|
||||
chatbox.scrollTop = chatbox.scrollHeight;
|
||||
}
|
||||
|
||||
function getBotResponse(input) {
|
||||
input = input.toLowerCase();
|
||||
if (input.includes("hello") || input.includes("hi")) {
|
||||
return "Hello! How can I help you?";
|
||||
} else if (input.includes("how are you")) {
|
||||
return "I'm just a bot, but I'm functioning perfectly!";
|
||||
} else if (input.includes("bye")) {
|
||||
return "Goodbye! Have a great day!";
|
||||
} else {
|
||||
return "I'm not sure how to answer that yet.";
|
||||
}
|
||||
}
|
||||
|
||||
function sendMessage() {
|
||||
const userMessage = userInput.value;
|
||||
if (userMessage.trim() === '') return;
|
||||
|
||||
addMessage('user', `You: ${userMessage}`);
|
||||
const botResponse = getBotResponse(userMessage);
|
||||
addMessage('bot', `Bot: ${botResponse}`);
|
||||
|
||||
userInput.value = '';
|
||||
}
|
||||
//userInput.addEventListener('keypress', function (e) {
|
||||
// if (e.key === 'Enter') {
|
||||
// sendMessage();
|
||||
// }
|
||||
//});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user