【NLP开发】Python实现聊天机器人(ChatterBot)

🍺NLP开发系列相关文章编写如下🍺:

  1. 🎈【NLP开发】Python实现词云图🎈
  2. 🎈【NLP开发】Python实现图片文字识别🎈
  3. 🎈【NLP开发】Python实现中文、英文分词🎈
  4. 🎈【NLP开发】Python实现聊天机器人(ELIZA))🎈
  5. 🎈【NLP开发】Python实现聊天机器人(ALICE)🎈
  6. 🎈【NLP开发】Python实现聊天机器人(ChatterBot)🎈
  7. 🎈【NLP开发】Python实现聊天机器人(微软Azure)🎈
  8. 🎈【NLP开发】Python实现聊天机器人(微软小冰)🎈
  9. 🎈【NLP开发】Python实现聊天机器人(钉钉机器人)🎈
  10. 🎈【NLP开发】Python实现聊天机器人(微信机器人)🎈

1、简介

官方地址:
https://github.com/gunthercox/ChatterBot

ChatterBot is a machine learning, conversational dialog engine for creating chat bots.

在这里插入图片描述

ChatterBot是Python中基于机器学习的对话对话引擎,可以根据已知对话的集合生成响应。ChatterBot与语言无关的设计允许它被训练成说任何语言。
在这里插入图片描述

未经训练的聊天机器人实例开始时不知道如何进行通信。每次用户输入语句时,库都会保存他们输入的文本以及语句所响应的文本。随着 ChatterBot 接收到更多输入,它可以回复的响应数以及每个响应相对于输入语句的准确性也会增加。程序通过搜索与输入匹配的最接近匹配的已知语句来选择最接近的匹配响应,然后根据机器人与之通信的人员发出每个响应的频率返回对该语句最可能的响应。

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
之前chatbot基于大量规则库,不好维护。
主流架构为“NLU自然语言理解+DM对话管理+NLG自然语言生成”。
主要目标是意图识别与实体识别; NLU负责基础自然语言处理,DM负责对话状态维护、数据库查询等;NLG负责生成交互的自然语言。

An example of typical input would be something like this:

user: Good morning! How are you doing?
bot: I am doing very well, thank you for asking.
user: You're welcome.
bot: Do you like hats?

Process flow diagram:
在这里插入图片描述

2、下载和安装

在这里插入图片描述

pip install chatterbot  # 聊天机器人
pip install chatterbot-corpus  # 语料库
pip install spacy  # 自然语言处理

开始安装chatterbot1.0.5库的时候还比较顺利,最后自动安装spacy-2.1.9.tar.gz (30.7 MB)的时候总是报错。
在这里插入图片描述
于是手动安装spacy库(pip install spacy),但手动单独安装的spacy是最新的版本3.4.3。
在这里插入图片描述

cd C:\Users\tomcat\Desktop\ChatterBot-master
python setup.py build
python setup.py install

初步安装成功如下:
在这里插入图片描述
先运行一段代码试试:

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('Ron Obvious')

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)

# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.english")

# Get a response to an input statement
chatbot.get_response("Hello, how are you today?")

报错如下:
ModuleNotFoundError: No module named ‘spacy’
在这里插入图片描述
安装spacy库,如下:

pip install spacy

在这里插入图片描述
再运行上面chatterbot测试的代码试试。结果报错如下:
在这里插入图片描述
报错 OSError: [E941] Can’t find model ‘en’?
回答:方法如下:
这是因为自然语言学习包NLP安装的spacy包需要调用一个英语模块,但是系统中没有找到缘故。

python -m spacy download en

下载https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.3.1/en_core_web_sm-2.3.1.tar.gz
or
pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.3.1/en_core_web_sm-2.3.1.tar.gz

python -m spacy download en_core_web_sm
pip install en_core_web_sm-3.2.0.tar.gz
pip install en_core_web_md-3.2.0.tar.gz
#or
pip install en_core_web_sm-2.3.1.tar.gz

在这里插入图片描述

安装完成后,在site-packages包里找到**en_core_web_sm-2.3.**1,复制改文件到项目的目录下,并更改文件名为 ‘en’。
步骤一:将如下文件夹“en_core_web_sm-2.3.1”复制到chatterbot项目文件夹中。
在这里插入图片描述
步骤二:将“en_core_web_sm-2.3.1”改名为“en”。
在这里插入图片描述
步骤三:将当前工作文件夹切换到chatterbot文件夹。然后执行测试代码如下:
在这里插入图片描述
如果能成功运行,则需要最后一步:为en_core_web_sm模块创建spacy的用户快捷模式,以管理员身份在命令行中运行:

