JustCode 源码随笔-07

Attempt at creating AI surpass ChatGPT – 04
尝试创造能超越ChatGPT的人工智能 – 04

//++中文版++//

这是简单的练习,要求使用C++语言,编写一个简单的、基于文本的聊天机器人。

#include <iostream>
#include <string>
#include <vector>
#include <map>

class ChatBot {
public:
    ChatBot() {
        responses = {
            {"1", "你好!我今天能帮你做什么?"},
            {"2", "再见!祝你今天过得愉快。"},
            {"3", "我是个机器人,我没有感觉,但我运行正常。"},
            {"4", "我是一个简单的C++聊天机器人。"},
        };
    }

    void displayMenu() {
        std::cout << "\n请选择一个选项:\n"
                  << "1. 打招呼\n"
                  << "2. 说再见\n"
                  << "3. 询问我现在好吗\n"
                  << "4. 询问我的名字\n"
                  << "0. 退出\n";
    }

    void run() {
        std::string input;
        while (true) {
            displayMenu();

            std::cout << "你的选择: ";
            std::getline(std::cin, input);

            if (responses.find(input) != responses.end()) {
                std::cout << "机器人: " << responses[input] << std::endl;
            } else if (input == "0") {
                break;
            } else {
                std::cout << "机器人: 对不起,我没有理解你的意思。请再试一次。" << std::endl;
            }
        }
    }

private:
    std::map<std::string, std::string> responses;
};

int main() {
    ChatBot bot;
    bot.run();

    return 0;
}

这段代码创建了一个简单的基于菜单的聊天机器人。以下是它的主要功能:

1. 创建聊天机器人(ChatBot)类: 这个类代表了聊天机器人。它存储了一系列预设的回复,这些回复是基于用户的输入选择。

2. displayMenu函数: 这个函数显示了一个简单的菜单,用户可以根据这个菜单选择他们想要进行的对话。

3. run函数:这个函数是聊天机器人的主要运行函数。它首先显示菜单,然后获取用户的输入。如果用户输入的是菜单中的一个选项,机器人会返回对应的预设回复。如果用户输入的是”0″,则程序结束。如果用户的输入没有在菜单中,机器人会返回一个错误消息。

4. main函数: 这个函数创建了一个ChatBot对象,并运行它。

这个程序是一个简单的示例,实际的聊天机器人可能需要更复杂的逻辑来处理用户的输入,以提供更自然和有用的对话。

经测试,运行正常,如下图:

未完待续。

 

//++English Version++//

This is a simple exercise, which requires using the C++ language to program a simple, text-based chatbot.

#include <iostream>
#include <string>
#include <vector>
#include <map>

class ChatBot {
public:
    ChatBot() {
        responses = {
            {"1", "Hello! How can I help you today?"},
            {"2", "Goodbye! Have a nice day."},
            {"3", "I'm a bot, so I don't have feelings, but I'm functioning as expected."},
            {"4", "I'm a simple C++ chatbot."},
        };
    }

    void displayMenu() {
        std::cout << "\nPlease choose an option:\n"
                  << "1. Say hello\n"
                  << "2. Say goodbye\n"
                  << "3. Ask how I am\n"
                  << "4. Ask my name\n"
                  << "0. Quit\n";
    }

    void run() {
        std::string input;
        while (true) {
            displayMenu();

            std::cout << "Your choice: ";
            std::getline(std::cin, input);

            if (responses.find(input) != responses.end()) {
                std::cout << "Bot: " << responses[input] << std::endl;
            } else if (input == "0") {
                break;
            } else {
                std::cout << "Bot: I'm sorry, I didn't understand that. Please try again." << std::endl;
            }
        }
    }

private:
    std::map<std::string, std::string> responses;
};

int main() {
    ChatBot bot;
    bot.run();

    return 0;
}

This code creates a simple menu-based chatbot. Here are its main functions:

  1. Creating a ChatBot class: This class represents the chatbot. It stores a series of preset responses based on user input choices.
  2. displayMenu function: This function displays a simple menu. Users can choose the conversation they want to have based on this menu.
  3. run function: This is the main running function of the chatbot. It first displays the menu and then gets user input. If the user inputs an option from the menu, the robot will return the corresponding preset response. If the user inputs “0”, the program ends. If the user’s input is not in the menu, the robot will return an error message.
  4. main function: This function creates a ChatBot object and runs it.

This program is a simple example. Actual chatbots may require more complex logic to handle user input to provide more natural and useful dialogues.

The program has been tested and is running properly, as shown in the picture below:

To be continued.