Python3, 10 lines of code, training a chat robot, we can also make the robot speak multiple languages.

1 Introduction

Little Diaosi : Brother Yu, you still owe me something.
Xiaoyu : Er... I still owe Nanbei to me.
Little Diaosi : Are you sure you don’t owe it.
Xiaoyu : There is still a certainty, this is a must, and affirmation.
Little Diaosi : Let me remind you, chatbots.
Xiaoyu : What's wrong with the chatbot?
Little Diaosi : ... training chatbots.
Xiaoyu : Then you can train,
Xiaocaosi : ... I just said it, I still remember this " Python3, 33 lines of code to build a chat robot, now I am no longer afraid of no one talking. "
Xiaoyu : I remember, what was posted two days ago.
Xiao Diaosi : Then tell me, the next article will arrange to train chatbots.
Xiaoyu : Oh... that's it.
Little Diaosi : That’s right, that’s it.
Xiaoyu : It's a matter of minutes.
Little Diaosi : Brother Yu, what are you...
Xiaoyu : Fuck it.

insert image description here

2. Code combat

2.1 Installation

Because the chatbot uses the chatterbot library, we need to install it.

pip install chatterbot

For other installation methods, you can refer to these two articles:

2.2 Introduction to ChatterBot

Definition :

  • The ChatterBot library is a Python-based chatbot library,
  • Can be used to create natural language processing bots;

Common methods :

  • ChatBot: A class for creating chatbots.
  • Train: The method used to train the chatbot.
  • get_response: The method used to get the chatbot's answer.
  • Corpus: Corpus for loading and training chatbots.

2.2 Actual combat


# -*- coding:utf-8 -*-
# @Time   : 2023-07-12
# @Author : Carl_DJ

'''
实现功能:
    训练聊天机器人。
'''
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

# 创建聊天机器人
bot = ChatBot('MyBot')

# 创建一个基于语料库的训练器
trainer = ChatterBotCorpusTrainer(bot)

# 使用英文语料库进行训练
trainer.train('chatterbot.corpus.english')

# 使用中文语料库进行训练
trainer.train('chatterbot.corpus.chinese')

# 在命令行中与机器人进行聊天
while True:
    try:
        user_input = input()
        response = bot.get_response(user_input)
        print(response)

    # 如果输入“退出”,则结束聊天
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

Parse :

  • First create a chatbot object named Mybot
  • Second, train the bot using the ChatterBotCorpusTrainer
    • Use the chatterbot.corpus.english corpus for training, including: Chinese, English, or use my own training data
  • Finally, through the loop, continuously receive the user's input, and use the bot.get_response() method to get the robot's reply, and then print it out.

insert image description here

3. Summary

Seeing this, today's sharing is almost over.
Today's main thing is a simple demo sharing, and no more interactive logic and functions have been added.
In the later stage, Xiaoyu will improve the interaction logic and functions for this part.

I am a small fish :

  • CSDN blog expert ;
  • Aliyun expert blogger ;
  • 51CTO blog expert ;
  • 51 certified lecturer, etc .;
  • Certified gold interviewer ;
  • Job interview and training planner ;
  • Certified expert bloggers in several domestic mainstream technical communities ;
  • Winners of the first and second prizes in the evaluation of various mainstream products (Alibaba Cloud, etc.) ;

Follow me and take you to learn more, more professional and prefaced Python technology.

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/131678938