始める無料で始める

保留中のアクション II

policy() 関数を定義したので、次は send_message() 関数を作成しましょう。この関数は pending アクションと message の2つを引数として受け取り、policy() 関数を活用してボットの応答を決定します。

前の演習で作成した policy(intent) 関数は事前に読み込まれています。

この演習はコースの一部です

Python でチャットボットを作る

コースを見る

演習の手順

  • pendingmessage の2つの引数を受け取る send_message() という関数を定義してください。
  • message を解釈した結果を policy() の引数として渡し、戻り値を actionpending_action の2つの変数に展開してください。
  • action"do_pending" であり、かつ pendingNone でない場合は pending の応答を出力してください。それ以外の場合は action を出力してください。
  • send_messages() 関数の定義内で、pendingmsg を引数として send_message() を呼び出してください。その後、回答を送信してメッセージを送り、結果を確認しましょう。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Define send_message()
def ____(____, ____):
    print("USER : {}".format(message))
    ____, ____ = ____
    if ____ == "____" and pending is not None:
        print("BOT : {}".format(____))
    else:
        print("BOT : {}".format(____))
    return pending_action
    
# Define send_messages()
def send_messages(messages):
    pending = None
    for msg in messages:
        pending = ____

# Send the messages
send_messages([
    "I'd like to order some coffee",
    "ok yes please"
])
コードを編集して実行