Python KEYWORD_CATEGORIES = {\'happy\': [\'great\',\'excited\',\'happy\',\'aweso
ID: 3681199 • Letter: P
Question
Python
KEYWORD_CATEGORIES = {'happy': ['great','excited','happy','awesome'], 'uncertain': ['maybe','worried','dunno'], 'sad': ['unhappy','miserable','awful'], 'good': ['pleasant','lovely','nice']}
CATEGORY_RESPONSES = {'happy': ["I'm glad to hear it!", "That's great!", "I like your attitude."], 'uncertain': ["No one knows what the future holds.", "Don't worry so much.", "It'll turn out ok."], 'sad': ["Tomorrow will be better.", "Cheer up!", "Let put a big smile, okay?"], 'good': ["I'm happy for you!", "Awesome news.", "The best thing can happens."]}
def keyword_category(keyword):
keyword = keyword.lower()
for key in KEYWORD_CATEGORIES:
if keyword in KEYWORD_CATEGORIES[key]:
return key
return "undefined"
def sentence_category(sentence):
for word in sentence.split():
if keyword_category(word) != "undefined":
return keyword_category(word)
return "undefined"
def category_response(category):
import random
for key in CATEGORY_RESPONSES:
if category in key:
return random.choice(CATEGORY_RESPONSES[key])
def chat():
print("Hello, I am a chatbot.")
list_of_str = []
while True:
user_input = input("> ")
if user_input.lower() == "bye":
print("Goodbye! It was nice talking to you.")
return list_of_str
break
category = sentence_category(user_input)
response = category_response(category)
print(response)
list_of_str.append('> ' + user_input)
list_of_str.append(response)
How do I fix my last function definition so the outcome display will include the early print (Hello, I am a chatbox) and the print after 'bye' is being input (Goodbye! ...) ? Like this:
Thank you!!
Explanation / Answer
Program:
KEYWORD_CATEGORIES = {'happy': ['great','excited','happy','awesome'], 'uncertain': ['maybe','worried','dunno'], 'sad': ['unhappy','miserable','awful'], 'good': ['pleasant','lovely','nice']}
CATEGORY_RESPONSES = {'happy': ["I'm glad to hear it!", "That's great!", "I like your attitude."], 'uncertain': ["No one knows what the future holds.", "Don't worry so much.", "It'll turn out ok."], 'sad': ["Tomorrow will be better.", "Cheer up!", "Let put a big smile, okay?"], 'good': ["I'm happy for you!", "Awesome news.", "The best thing can happens."]}
def keyword_category(keyword):
keyword = keyword.lower()
for key in KEYWORD_CATEGORIES:
if keyword in KEYWORD_CATEGORIES[key]:
return key
return "undefined"
def sentence_category(sentence):
for word in sentence.split():
if keyword_category(word) != "undefined":
return keyword_category(word)
return "undefined"
def category_response(category):
import random
for key in CATEGORY_RESPONSES:
if category in key:
return random.choice(CATEGORY_RESPONSES[key])
def chat():
print("Hello, I am a chatbot.")
list_of_str = []
while True:
user_input = raw_input("> ")
if user_input.lower() == "bye":
print("Goodbye! It was nice talking to you.")
return list_of_str
break
category = sentence_category(user_input)
response = category_response(category)
print(response)
list_of_str.append('> ' + user_input)
list_of_str.append(response)
print(chat())
output:
>I have a some great news
I'm glad to hear it!
>Or maybe it's not so great
No one knows what the future holds.
> lovely chat with you
>bye
Goodbye! It was nice talking to you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.