python -m spacy link en_core_web_sm en

PS:上述指令将会在spacy/data目录下创建模型的快捷方式。第一个参数是模型名词(模型已经通过pip安装),或模型存放目录。第二个参数是你想使用的内部名词。设置–force标记将会强制覆盖已存在的连接。

3、入门示例

3.1 基本使用

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('XiaoMu')

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)

# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.english")

# Get a response to an input statement
res = chatbot.get_response("Hello, how are you today?")
print(res)
res = chatbot.get_response("what is your name?")
print(res)
res = chatbot.get_response("My name is Xiao Mu.")
print(res)

在这里插入图片描述

from chatterbot import ChatBot  
from chatterbot.trainers import ChatterBotCorpusTrainer
chatbot = ChatBot("myBot")
trainer = ChatterBotCorpusTrainer(chatbot)
lineCounter = 1 
# 开始对话  
while True:  
    print(chatbot.get_response(input("(" + str(lineCounter) + ") user:")))  
    lineCounter += 1

在这里插入图片描述

3.2 训练数据

from chatterbot.trainers import ChatterBotCorpusTrainer

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)

# Train based on the english corpus
trainer.train("chatterbot.corpus.english")

# Train based on english greetings corpus
trainer.train("chatterbot.corpus.english.greetings")

# Train based on the english conversations corpus
trainer.train("chatterbot.corpus.english.conversations")

在这里插入图片描述

3.3 轮询测试

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('Xiao Mu')

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)

# Train based on the english corpus
trainer.train("chatterbot.corpus.english")

# Train based on english greetings corpus
trainer.train("chatterbot.corpus.english.greetings")

# Train based on the english conversations corpus
trainer.train("chatterbot.corpus.english.conversations")

# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.chinese")

lineCounter = 1
# 开始对话
while True:
    print(chatbot.get_response(input("(" + str(lineCounter) + ") user:")))
    lineCounter += 1

在这里插入图片描述

3.4 使用自定义集合

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

chatbot = ChatBot('Xiao Mu')

conversation = [
    "Hello",
    "Hi there!",
    "How are you doing?",
    "I'm doing great.",
    "That is good to hear",
    "Thank you.",
    "You're welcome."
]

trainer = ListTrainer(chatbot)
trainer.train(conversation)

response = chatbot.get_response("Good morning!")
print(response)

在这里插入图片描述

3.5 转换单位

  • convert_units.py
from chatterbot import ChatBot

bot = ChatBot(
    'Unit Converter',
    logic_adapters=[
        'chatterbot.logic.UnitConversion',
    ]
)

questions = [
    'How many meters are in a kilometer?',
    'How many meters are in one inch?',
    '0 celsius to fahrenheit',
    'one hour is how many minutes ?'
]

# Prints the convertion given the specific question
for question in questions:
    response = bot.get_response(question)
    print(question + ' -  Response: ' + response.text)

在这里插入图片描述

3.6 默认回答

  • default_response_example.py:
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer


# Create a new instance of a ChatBot
bot = ChatBot(
    'XiaoMu Bot',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch',
            'default_response': 'I am sorry, but I do not understand.',
            'maximum_similarity_threshold': 0.90
        }
    ]
)

trainer = ListTrainer(bot)

# Train the chat bot with a few responses
trainer.train([
    'How can I help you?',
    'I want to create a chat bot',
    'Have you read the documentation?',
    'No, I have not',
    'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html'
])

# Get a response for some unexpected input
response = bot.get_response('How do I make an omelette?')
print(response)

response = bot.get_response('How can I help you?')
print(response)

response = bot.get_response('No, I have not?')
print(response)

在这里插入图片描述

3.7 数学和时间

  • math_and_time.py:
from chatterbot import ChatBot

bot = ChatBot(
    'Math & Time Bot',
    logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter'
    ]
)

# Print an example of getting one math based response
response = bot.get_response('What is 4 + 9?')
print(response)

response = bot.get_response('What is 11 * 12 + 4 ?')
print(response)

# Print an example of getting one time based response
response = bot.get_response('What time is it?')
print(response)

response = bot.get_response('it is time to go to sleep?')
print(response)

