始める無料で始める

除外スロットによるフィルタリング

ここでは、これまでの演習のアイデアを組み合わせて、ユーザーが複数のメッセージにわたって「欲しいもの」と「欲しくないもの」をボットに伝えられるようにします。

negated_ents() 関数はすでに定義されています。また、params 辞書に加えて neg_params 辞書も受け取るよう少し改良された find_hotels() 関数も定義済みです。

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

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

コースを見る

演習の手順

  • messageparamsneg_params を引数として受け取る respond() 関数を定義してください。
  • messageent_vals を引数として negated_ents() 関数を使用し、結果を negated に格納してください。
  • paramsneg_params の辞書を引数として改良版 find_hotels() 関数を使用し、一致するホテルを検索してください。結果を results に格納してください。
  • respond() 関数の外で paramsneg_params の辞書を初期化し、「回答を送信」をクリックしてボットの応答を確認しましょう!

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

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

# Define the respond function
def ____:
    # Extract the entities
    entities = interpreter.parse(message)["entities"]
    ent_vals = [e["value"] for e in entities]
    # Look for negated entities
    negated = ____
    for ent in entities:
        if ent["value"] in negated and negated[ent["value"]]:
            neg_params[ent["entity"]] = str(ent["value"])
        else:
            params[ent["entity"]] = str(ent["value"])
    # Find the hotels
    results = ____
    names = [r[0] for r in results]
    n = min(len(results),3)
    # Return the correct response
    return responses[n].format(*names), params, neg_params

# Initialize params and neg_params
params = ____
neg_params = ____

# Pass the messages to the bot
for message in ["I want a cheap hotel", "but not in the north of town"]:
    print("USER: {}".format(message))
    response, params, neg_params = respond(message, params, neg_params)
    print("BOT: {}".format(response))
コードを編集して実行