返回首页

c++智能机器人问答系统设计

来源:www.fanlv.net  时间:2022-05-18 06:24   点击:296  编辑:宋家   手机版

#include #include #include int main() { std::string response[] = { i heard you!, so, you are talking to me., continue, i’m listening., very interesting conversation., tell me more... }; srand((unsigned) time(null)); std::string sinput = ; std::string sresponse = ; while(1) { std::cout << >; std::getline(std::cin, sinput); int nselection = rand() % 5; sresponse = response[nselection]; std::cout << sresponse << std::endl; } return 0; } as you can see, it doesn't take a lot of code to write a very basic program that can interact with a user but it would probably be very difficult to write a program that would really be capable of truly interpreting what the user is actually saying and after that would also generate an appropriate response to it. these have been a long term goal since the beginning and even before the very first computers were created. in 1951,the british mathematician alan turing has came up with the question can machines think and he has also propose a test which is now known as the turing test. in this test, a computer program and also a real person is set to speak to a third person (the judge) and he has to decide which of them is the real person. nowadays, there is a competition that was named the loebner prize and in this competition bots that has successfully fool most of the judge for at list 5 minutes would win a prize of 100.000$. so far no computer program was able to pass this test successfully. one of the major reasons for this is that computer programs written to compute in such contest have naturally the tendency of committing a lot of typo (they are often out of the context of the conversation). which means that generally, it isn't that difficult for a judge to decide whether he is speaking to a computer program or a real person. also, the direct ancestor of all those program that tries to mimic a conversation between real human beings is eliza, the first version of this program was written in 1966 by joseph weizenbaum a professor of mit. chatbots in general are considered to belong to the weak ai field (weak artificial intelligence) as opposed to strong a.i who's goal is to create programs that are as intelligent as humans or more intelligent. but it doesn't mean that chatbots do not have any true potential. being able to create a program that could communicate the same way humans do would be a great advance for the ai field. chatbot is this part of artificial intelligence which is more accessible to hobbyist (it only take some average programming skill to be a chatbot programmer). so, programmers out there who wanted to create true ai or some kind of artificial intelligence, writing intelligent chatbots is a great place to start! now, let's get back to our previous program, what are the problems with it? well, there is a lot of them. first of all, we can clearly see that the program isn't really trying to understand what the user is saying but instead he is just selecting a random response from his database each time the user type some sentence on the keyboard. and also, we could notice that the program repeat himself very often. one of the reason for this is because of the size of the database which is very small (5 sentences). the second thing that would explain the repetitions is that we haven't implemented any mechanism that would control this unwanted behavior. how do we move from a program that just select responses randomly to whatever input that the user might enter on the keyboard to a program that shows some more understanding of the inputs? the answer to that question is quiet simple; we simply need to use keywords. a keyword is just a sentence (not necessarily a complete one) or even a word that the program might recognize from the user's input which then makes it possible for the program to react to it (ex: by printing a sentence on the screen). for the next program, we will write a knowledge base or database, it will be composed of keywords and some responses associated to each keyword. so, now we know what to do to improve our first chatterbot and make it more intelligent. let’s proceed on writing our second bot, we will call it chatterbot2. hide shrink copy code// // program name: chatterbot2 // description: this is an improved version // of the previous chatterbot program chatterbot1 // this one will try a little bit more to understand what the user is trying to say // // author: gonzales cenelia // #pragma warning(disable: 4786) #include #include #include #include const int max_resp = 3; typedef std::vector vstring; vstring find_match(std::string input); void copy(char *array[], vstring &v); typedef struct { char *input; char *responses[max_resp]; }record; record knowledgebase[] = { {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.} } }; size_t nknowledgebasesize = sizeof(knowledgebase)/sizeof(knowledgebase[0]); int main() { srand((unsigned) time(null)); std::string sinput = ; std::string sresponse = ; while(1) { std::cout << >; std::getline(std::cin, sinput); vstring responses = find_match(sinput); if(sinput == bye) { std::cout << it was nice talking to you user, see you nexttime! << std::endl; break; } else if(responses.size() == 0) { std::cout << i'm not sure if i understand what you are talking about. << std::endl; } else { int nselection = rand() % max_resp; sresponse = responses[nselection]; std::cout << sresponse << std::endl; } } return 0; } // make a search for the user's input // inside the database of the program vstring find_match(std::string input) { vstring result; for(int i = 0; i < nknowledgebasesize; ++i) { if(std::string(knowledgebase[i].input) == input) { copy(knowledgebase[i].responses, result); return result; } } return result; } void copy(char *array[], vstring &v) { for(int i = 0; i < max_resp; ++i) { v.push_back(array[i]); } }

顶一下
(0)
0%
踩一下
(0)
0%