智能对话机器人学习与制作(1)

来源:互联网 发布:江南大学网络教育入口 编辑:程序博客网 时间:2024/05/22 06:04

1. 前言

最近人工智能这个概念特别的火, 就想试着做一些类似的东西, 正好看到了这个项目 http://www.codeproject.com/articles/36106/chatbot-tutorial 于是就想跟着做一下, 顺便记录下相关的内容

2. 流程

1. version 1.0

1. 原理

第一版的功能非常简单, 只是创建了一个知识库, 用户输入一段话之后, 随机的从知识库中挑选一句话进行输出

2. 实现

chatbot.h

#ifndef _CHATBOT_H_#define _CHATBOT_H_#include <vector>#include <string>using std::vector;using std::string;/*** This is a ChatBot class which is used to do some AI talking with you* @date         2016.10.21*/class ChatBot{public:    /**    *   get a response.     *  @return      string      */    static string getResponse();private:    static vector<string> arr_tips;   /**< this is something like the database for the AI tips */};#endif // _CHATBOT_H_

chatbot.cpp

#include "ChatBot.h"#include <random>#include <ctime>using std::default_random_engine;using std::uniform_int_distribution;string ChatBot::getResponse(){    default_random_engine e;    uniform_int_distribution<unsigned int> u(0, arr_tips.size() - 1);    e.seed(time(nullptr));    int selectionId = u(e);    return arr_tips[selectionId];}vector<string> ChatBot::arr_tips = vector<string>{"I HEARD YOU!",            "So, You are talking to me",            "continue, i'am listening",            "very interesting conversation",            "tell me more..."};

main.cpp

#include "ChatBot.h"#include <string>#include <iostream>using namespace std;int main(){    string line;    while (getline(cin, line)){        cout << "  =====  " << ChatBot::getResponse() << endl;    }    return 0;}

2. version 1.1

1. 基本原理

在version 1.0 的基础上进行改进, 对原有的知识库根据关键字进行细分, 通过将输入的数据与知识库中的关键句进行比对, 获取相应话题的应答知识子库,随机挑选一条进行输出即可

2. 一个细节

vector 中的operator[] 是不会抛出异常的, 所以通过try/catch 机制也无法对其进行捕获。 通常可以采用 at 方法作为替代, 因为at 方法会对越界进行检查, 并抛出out_of_range 异常

3. 实现

chatbot.h

#ifndef _CHATBOT_H_#define _CHATBOT_H_#include <vector>#include <string>using std::vector;using std::string;namespace zhyh2010{    typedef struct _RECORD{        string input;        vector<string> responses;        _RECORD(string input, vector<string> responses) :input(input), responses(responses){}    }RECORD;    /**    * This is a ChatBot class which is used to do some AI talking with you    * @date         2016.10.21    */    class ChatBot    {    public:        /**        *   get a response.        *  @param input 输入的提示        *  @return      string        */        static string getResponse(const string & input);    private:        static vector<RECORD> arr_tips;   /**< this is something like the database for the AI tips */        static vector<string> findMatches(string input);   /**< 查找与输入input 匹配的参考回复库资料 */        static int getRandomId(int size);   /**< 返回参考资料库中选择的资料语句的id */        static string ToUpper(string line); /**< 将字符串大写 */    };}#endif // _CHATBOT_H_

chatbot.cpp

#include "ChatBot.h"#include <random>#include <ctime>#include <exception>#include <algorithm>using std::default_random_engine;using std::uniform_int_distribution;namespace zhyh2010{    string ChatBot::getResponse(const string & input)    {        try{            auto tips = ChatBot::findMatches(input);            //return tips[getRandomId(tips.size())];   /**< operator[] 没有异常机制直接崩溃 */            return tips.at(getRandomId(tips.size()));        }catch (std::exception){            if (input == "BYE")                return "bye";            return "sorry, I cannot understand what you say!!";        }           }    vector<RECORD> ChatBot::arr_tips = vector<RECORD>{        RECORD( "WHAT IS YOUR NAME",        { "MY NAME IS CHATTERBOT2.",        "YOU CAN CALL ME CHATTERBOT2.",        "WHY DO YOU WANT TO KNOW MY NAME?" }        ),        { "HI",        { "HI THERE!",        "HOW ARE YOU?",        "HI!" }        },        { "HOW ARE YOU",        { "I'M DOING FINE!",        "I'M DOING WELL AND YOU?",        "WHY DO YOU WANT TO KNOW HOW AM I DOING?" }        },        { "WHO ARE YOU",        { "I'M AN A.I PROGRAM.",        "I THINK THAT YOU KNOW WHO I'M.",        "WHY ARE YOU ASKING?" }        },        { "ARE YOU INTELLIGENT",        { "YES,OFCORSE.",        "WHAT DO YOU THINK?",        "ACTUALY,I'M VERY INTELLIGENT!" }        },        { "ARE YOU REAL",        { "DOES THAT QUESTION REALLY MATERS TO YOU?",        "WHAT DO YOU MEAN BY THAT?",        "I'M AS REAL AS I CAN BE." }        }    };    vector<string> ChatBot::findMatches(string input)    {        vector<string> res;        for (auto item : arr_tips){            if (ToUpper(item.input) == ToUpper(input))                res = std::move(item.responses);        }        return res;    }    int ChatBot::getRandomId(int size)    {        default_random_engine e;        uniform_int_distribution<unsigned int> u(0, size - 1);        e.seed(time(nullptr));        return u(e);    }    string ChatBot::ToUpper(string line)    {        std::transform(line.begin(), line.end(), line.begin(), toupper);        return line;    }}

main.cpp

#include "ChatBot.h"#include <string>#include <iostream>using namespace std;using namespace zhyh2010;int main(){    string line;    while (getline(cin, line)){        cout << "  =====  " << ChatBot::getResponse(line) << endl;    }    return 0;}
0 0