Začněte nyníZačněte zdarma

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

Zobrazit kurz

Pokyny k cvičení

  • Definuj funkci send_message(), která přijímá 3 argumenty: state, pending a message.
  • Zavolej chitchat_response(message) a výsledek ulož do proměnné response. Pokud odpověď existuje, vypiš ji a vrať state spolu s hodnotou None.
  • Rozbal slovník policy_rules do proměnných new_state, response a pending_state. K tomu předej n-tici složenou z state a interpret(message).
  • Pokud pending není None, získej nové stavy a odpověď tak, že jako klíč slovníku policy_rules použ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"
])  
Upravit a spustit kód