In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to use Python to make an intelligent chat robot. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
With the development of natural language technology and machine learning technology, more and more interesting natural language projects are presented in front of everyone. Chatbot is the most typical application. Today, the editor leads you to use less than 20 lines of code to build your own chatbot in two ways.
1. Artifact wxpy Library
First of all, the editor first introduces the python library used in this project, and the libraries mainly used in this project are wxpy and chatterbot.
Wxpy is based on the itchat library, through a large number of interface optimization, make the module easy to use, and expand the function. What is interface optimization? to put it simply, the user can call the function directly and enter a few parameters, and you don't need to care about the underlying implementation of the function.
Chatterbot is a chatbot engine based on machine learning, written based on python, which can match memories from existing conversations.
Because Wxpy uses a large number of interface integration, it is very convenient to use. The following figure shows a simple wxpy use. By entering the interactive interface, you can talk to your designated friends. The code and effect are shown in the following figure:
The print_msg function in the above code uses the decorator of the wxpy library, and the role of the decorator here can be viewed by consulting its source code, mainly for registering the configuration of the message. For example, the print_msg function can output the reply message of the other party. We also see the communication between the two sides on the mobile Wechat, so to some extent, wxpy is the Wechat on the PC.
two。 Turing chat robot
Next we will implement * chat robot, which is a Turing robot integrated in wxpy. Because the Turing robot is highly professional, the chat robot communication is very smooth. So how to embed the Turing robot into our communication? First of all, we need to go to the Turing Robot website to register to get our own robot. After registration, we can generate a Turing robot and get an api_key, as shown in the following figure:
This api_key is the key to our follow-up production of chat robots. The code and effect of the Turing chat robot are shown in the following figure:
From the chat record on the right side of the picture above, the chat robot relying on the Turing robot is very effective, and it can also query the weather and other functions.
3. Self-trained chat robot
Below we do not rely on the Turing robot, but rely on ChatterBot to make a robot, although the effect of this robot, although the performance gap is larger than the previous model, but it can train our own corpus, the following is a demonstration of the code and effects.
As can be seen from the above picture, it may be due to the lack of corpus resources and the inability to actively query on the Internet. So it takes a lot of work to make the answers basically match the questions.
Is there nothing else we can do besides this? Of course not. ChatterBot provides us with a way to train, and we can provide materials for them to train. The code and effect are shown in the following figure:
We first define a chatbot, and then add the training data to make the model match our problems and get the training results.
The figure on the right shows our results, which show that because there is no "in" data in our training set, the answers we get are baffling, while for "Hello" and "what's your name?" The answers to these two questions have been answered very well, which is the advantage of adding training data sets.
Trainer.train (["Hello", "Nice to meet you", "what's your name?", "my name is chat-robot-2.")
(data from the training set)
You can also join the corpus you want to train, and you will also get good results. Some students may ask how this is trained. The answer is in the chatterbot source code, open the source code we can choose here the chatbot training method is "BestMatch", that is, the most matching way, from the training dialogue to find the most familiar sentences, and then provide answers according to the dialogue.
Isn't it interesting for Python to do a machine learning project?
The above is the editor for you to share how to use Python to do an intelligent chat robot, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.