雑談
ここでは、シンプルなEchoBotを卒業して、「あなたの名前は?」や「今日の天気は?」といった簡単な質問に答えられるボットを作成しましょう。
質問をキー、対応する返答を値とする辞書を使います。
この方法では、メッセージが完全に一致する場合にのみ正しく応答できるため、大きな制限があります。後の演習では、より柔軟な解決策を作成します。
send_message() 関数はあらかじめ定義されており、bot_template および user_template 変数も用意されています。
この演習はコースの一部です
Python でチャットボットを作る
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Define variables
name = "Greg"
weather = "cloudy"
# Define a dictionary with the predefined responses
responses = {
"what's your name?": "my name is {0}".format(name),
"what's today's weather?": "the weather is {0}".format(weather),
"default": "default message"
}
# Return the matching response if there is one, default otherwise
def ____(____):
# Check if the message is in the responses
if ____ in ____:
# Return the matching message
bot_message = ____[____]
else:
# Return the "default" message
bot_message = ____["____"]
return bot_message