enhanced chatbot

pull/915/head
Zarathushtra 9 months ago
parent aae8023ce3
commit ee84c0aad8

@ -1,26 +1,91 @@
import random import random
import string
# New Test
INTENTS = {
"greeting": ["hello", "hi", "hey"],
"question": ["why", "what", "how", "is", "are", "?"],
"opinion": ["i think", "i believe", "in my opinion"],
"goodbye": ["bye", "exit", "quit"]
}
responses ={
"greeting": [
"Hello. How can I assist you today?"
],
"question": [
"That is a thoughtful question. Could you clarify further?",
"Let us explore that in more detail."
],
"opinion": [
"That is an interesting perspective. What led you to that conclusion?"
],
"unknown": [
"I am not entirely sure I understand. Could you rephrase?",
"Could you provide more detail so I can respond appropriately?"
]
}
conversation_state = {
"last_intent": None,
"last_topic": None
}
def normalize(text):
text = text.lower()
return text.translate(str.maketrans('', '', string.punctuation))
def detect_intent(text):
for intent, patterns in INTENTS.items():
for pattern in patterns:
if pattern in text:
return intent
return "unknown"
# This list contains the random responses (you can add your own or translate them into your own language too) def extract_topic(text):
random_responses = ["That is quite interesting, please tell me more.", keywords = ["coding", "programming", "money", "career", "finance"]
"I see. Do go on.", for word in keywords:
"Why do you say that?", if word in text:
"Funny weather we've been having, isn't it?", return word
"Let's change the subject.", return None
"Did you catch the game last night?"]
print("Hello, I am Marvin, the simple robot.")
print("You can end this conversation at any time by typing 'bye'")
print("After typing each answer, press 'enter'")
print("How are you today?")
def generate_response(intent, topic):
base_response = random.choice(responses.get(intent, responses["unknown"]))
if topic:
return f"{base_response} You mentioned {topic}, Would you like to discuss it further?"
return base_response
print("Hello, I am Paskal Chatbot, the conversational AI.")
print("Type 'bye' to end the conversation.")
while True: while True:
# wait for the user to enter some text
user_input = input("> ") user_input = input("> ")
if user_input.lower() == "bye": normalized_input = normalize(user_input)
# if they typed in 'bye' (or even BYE, ByE, byE etc.), break out of the loop if normalized_input == "bye":
print("It was nice talking to you, goodbye!")
break break
else: intent = detect_intent(normalized_input)
response = random.choices(random_responses)[0] topic = extract_topic(normalized_input)
conversation_state["last_intent"] = intent
if topic:
conversation_state["last_topic"] = topic
response = generate_response(intent, topic)
print(response) print(response)
print("It was nice talking to you, goodbye!") # This list contains the random responses (you can add your own or translate them into your own language too)
# random_responses = ["That is quite interesting, please tell me more.",
# "I see. Do go on.",
# "Why do you say that?",
# "Funny weather we've been having, isn't it?",
# "Let's change the subject.",
# "Did you catch the game last night?"]
# print("Hello, I am Marvin, the simple robot.")
# print("You can end this conversation at any time by typing 'bye'")
# print("After typing each answer, press 'enter'")
# print("How are you today?")
# while True:
# # wait for the user to enter some text
# user_input = input("> ")
# if user_input.lower() == "bye":
# # if they typed in 'bye' (or even BYE, ByE, byE etc.), break out of the loop
# break
# else:
# response = random.choices(random_responses)[0]
# print(response)
# print("It was nice talking to you, goodbye!")

Loading…
Cancel
Save