sudo pip install chatterbot

# -*- coding: utf-8 -*-
from chatterbot import ChatBot
# 构建ChatBot并指定
'Default Response Example Bot',
{
'import_path': 'chatterbot.logic.BestMatch'
},
{
'threshold': 0.65,
'default_response': 'I am sorry, but I do not understand.'
}
],
trainer='chatterbot.trainers.ListTrainer')
# 手动给定一点语料用于训练
bot.train([
'I want to create a chat bot',
'No, I have not',
'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html'])
# 给定问题并取回结果
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)

How do I make an omelette?
I am sorry, but I do not understand.

how to make a chat bot?

# -*- coding: utf-8 -*-
from chatterbot import ChatBot
bot = ChatBot(
"Math & Time Bot",
"chatterbot.logic.MathematicalEvaluation",
# 进行数学计算
question = "What is 4 + 9?"
print(question)response = bot.get_response(question)
print(response)
print("\n")
# 回答和时间相关的问题
question = "What time is it?"
print(question)
response = bot.get_response(question)
print(response)

What is 4 + 9?
( 4 + 9 ) = 13

What time is it?
The current time is 05:08 PM

# -*- coding: utf-8 -*-
from chatterbot import ChatBot
'''如果一个已经训练好的chatbot，你想取出它的语料，用于别的chatbot构建，可以这么做'''
chatbot = ChatBot(
'Export Example Bot',
trainer='chatterbot.trainers.ChatterBotCorpusTrainer')
# 训练一下咯
chatbot.train('chatterbot.corpus.english')
# 把语料导出到json文件中chatbot.trainer.export_for_training('./my_export.json')

# -*- coding: utf-8 -*-
from chatterbot import ChatBot
import logging
"""反馈式的聊天机器人，会根据你的反馈进行学习"""
# 把下面这行前的注释去掉，可以把一些信息写入日志中
# logging.basicConfig(level=logging.INFO)
# 创建一个聊天机器人
bot = ChatBot(
'Feedback Learning Bot',
'chatterbot.logic.BestMatch'
],
DEFAULT_SESSION_ID = bot.default_session.id
def get_feedback():
from chatterbot.utils import input_function
text = input_function()
if 'Yes' in text:
return True
elif 'No' in text:
return False
else:
print('Please type either "Yes" or "No"')
return get_feedback()
print('Type something to begin...')
# 每次用户有输入内容，这个循环就会开始执行
while True:
try:
input_statement = bot.input.process_input_statement()
statement, response = bot.generate_response(input_statement, DEFAULT_SESSION_ID)
print('\n Is "{}" this a coherent response to "{}"? \n'.format(response, input_statement))
if get_feedback():
bot.learn_response(response,input_statement)
bot.output.process_response(response)
# 更新chatbot的历史聊天数据
bot.conversation_sessions.update(
bot.default_session.id_string,
(statement, response, )
)
# 直到按ctrl-c 或者 ctrl-d 才会退出
except (KeyboardInterrupt, EOFError, SystemExit):
break

from chatterbot import ChatBot
import logging
'''这是一个使用Ubuntu语料构建聊天机器人的例子'''
# 允许打日志logging.basicConfig(level=logging.INFO)
chatbot = ChatBot(
'Example Bot',   trainer='chatterbot.trainers.UbuntuCorpusTrainer')
# 使用Ubuntu数据集开始训练
chatbot.train()
# 我们来看看训练后的机器人的应答
response = chatbot.get_response('How are you doing today?')
print(response)

# -*- coding: utf-8 -*-
from chatterbot import ChatBot
from settings import Microsoft
chatbot = ChatBot(
'MicrosoftBot',
directline_host = Microsoft['directline_host'],
direct_line_token_or_secret = Microsoft['direct_line_token_or_secret'],
conversation_id = Microsoft['conversation_id'],
trainer='chatterbot.trainers.ChatterBotCorpusTrainer')chatbot.train('chatterbot.corpus.english')
# 是的，会一直聊下去
while True:
try:
response = chatbot.get_response(None)

# 直到按ctrl-c 或者 ctrl-d 才会退出
except (KeyboardInterrupt, EOFError, SystemExit):
break

0 条评论

相关文章

15120

手把手带你用机器学习写unity AI

2017unity机器学习社区挑战赛参加地址：https://connect.unity.com/challenges/ml-agents-1 打开下载的uni...

26770

35400

CUDA Study Notes

SSE（Streaming SIMD Extensions，单指令多数据流扩展）指令集是Intel在Pentium III处理器中率先推出的。其中包含70条指令...

14620

85750

26210

Tyvj P1813 [JSOI2008]海战训练

P1813 [JSOI2008]海战训练 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 为了准备高层峰会，元首命令武...

397120

24520

8K110

【干货】TensorFlow协同过滤推荐实战

【导读】本文利用TensorFlow构建了一个用于产品推荐的WALS协同过滤模型。作者从抓取数据开始对模型进行了详细的解读，并且分析了几种推荐中可能隐藏的情况及...

914110