除外スロットによるフィルタリング
ここでは、これまでの演習のアイデアを組み合わせて、ユーザーが複数のメッセージにわたって「欲しいもの」と「欲しくないもの」をボットに伝えられるようにします。
negated_ents() 関数はすでに定義されています。また、params 辞書に加えて neg_params 辞書も受け取るよう少し改良された find_hotels() 関数も定義済みです。
この演習はコースの一部です
Python でチャットボットを作る
演習の手順
message、params、neg_paramsを引数として受け取るrespond()関数を定義してください。messageとent_valsを引数としてnegated_ents()関数を使用し、結果をnegatedに格納してください。paramsとneg_paramsの辞書を引数として改良版find_hotels()関数を使用し、一致するホテルを検索してください。結果をresultsに格納してください。respond()関数の外でparamsとneg_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))