在这里插入图片描述

3.8 内存数据库

  • memory_sql_example.py:
from chatterbot import ChatBot

# Uncomment the following lines to enable verbose logging
import logging
logging.basicConfig(level=logging.INFO)

# Create a new instance of a ChatBot
bot = ChatBot(
    'SQLMemoryTerminal',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    database_uri=None,
    logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter',
        'chatterbot.logic.BestMatch'
    ]
)

# Get a few responses from the bot
bot.get_response('What time is it?')
bot.get_response('What is 7 plus 7?')

在这里插入图片描述

3.9 具体响应示例

  • specific_response_example.py:
from chatterbot import ChatBot

# Create a new instance of a ChatBot
bot = ChatBot(
    'Exact Response Example Bot',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch'
        },
        {
            'import_path': 'chatterbot.logic.SpecificResponseAdapter',
            'input_text': 'Help me!',
            'output_text': 'Ok, here is a link: http://chatterbot.rtfd.org'
        }
    ]
)

# Get a response given the specific input
response = bot.get_response('Help me!')
print(response)
response = bot.get_response('Hello!')
print(response)
response = bot.get_response('World!')
print(response)

在这里插入图片描述

3.10 标记数据集示例

  • tagged_dataset_example.py:
from chatterbot import ChatBot
from chatterbot.conversation import Statement


chatbot = ChatBot(
    'Example Bot',
    # This database will be a temporary in-memory database
    database_uri=None
)

label_a_statements = [
    Statement(text='Hello', tags=['label_a']),
    Statement(text='Hi', tags=['label_a']),
    Statement(text='How are you?', tags=['label_a'])
]

label_b_statements = [
    Statement(text='I like dogs.', tags=['label_b']),
    Statement(text='I like cats.', tags=['label_b']),
    Statement(text='I like animals.', tags=['label_b'])
]

chatbot.storage.create_many(
    label_a_statements + label_b_statements
)

# Return a response from "label_a_statements"
response_from_label_a = chatbot.get_response(
    'How are you?',
    additional_response_selection_parameters={
        'tags': ['label_a']
    }
)

# Return a response from "label_b_statements"
response_from_label_b = chatbot.get_response(
    'How are you?',
    additional_response_selection_parameters={
        'tags': ['label_b']
    }
)

print('Response from label_a collection:', response_from_label_a.text)
print('Response from label_b collection:', response_from_label_b.text)

在这里插入图片描述

3.11 终端示例

  • terminal_example.py:
from chatterbot import ChatBot


# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

# Create a new instance of a ChatBot
bot = ChatBot(
    'Terminal',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter',
        'chatterbot.logic.BestMatch'
    ],
    database_uri='sqlite:///database.sqlite3'
)

print('Type something to begin...')

# The following loop will execute each time the user enters input
while True:
    try:
        user_input = input()
        bot_response = bot.get_response(user_input)
        print(bot_response)

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

在这里插入图片描述

3.12 训练语料库示例

  • training_example_chatterbot_corpus.py:
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
import logging

'''
This is an example showing how to train a chat bot using the
ChatterBot Corpus of conversation dialog.
'''

# Enable info level logging
# logging.basicConfig(level=logging.INFO)

chatbot = ChatBot('XiaoMu Bot')

# Start by training our bot with the ChatterBot corpus data
trainer = ChatterBotCorpusTrainer(chatbot)

trainer.train(
    # 'chatterbot.corpus.english',
    'chatterbot.corpus.chinese'
)

# Now let's get a response to a greeting
response = chatbot.get_response('How are you doing today?')
print(response)
response = chatbot.get_response('Hello?')
print(response)
response = chatbot.get_response('why can you not eat?')
print(response)
response = chatbot.get_response('怎么称呼你')
print(response)
response = chatbot.get_response('你什么时候走?')
print(response)
response = chatbot.get_response('为什么不能你吃?')
print(response)

在这里插入图片描述
网友整理的语料库地址:
https://github.com/codemayq/chinese_chatbot_corpus
https://github.com/candlewill/Dialog_Corpus

3.13 训练列表数据示例

  • training_example_list_data.py:
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer


'''
This is an example showing how to train a chat bot using the
ChatterBot ListTrainer.
'''

chatbot = ChatBot('XiaoMu Bot')

# Start by training our bot with the ChatterBot corpus data
trainer = ListTrainer(chatbot)

