Vše dohromady II
Teď, když máš definovanou funkci chitchat_response(message), je dalším krokem definovat funkci send_message(). Tato funkce by měla nejprve zavolat chitchat_response(message) a kafébotovu politiku použít pouze tehdy, když se pro danou zprávu nenajde shoda.
Toto cvičení je součástí kurzu
Building Chatbots in Python
Pokyny k cvičení
- Definuj funkci
send_message(), která přijímá 3 argumenty:state,pendingamessage. - Zavolej
chitchat_response(message)a výsledek ulož do proměnnéresponse. Pokud odpověď existuje, vypiš ji a vraťstatespolu s hodnotouNone. - Rozbal slovník
policy_rulesdo proměnnýchnew_state,responseapending_state. K tomu předej n-tici složenou zstateainterpret(message). - Pokud
pendingneníNone, získej nové stavy a odpověď tak, že jako klíč slovníkupolicy_rulespoužiješpending.
Interaktivní cvičení na vyzkoušení si v praxi
Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.
# Define send_message()
def ____:
print("USER : {}".format(message))
response = ____
if response is not None:
print("BOT : {}".format(response))
return state, None
# Calculate the new_state, response, and pending_state
____, ____, ____ = ____[(____, ____)]
print("BOT : {}".format(response))
if pending is not None:
new_state, response, pending_state = ____
print("BOT : {}".format(response))
if pending_state is not None:
pending = (pending_state, interpret(message))
return new_state, pending
# Define send_messages()
def send_messages(messages):
state = INIT
pending = None
for msg in messages:
state, pending = send_message(state, pending, msg)
# Send the messages
send_messages([
"I'd like to order some coffee",
"555-12345",
"do you remember when I ordered 1000 kilos by accident?",
"kenyan"
])