首页 > 编程语言 >【NLP开发】Python实现聊天机器人(ChatterBot)

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

时间:2022-10-08 13:40:52浏览次数:98  
标签:NLP get Python ChatterBot chatbot print import chatterbot response

文章目录

1、简介

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

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

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

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

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

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

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


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

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


之前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

Process flow diagram:

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

2、下载和安装

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

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

报错 OSError: [E941] Can’t find model ‘en’?
回答:方法如下:

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

pip install

安装完成后,在site-packages包里找到**en_core_web_sm-2.3.**1,复制改文件到项目的目录下,并更改文件名为 ‘en’。

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)

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

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")

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

  • (1)使用语料库数据进行训练
from chatterbot.trainers import ChatterBotCorpusTrainer

chatterbot = ChatBot("Training Example")
chatterbot.set_trainer(ChatterBotCorpusTrainer)

chatterbot.train(
"chatterbot.corpus.english"
)
  • (2)指定语料库范围
chatterbot.train(
"chatterbot.corpus.english.greetings",
"chatterbot.corpus.english.conversations"
)
  • (3)您还可以指定文件路径到语料库文件或语料库文件的目录。
chatterbot.train(
"./data/greetings_corpus/custom.corpus.json",
"./data/my_corpus/"
)
  • (4)使用Twitter API进行训练
    允许使用从Twitter收集的数据训练聊天机器人。
chatterbot.trainers.TwitterTrainer(storage, **kwargs)
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
from settings import TWITTER
import logging

'''
This example demonstrates how you can train your chat bot
using data from Twitter.

To use this example, create a new file called settings.py.
In settings.py define the following:

TWITTER = {
"CONSUMER_KEY": "my-twitter-consumer-key",
"CONSUMER_SECRET": "my-twitter-consumer-secret",
"ACCESS_TOKEN": "my-access-token",
"ACCESS_TOKEN_SECRET": "my-access-token-secret"
}
'''

# Comment out the following line to disable verbose logging
logging.basicConfig(level=logging.INFO)

chatbot = ChatBot(
"TwitterBot",
logic_adapters=[
"chatterbot.logic.BestMatch"
],
input_adapter="chatterbot.input.TerminalAdapter",
output_adapter="chatterbot.output.TerminalAdapter",
database="./twitter-database.db",
twitter_consumer_key=TWITTER["CONSUMER_KEY"],
twitter_consumer_secret=TWITTER["CONSUMER_SECRET"],
twitter_access_token_key=TWITTER["ACCESS_TOKEN"],
twitter_access_token_secret=TWITTER["ACCESS_TOKEN_SECRET"],
trainer="chatterbot.trainers.TwitterTrainer"
)

chatbot.train()

chatbot.logger.info('Trained database generated successfully!')
  • (5)使用Ubuntu对话语料库进行训练
    警告 Ubuntu对话语料库是一个海量数据集。当使用这个语料库时,开发者目前会遇到显着下降的表现,其形式是来自聊天机器人的延迟训练和响应时间。
chatterbot.trainers.UbuntuCorpusTrainer(storage, **kwargs)
  • (6)创建一个新的训练类
    您可以创建一个新的训练器,从您自己的数据文件中训练您的聊天机器人。如果您想以ChatterBot不直接支持的格式从数据源训练聊天工具,您可以选择这样做。

你的自定义教练应该继承chatterbot.trainers.Trainer类。你的训练器需要有一个名为train的方法,它可以接受你选择的任何参数。

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

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

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)

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

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)

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

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)

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

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)

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

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?')

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

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)

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

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)

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

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

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

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)

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


网友整理的语料库地址:

​ 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)

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

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:")))

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

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()

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

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)_nlp_23

标签:NLP,get,Python,ChatterBot,chatbot,print,import,chatterbot,response
From: https://blog.51cto.com/u_15800063/5737387

相关文章