200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 【小沐学NLP】Python实现聊天机器人(ChatterBot 代码示例)

【小沐学NLP】Python实现聊天机器人(ChatterBot 代码示例)

时间:2021-03-31 02:18:18

相关推荐

【小沐学NLP】Python实现聊天机器人(ChatterBot 代码示例)

文章目录

1、简介2、下载和安装3、入门示例3.1 基本使用3.2 训练数据3.3 轮询测试3.4 使用自定义集合3.5 转换单位3.6 默认回答3.7 数学和时间3.8 内存数据库3.9 具体响应示例3.10 标记数据集示例3.11 终端示例3.12 训练语料库示例3.13 训练列表数据示例3.14 训练自定义数据示例3.15 基于tkinter的示例3.16 基于flask的示例 结语

1、简介

官方地址:

/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。

仍然安装失败,不想折腾了,这是因为清华源的chatterbot版本太老,因此弃用之。

于是直接在github下载源码进行安装(/gunthercox/ChatterBot)。

会发现ChatterBot的源码版本为:ChatterBot==1.1.0a7

cd C:\Users\tomcat\Desktop\ChatterBot-masterpython setup.py buildpython setup.py install

初步安装成功如下:

先运行一段代码试试:

from chatterbot import ChatBotfrom chatterbot.trainers import ChatterBotCorpusTrainerchatbot = ChatBot('Ron Obvious')# Create a new trainer for the chatbottrainer = ChatterBotCorpusTrainer(chatbot)# Train the chatbot based on the english corpustrainer.train("chatterbot.corpus.english")# Get a response to an input statementchatbot.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

下载/explosion/spacy-models/releases/download/en_core_web_sm-2.3.1/en_core_web_sm-2.3.1.tar.gz

or

pip install /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_smpip install en_core_web_sm-3.2.0.tar.gzpip install en_core_web_md-3.2.0.tar.gz#orpip 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 ChatBotfrom chatterbot.trainers import ChatterBotCorpusTrainerchatbot = ChatBot('XiaoMu')# Create a new trainer for the chatbottrainer = ChatterBotCorpusTrainer(chatbot)# Train the chatbot based on the english corpustrainer.train("chatterbot.corpus.english")# Get a response to an input statementres = 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 ChatterBotCorpusTrainerchatbot = 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 chatbottrainer = ChatterBotCorpusTrainer(chatbot)# Train based on the english corpustrainer.train("chatterbot.corpus.english")# Train based on english greetings corpustrainer.train("chatterbot.corpus.english.greetings")# Train based on the english conversations corpustrainer.train("chatterbot.corpus.english.conversations")

3.3 轮询测试

from chatterbot import ChatBotfrom chatterbot.trainers import ChatterBotCorpusTrainerchatbot = ChatBot('Xiao Mu')# Create a new trainer for the chatbottrainer = ChatterBotCorpusTrainer(chatbot)# Train based on the english corpustrainer.train("chatterbot.corpus.english")# Train based on english greetings corpustrainer.train("chatterbot.corpus.english.greetings")# Train based on the english conversations corpustrainer.train("chatterbot.corpus.english.conversations")# Train the chatbot based on the english corpustrainer.train("chatterbot.corpus.chinese")lineCounter = 1# 开始对话while True:print(chatbot.get_response(input("(" + str(lineCounter) + ") user:")))lineCounter += 1

3.4 使用自定义集合

from chatterbot import ChatBotfrom chatterbot.trainers import ListTrainerchatbot = 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 ChatBotbot = 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 questionfor question in questions:response = bot.get_response(question)print(question + ' - Response: ' + response.text)

3.6 默认回答

default_response_example.py:

from chatterbot import ChatBotfrom chatterbot.trainers import ListTrainer# Create a new instance of a ChatBotbot = 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 responsestrainer.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: /en/latest/quickstart.html'])# Get a response for some unexpected inputresponse = 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 ChatBotbot = ChatBot('Math & Time Bot',logic_adapters=['chatterbot.logic.MathematicalEvaluation','chatterbot.logic.TimeLogicAdapter'])# Print an example of getting one math based responseresponse = 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 responseresponse = 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 loggingimport logginglogging.basicConfig(level=logging.INFO)# Create a new instance of a ChatBotbot = 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 botbot.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 ChatBotbot = 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: '}])# Get a response given the specific inputresponse = 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 ChatBotfrom chatterbot.conversation import Statementchatbot = ChatBot('Example Bot',# This database will be a temporary in-memory databasedatabase_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 ChatBotbot = 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 inputwhile 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 exitexcept (KeyboardInterrupt, EOFError, SystemExit):break

3.12 训练语料库示例

training_example_chatterbot_corpus.py:

from chatterbot import ChatBotfrom chatterbot.trainers import ChatterBotCorpusTrainerimport logging'''This is an example showing how to train a chat bot using theChatterBot 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 datatrainer = ChatterBotCorpusTrainer(chatbot)trainer.train(# 'chatterbot.corpus.english','chatterbot.corpus.chinese')# Now let's get a response to a greetingresponse = 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)

网友整理的语料库地址:

/codemayq/chinese_chatbot_corpus

/candlewill/Dialog_Corpus

3.13 训练列表数据示例

training_example_list_data.py:

from chatterbot import ChatBotfrom chatterbot.trainers import ListTrainer'''This is an example showing how to train a chat bot using theChatterBot ListTrainer.'''chatbot = ChatBot('XiaoMu Bot')# Start by training our bot with the ChatterBot corpus datatrainer = 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 variationstrainer.train(['Hello, how are you?','I am great.','That is awesome.','Thanks'])# Now let's get a response to a greetingresponse = 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 ChatBotfrom 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:breakif line == 'E\n':continuecorpus.append(line.split('M ')[1].strip('\n'))except:passfile.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 ChatBotimport tkinter as tktry:import ttk as ttkimport ScrolledTextexcept ImportError:import tkinter.ttk as ttkimport tkinter.scrolledtext as ScrolledTextimport timeclass 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, requestfrom chatterbot import ChatBotfrom chatterbot.trainers import ChatterBotCorpusTrainerapp = 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()

运行结果如下:

chatterbot+flask+jquery,版本v1.0

chatterbot+flask+chatui,版本v5.0

结语

如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;╮( ̄▽ ̄)╭

如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???

如果您需要相关功能的代码定制化开发,可以留言私信作者;(✿◡‿◡)

感谢各位大佬童鞋们的支持!( ´ ▽´ )ノ ( ´ ▽´)っ!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。