机器学习构建chatbot

预备知识

机器人应答逻辑(Logic Adapter),针对每个部分设计了不同的适配器

  1. Closet Match Adapter:字符串模糊匹配(编辑距离)
  2. Closet Meaning Adapter:借助nltk的WordNet,近义词评估
  3. Time Logic Adapter:处理涉及实践的提问
  4. Mathematical Evaluation Adapter:涉及数学运算

存储器后段(Storage Adapter)

  1. Read Only Mode:只读模式
  2. Json Database Mode:Json格式存储对话数据
  3. Mongo Database Mode:以MongoDb方式存储对话数据库

输入形式(Input Adapter)

  1. Variable input type Adapter:允许chatbot接收不同类型的输入
  2. Terminal Adapter:以终端方式进行对话
  3. HipChat Adapter:通过HipChat聊天室和chatbo进行对话
  4. Speech recognition:语音识别输入

输出形式(Output Adapters)

  1. Output format adapter:支持text,json和object格式的输出
  2. Terminal adapter
  3. HipChat Adapter
  4. Mailgun adapter:允许chat bot基于Mailgun API进行邮件的发送 Speech synthesis
  5. TTS(Text to speech)部分,详见chatterbot-voice

使用ChatterBot构建聊天机器人

1. 首先构建ChatBot并指定Adapter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
bot = ChatBot(
'Default Response Example Bot',
#置信度低于0.65时回复default_response
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch'
},
{
'import_path': 'chatterbot.logic.LowConfidenceAdapter',
'threshold': 0.65,
'default_response': 'I am sorry, but I do not understand.'
}
],
#指定对话类型是列表
trainer='chatterbot.trainers.ListTrainer'
)
2. 接着手动给定一点语料用于训练
1
2
3
4
5
6
7
bot.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'
])
3. 给定问题并取回结果
1
2
3
4
5
6
7
8
9
10
question = 'How do I make an omelette?'
print(question)
response = bot.get_response(question)
print(response)

print("\n")
question = 'how to make a chat bot?'
print(question)
response = bot.get_response(question)
print(response)

各自输出:

1
2
3
4
5
6
How do I make an omelette?
I am sorry, but I do not understand.


how to make a chat bot?
Have you read the documentation?
4. 使用chatterbot自带的中文语料库训练
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/python
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot("ChineseChatBot")
chatbot.set_trainer(ChatterBotCorpusTrainer)

# 使用中文语料库训练它
chatbot.train("chatterbot.corpus.chinese")

# 开始对话
while True:
print(chatbot.get_response(input(">")))

试了一下,问答比较二。。。


  1. seq2seq
  2. LSTM

THE END.

评论