0%

chatterbot-聊天机器人

读取excel中问题,配置默认返回值,数据存储为mongo
Apdater类型:
logic_adapters:机器人应答逻辑
Closest Match Adapter :字符串模糊匹配(编辑距离)
Closest Meaning Adapter  :借助nltk的WordNet,近义词评估
Time Logic Adapter :处理涉及时间的提问
Mathematical Evaluation Adapter: 涉及数学运算

storage_adapters:存储器后端
 Read Only Mode :只读模式,当有输入数据到chatterbot的时候,数据库并不会发生改变
 Json Database Adapter :用以存储对话数据的接口,对话数据以Json格式进行存储。
Mongo Database Adapter  以MongoDB database:方式来存储对话数据
input_adapters:输入形式
Variable input type adapter :允许chatter bot接收不同类型的输入的,如strings,dictionaries和Statements
Terminal adapter :使得ChatterBot可以通过终端进行对话
 HipChat Adapter :使得ChatterBot 可以从HipChat聊天室获取输入语句,通过HipChat 和 ChatterBot 进行对话
Speech recognition :语音识别输入,详见chatterbot-voice
output_adapters:输出形式
Output format adapter:支持text,json和object格式的输出
Terminal adapter
HipChat Adapter
Mailgun adapter:允许chat bot基于Mailgun API进行邮件的发送
Speech synthesisTTS(Text to speech)部分,详见chatterbot-voice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
# -*- coding: utf-8 -*-

#手动设置一些语料
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import xlrd
#读取excel问题 到数组
wb = xlrd.open_workbook("excel.xlsx")
sh = wb.sheet_by_index(0)
questionList = []
for r in range(sh.nrows):
questions = sh.cell_value(r, 0).split("##")
answer = sh.cell_value(r, 1).split("#")[0]
for question in questions:
if question:
questionList.append(question)
questionList.append(answer)
#配置机器人名称
#adapter
#storage adapters:提供连接不同存储系统( MongoDB 或者本地文件存储)的接口。
#output adapters :提供应答接口支持text,json和object格式的输出
#logic adapters:定义输入<—>应答的逻辑
###MathematicalEvaluation 该adapter对于输入的语句进行数学表达式是否存在的检测,若有则返回应答结果。该adapter能够处理单词和数字运算符的任意组合
###Closest Meaning Adapter  借助nltk的WordNet,近义词评估
###BestMatch 就是最匹配方式,从训练的对话中找到最相识的语句,根据对话,提供回答
Chinese_bot = ChatBot("Excel",
storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch',
'threshold': 0.6,
'default_response': '正在学习中'
}
],
database_uri='mongodb://localhost:27017/chatterbot-database',
output_adapter="chatterbot.adapters.output.OutputFormatAdapter",
output_format='text')
trainer = ListTrainer(Chinese_bot)
#在trainer中,决定选择哪种构造方式来创建上下文的关系

trainer.train(questionList)

# 测试一下
while True:
question = input("请提问:")
if question=='quit':
break
response = Chinese_bot.get_response(question)
print(response)

@http://www.shareditor.com/blogshow?blogId=64
开发文档:
@https://chatterbot.readthedocs.io/en/stable/index.html
github:
@https://github.com/gunthercox/ChatterBot