Skip to content

Instantly share code, notes, and snippets.

@yangl
Last active March 6, 2018 07:23
Show Gist options
  • Save yangl/d94fac9acd70a68f4dc779563f1074c9 to your computer and use it in GitHub Desktop.
Save yangl/d94fac9acd70a68f4dc779563f1074c9 to your computer and use it in GitHub Desktop.
chatterbot使用java调用例子 https://github.com/gunthercox/ChatterBot MySQL支持请确认chatterbot/ext/sqlalchemy_app/models.py中的UnicodeString改为UnicodeString(512) https://github.com/gunthercox/ChatterBot/pull/1200
# -*- coding: utf-8 -*-
import sys
from chatterbot import ChatBot
from chatterbot.comparisons import sentiment_comparison
from chatterbot.trainers import ChatterBotCorpusTrainer
bot = ChatBot(
"sf",
storage_adapter="chatterbot.storage.SQLStorageAdapter",
database_uri="mysql+pymysql://root:root@localhost/test?charset=utf8mb4",
# database="D:/workspace/chatterbot-api/src/main/resources/py/sf.sqlite3",
read_only=True,
# statement_comparison_function=sentiment_comparison,
logic_adapters=[
{
'import_path': 'chatterbot.logic.SpecificResponseAdapter',
'input_text': 'sfpp',
'output_text': '您好,请访问: http://sfpp.sf-express.com'
},
{
'import_path': 'chatterbot.logic.LowConfidenceAdapter',
'threshold': 0.25,
'default_response': '您好,我现在还不知道怎么回答您的问题!请访问SFPP--搜索您的问题--也许能得到答案,3Q。'
},
{
'import_path': 'chatterbot.logic.BestMatch'
}
]
)
bot.set_trainer(ChatterBotCorpusTrainer)
bot.train([
"chatterbot.corpus.chinese"
])
def get_bot(arg):
response = bot.get_response(arg, conversation_id=10)
print(response)
if __name__ == '__main__':
get_bot(sys.argv[1])
package com.xxxx.chatterbot.chatterbotapi.service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteStreamHandler;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* @author YANGLiiN 2018-02-01 16:46
*/
@Slf4j
@Service
public class ChatterbotService implements InitializingBean {
@Value("${python.command.path:/home/sfapp/}")
private String pythonCommandPath;
private static final String CHARSET = SystemUtils.IS_OS_WINDOWS ? "GBK" : "UTF-8";
public String bot(String question) {
String resp = execCommand(
"python " + pythonCommandPath + "chatterbot.py " + question);
return resp;
}
@Override
public void afterPropertiesSet() throws Exception {
execCommand("python " + pythonCommandPath + "train.py");
}
private final String execCommand(String commandLine) {
log.info("要执行的命令是:{}", commandLine);
String resp = null;
Executor executor = new DefaultExecutor();
//接收正常结果流
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//接收异常结果流
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
ExecuteStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
executor.setStreamHandler(streamHandler);
// 设置脚本执行超时时间
ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000);
executor.setWatchdog(watchdog);
CommandLine cl = CommandLine.parse(commandLine);
try {
executor.execute(cl);
resp = outputStream.toString(CHARSET);
} catch (IOException e) {
e.printStackTrace();
}
return resp;
}
}
from datetime import datetime
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
# 初始化聊天机器人
momo = ChatBot(
'Momo',
storage_adapter="chatterbot.storage.SQLStorageAdapter",
database_uri="mysql+pymysql://root:root@localhost/test?charset=utf8mb4",
read_only=False
)
# 读取.conv 数据文件,因为我服务器配置较低,所以选择了一个内容较少的文件
# 这个函数是一个生成器
def read_conv(filename='D:/opensource/dgk_lost_conv/results/prisonb.conv'):
with open(filename, mode='rt', encoding='utf-8') as f:
conv = []
# 逐行读取
for line in f:
_line = line.replace('\n', '').replace('/', '').strip() # 预处理字符串 去掉首位空格
if _line.startswith('E'): # 如果是分隔符 表示对话结束 返回对话列表
yield conv
conv = [] # 重置对话列表
elif _line.startswith('M'): # 不是分隔符则将内容加入对话列表
# c = _line.split()[-1] # 其实这里如果对话中包含空格 对话数据会不完整,应该只去掉M和开头的空格
c = _line.replace('M ',
'').strip()
conv.append(c)
def train_momo():
for conv in read_conv():
print(conv)
momo.set_trainer(ListTrainer) # 指定训练方式
momo.train(conv) # 训练数据
def test_momo(question=None):
print(momo.get_response(question, 7))
if __name__ == '__main__':
# train_momo()
print(datetime.now())
test_momo('到底发生什么了')
print(datetime.now())
import logging
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from flask import Flask, request, jsonify
logging.basicConfig(level=logging.INFO, filename='mybot.log',
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
tbot = ChatBot(
"sfbot",
storage_adapter="chatterbot.storage.SQLStorageAdapter",
database_uri="mysql+pymysql://appmgr:[email protected]:3306/bot?charset=utf8mb4",
logic_adapters=["chatterbot.logic.BestMatch"],
read_only=False
)
cbot = ChatBot(
"sfbot",
storage_adapter="chatterbot.storage.SQLStorageAdapter",
database_uri="mysql+pymysql://appmgr:[email protected]:3306/bot?charset=utf8mb4",
read_only=True,
# statement_comparison_function=sentiment_comparison,
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch'
},
{
'import_path': 'chatterbot.logic.SpecificResponseAdapter',
'input_text': 'sfpp',
'output_text': '您好,请访问: http://sfpp.sf-express.com'
},
{
'import_path': 'chatterbot.logic.LowConfidenceAdapter',
'threshold': 0.25,
'default_response': '您好,我现在还不知道怎么回答您的问题!请访问SFPP--搜索您的问题--也许能得到答案,3Q。'
}
]
)
error_format_msg = '训练数据为空或格式不正确,请按照"Q: 问题A: 答案"这种格式,注意Q: A: 后边各一个英文空格'
@app.route('/chat', methods=['GET', 'POST'])
def chat_bot():
question = request.args.get('question', '')
user_id = request.args.get('user_id', '1')
try:
resp = cbot.get_response(question, user_id)
return jsonify(code=1, data=resp.text)
except Exception:
logging.exception('获取回复报错了,请求参数question[%s] user_id[%s]', question, user_id)
return jsonify(code=-1, data='error')
@app.route('/train', methods=['POST'])
def train_bot():
lines = []
ss = request.form.get('data')
if ss:
data = ss.split('Q: ')
if data:
for d in data:
idx_a = d.find('A: ')
if idx_a > -1:
q = d[:idx_a]
a = d[idx_a + 2:]
lines.append(q.strip())
lines.append(a.strip())
else:
return jsonify(code=2, data=error_format_msg)
else:
return jsonify(code=2, data=error_format_msg)
try:
if lines:
tbot.set_trainer(ListTrainer)
tbot.train(lines)
return jsonify(code=1, data='success')
except Exception:
logging.exception('训练数据时报错了')
return jsonify(code=-1, data='error')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
bot = ChatBot(
"sf",
storage_adapter="chatterbot.storage.SQLStorageAdapter",
database_uri="mysql+pymysql://root:root@localhost/test?charset=utf8mb4"
# database="D:/workspace/chatterbot-api/src/main/resources/py/sf.sqlite3"
)
bot.set_trainer(ChatterBotCorpusTrainer)
bot.train([
"chatterbot.corpus.chinese"
])
if __name__ == '__main__':
print(bot.get_response('disconf'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment