ELIZA IV: すべてをまとめる
これまでの演習で学んだ内容をすべて組み合わせて、チャットボットを完成させましょう。
match_rule()、send_message()、replace_pronouns() の各関数はすでに定義済みで、rules 辞書もワークスペースで利用できます。
ここでは、message を引数に受け取る respond() という関数を作成します。この関数は send_message() に渡す適切な応答を生成します。
この演習はコースの一部です
Python でチャットボットを作る
演習の手順
rules辞書とmessageを引数にしてmatch_rule()を呼び出し、responseとphraseを取得します。responseに文字列'{0}'が含まれているかどうかを確認して、テンプレートかどうかを判断します。含まれている場合は以下を行います。phraseにreplace_pronouns()関数を適用します。responseに対して.format()を使用してphraseを埋め込み、responseの値を上書きします。
- 回答を送信 をクリックして、ボットが各メッセージにどのように応答するか確認しましょう。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Define respond()
def respond(message):
# Call match_rule
____, ____ = ____
if '{0}' in response:
# Replace the pronouns in the phrase
phrase = ____
# Include the phrase in the response
response = ____
return response
# Send the messages
send_message("do you remember your last birthday")
send_message("do you think humans should be worried about AI")
send_message("I want a robot friend")
send_message("what if you could be anything you wanted")