trainer.train([
    'Hello, how are you?',
    'I am doing well.',
    'That is good to hear.',
    'Thank you'
])

# You can train with a second list of data to add response variations

trainer.train([
    'Hello, how are you?',
    'I am great.',
    'That is awesome.',
    'Thanks'
])

# Now let's get a response to a greeting
response = chatbot.get_response('How are you doing today?')
print(response)
response = chatbot.get_response('Hello, how are you?')
print(response)
response = chatbot.get_response('Hello, how are you?')
print(response)

在这里插入图片描述

3.14 训练自定义数据示例

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

## 加载语料库
file = open(r"C:\Users\tomcat\Desktop\xiaohuangji50w_nofenci.conv",
            'r', encoding='utf-8')
corpus = []
print('开始加载语料!')
while 1:
    try:
        line = file.readline()
        if not line:
            break
        if line == 'E\n':
            continue
        corpus.append(line.split('M ')[1].strip('\n'))
    except:
        pass
file.close()
print('语料加载完毕!')

## 新建机器人
chatbot = ChatBot("XiaoMu bot")
trainer = ListTrainer(chatbot)

## 训练语料库
print('开始训练!')
trainer.train(corpus[:10000])
print('训练完毕!')

## 轮询测试
while True:
    print(chatbot.get_response(input("Me:")))

在这里插入图片描述

3.15 基于tkinter的示例

  • tkinter_gui.py:
from chatterbot import ChatBot
import tkinter as tk
try:
    import ttk as ttk
    import ScrolledText
except ImportError:
    import tkinter.ttk as ttk
    import tkinter.scrolledtext as ScrolledText
import time


class TkinterGUIExample(tk.Tk):

    def __init__(self, *args, **kwargs):
        """
        Create & set window variables.
        """
        tk.Tk.__init__(self, *args, **kwargs)

        self.chatbot = ChatBot(
            "GUI Bot",
            storage_adapter="chatterbot.storage.SQLStorageAdapter",
            logic_adapters=[
                "chatterbot.logic.BestMatch"
            ],
            database_uri="sqlite:///database.sqlite3"
        )

        self.title("Chatterbot")

        self.initialize()

    def initialize(self):
        """
        Set window layout.
        """
        self.grid()

        self.respond = ttk.Button(self, text='Get Response', command=self.get_response)
        self.respond.grid(column=0, row=0, sticky='nesw', padx=3, pady=3)

        self.usr_input = ttk.Entry(self, state='normal')
        self.usr_input.grid(column=1, row=0, sticky='nesw', padx=3, pady=3)

        self.conversation_lbl = ttk.Label(self, anchor=tk.E, text='Conversation:')
        self.conversation_lbl.grid(column=0, row=1, sticky='nesw', padx=3, pady=3)

        self.conversation = ScrolledText.ScrolledText(self, state='disabled')
        self.conversation.grid(column=0, row=2, columnspan=2, sticky='nesw', padx=3, pady=3)

    def get_response(self):
        """
        Get a response from the chatbot and display it.
        """
        user_input = self.usr_input.get()
        self.usr_input.delete(0, tk.END)

        response = self.chatbot.get_response(user_input)

        self.conversation['state'] = 'normal'
        self.conversation.insert(
            tk.END, "Human: " + user_input + "\n" + "ChatBot: " + str(response.text) + "\n"
        )
        self.conversation['state'] = 'disabled'

        time.sleep(0.5)


gui_example = TkinterGUIExample()
gui_example.mainloop()

在这里插入图片描述

在这里插入图片描述

3.16 基于flask的示例

  • app.py:
from flask import Flask, render_template, request
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

app = Flask(__name__)

english_bot = ChatBot("Chatterbot", storage_adapter="chatterbot.storage.SQLStorageAdapter")
trainer = ChatterBotCorpusTrainer(english_bot)
trainer.train("chatterbot.corpus.english")

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/get")
def get_bot_response():
    userText = request.args.get('msg')
    return str(english_bot.get_response(userText))

if __name__ == "__main__":
    app.run()

【NLP开发】Python实现聊天机器人(ChatterBot)

结语

如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;(✿◡‿◡)
感谢各位大佬童鞋们的支持!( ´ ▽´ )ノ ( ´ ▽´)っ!!!
在这里插入图片描述

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年3月8日 下午8:57
下一篇 2023年3月8日 下午9:00

相关推荐