Joke Collection Website - Blessing messages - I used 100 lines of Python code to chat with Goddess on WeChat (attached code).
I used 100 lines of Python code to chat with Goddess on WeChat (attached code).
Introduce a small software developed with python, which can automatically chat with beautiful women. The following are all dry goods, which I wrote in my spare time. After continuous optimization, I will share it with you now. Then let's get started!
Prepare:
Programming tool IDE:pycharm
Python version: 3.6.0
First, create a new py file named ai_chat.py
PS: The codes of the following five steps can be directly copied into a single py file and run directly. In order to make it easier for readers to write code, I posted all the codes, but there was something wrong with the typesetting, so I typeset the screenshots in pycharm.
Step 1: Introduce the main product packages.
Briefly introduce the functions of the above packages: pickle package is used to serialize and save data and deserialize and read files, which can't be read by humans, but the speed of computer reading is super fast. (even if you open it with notepad, it's all garbled). Json package is a kind of text serialization, which is human-readable and convenient for you to modify (notepad is opened, and you can see and know all the contents inside). Gensim package is one of python packages in natural language processing, which is simple and easy to use, and it is a python package that must be used to get started with NLP algorithm. Jieba package is used for word segmentation, which is generally effective for algorithm coffee, but it is very fast and suitable for entry.
These bags are not the key. You can skip it when you study. After understanding the whole program flow, you can read the documents one by one.
Step 2: Static Configuration
Here, path refers to the location where the dialogue corpus (training data) is stored, and model_path is the path where the model is stored.
The following are personal programming habits. I am used to putting some configurations, such as file path, model storage path and model parameters in one class. Of course, when developing an actual project, it is stored in a configuration file and will not be written directly in the code. Here, for the convenience of demonstration, it is written together and easy to operate.
Step 3: Write a class to realize the integration of data guidance, model training and dialogue prediction.
When it runs for the first time, it will read the path of training data from the static configuration, read the data, train, and store the trained model to the specified model path. The subsequent operation is to import the model directly without training again.
For the model classes, just introduce them one by one.
Initialize () function and __init__ () function are the initialization and instantiation of objects, including assignment of basic parameters, model import, model training, model saving, and finally returning an object to the user.
__train_model () function, the problem is segmented, the bag-of-words model is realized by gesim, the tf-idf of each feature is counted, the sparse matrix is established, and then the index is established.
The __save_model () function and the __load_model () function appear in pairs, and many projects have these two functions for saving and importing models. The difference is that this project uses a file storage method, but actually uses an online database.
The get_answer () function uses the trained model to analyze the problem, and finally feeds back the predicted answer to the user.
Step 4: Write functions of three tool types to read and write files.
Among them, the obtained conversation material can be used as the training data of the machine to modify the conversation content independently. Here I just gave a few simple dialogue corpora. Online projects actually need a large number of corpora to train, so that the dialogue content is more full.
The functions of these three tools are relatively simple. In the get_data () function, the data is compiled by itself. You can add your own dialogue data according to your own habits, so that the final training model and dialogue mode will be closer to your own speaking style.
Step 5: Call the model and make dialogue prediction.
The main function main () is the starting point of your whole program and controls all the steps.
Running results:
Program background running results:
If you have any questions and want to get the source code (in fact, the code is all on it), you can trust me privately in the background and reply: python Intelligent Dialogue. I will send you the source code. Finally, thank you for reading and wish you a happy working life!
- Previous article:1000037 1 Can I unsubscribe from the SMS?
- Next article:JD.COM can't pay the IOU for cosmetic contact lenses.
- Related articles
- The latest epidemic prevention policy in Zhengzhou
- Send a greeting from an elder.
- Red envelope greetings from one to ten
- My friend bought a new house. What did you say to him?
- Iphone 1 1promax power saving settings
- How to restrict WeChat from sending messages?
- What if the sun shines in loans overdue?
- 006 19 harassing text messages
- Someone else brought me something from Jingdong Express. What is the content of the message I received?
- How to set the SMS voice broadcast of oppo mobile phone?