始める無料で始める

ELIZA IV: すべてをまとめる

これまでの演習で学んだ内容をすべて組み合わせて、チャットボットを完成させましょう。 match_rule()send_message()replace_pronouns() の各関数はすでに定義済みで、rules 辞書もワークスペースで利用できます。

ここでは、message を引数に受け取る respond() という関数を作成します。この関数は send_message() に渡す適切な応答を生成します。

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

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

コースを見る

演習の手順

  • rules 辞書と message を引数にして match_rule() を呼び出し、responsephrase を取得します。
  • response に文字列 '{0}' が含まれているかどうかを確認して、テンプレートかどうかを判断します。含まれている場合は以下を行います。
    • phrasereplace_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")
コードを編集して実行