-
大小: 54KB文件类型: .zip金币: 2下载: 0 次发布日期: 2021-05-14
- 语言: Python
- 标签: 聊天 机器人 tensorflow
资源简介
基于Python,使用TensorFlow实现的Sequence to Sequence的聊天机器人模型

代码片段和文件信息
#!/usr/bin/env python3
__author__ = ‘qhduan@memect.co‘
import os
import sys
import json
import math
import shutil
import pickle
import sqlite3
from collections import OrderedDict Counter
import numpy as np
from tqdm import tqdm
def with_path(p):
current_dir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(current_dir p)
DICTIONARY_PATH = ‘db/dictionary.json‘
EOS = ‘‘
UNK = ‘‘
PAD = ‘‘
GO = ‘‘
# 我一般是逗号放到句子后面的……
# 不过这样比较方便屏蔽某一行,如果是JS就不用这样了,因为JS的JSON语法比较松,允许多余逗号
buckets = [
(5 15)
(10 20)
(15 25)
(20 30)
]
def time(s):
ret = ‘‘
if s >= 60 * 60:
h = math.floor(s / (60 * 60))
ret += ‘{}h‘.format(h)
s -= h * 60 * 60
if s >= 60:
m = math.floor(s / 60)
ret += ‘{}m‘.format(m)
s -= m * 60
if s >= 1:
s = math.floor(s)
ret += ‘{}s‘.format(s)
return ret
def load_dictionary():
with open(with_path(DICTIONARY_PATH) ‘r‘) as fp:
dictionary = [EOS UNK PAD GO] + json.load(fp)
index_word = OrderedDict()
word_index = OrderedDict()
for index word in enumerate(dictionary):
index_word[index] = word
word_index[word] = index
dim = len(dictionary)
return dim dictionary index_word word_index
“““
def save_model(sess name=‘model.ckpt‘):
import tensorflow as tf
if not os.path.exists(‘model‘):
os.makedirs(‘model‘)
saver = tf.train.Saver()
saver.save(sess with_path(‘model/‘ + name))
def load_model(sess name=‘model.ckpt‘):
import tensorflow as tf
saver = tf.train.Saver()
saver.restore(sess with_path(‘model/‘ + name))
“““
dim dictionary index_word word_index = load_dictionary()
print(‘dim: ‘ dim)
EOS_ID = word_index[EOS]
UNK_ID = word_index[UNK]
PAD_ID = word_index[PAD]
GO_ID = word_index[GO]
class BucketData(object):
def __init__(self buckets_dir encoder_size decoder_size):
self.encoder_size = encoder_size
self.decoder_size = decoder_size
self.name = ‘bucket_%d_%d.db‘ % (encoder_size decoder_size)
self.path = os.path.join(buckets_dir self.name)
self.conn = sqlite3.connect(self.path)
self.cur = self.conn.cursor()
sql = ‘‘‘SELECT MAX(ROWID) FROM conversation;‘‘‘
self.size = self.cur.execute(sql).fetchall()[0][0]
def all_answers(self ask):
“““找出所有数据库中符合ask的answer
“““
sql = ‘‘‘
SELECT answer FROM conversation
WHERE ask = ‘{}‘;
‘‘‘.format(ask.replace(“‘“ “‘‘“))
ret = []
for s in self.cur.execute(sql):
ret.append(s[0])
return list(set(ret))
def random(self):
while True:
# 选择一个[1 MAX(ROWID)]中的整数,读取这一行
rowid = np.random.randint(1 self.size + 1)
sql = ‘‘‘
SELECT ask answer FROM conversation
WHERE ROWID = {};
‘‘‘.format(rowid)
ret = self.cur.e
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\
文件 123 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\.gitignore
文件 4964 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\README.md
文件 339 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\config.json
文件 8892 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\data_utils.py
目录 0 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\db\
文件 10054 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\db\chinese.txt
文件 75314 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\db\dictionary.json
文件 11266 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\db\gb2312_level1.txt
文件 9025 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\db\gb2312_level2.txt
文件 5093 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\db\generate_dict.ipynb
文件 2610 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\decode_conv.py
文件 9331 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\s2s.py
文件 10421 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\s2s_model.py
文件 417 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\train_model.sh
文件 419 2016-11-30 01:19 Seq2Seq_Chatbot_QA-master\train_model2.sh
- 上一篇:Python基础项目配套代码
- 下一篇:python视频资料.zip
相关资源
- DeepLabV3-Tensorflow-master
- python 微信机器人
- 基于TensorFlow实现CNN文本分类实验指导
- 微信聊天机器人(基于wxpy)
- tensorflow2.0 yolo3目标检测算法
- tensorflow制作自己的灰度图像数据集并
- anaconda下安装tensorflow(注:不同版本
- 北京大学曹健老师-人工智能实践:
- Deep Learning With Python - Jason Brownlee
- Python-自然场景文本检测PSENet的一个
- Python-高效准确的EAST文本检测器的一个
- Python-TensorFlow弱监督图像分割
- Python-基于tensorflow实现的用textcnn方法
- Python-subpixel利用Tensorflow的一个子像素
- 【官方文档】TensorFlow Python API docume
- Python微信聊天机器人
-
tensorflow画风迁移代码 st
yle transfer - 基于Python的微信自动回复机器人
- 简单粗暴 TensorFlow
- Python-即时通讯Python实现web版多人聊天
- [PDF] Reinforcement Learning With Open AI Tens
- tensorflow目标检测代码
- 基于Python的手写字体识别系统
- 基于Tensorflow的人脸识别源码
- python TensorFlow 官方文档中文版
- Python-在TensorFlow中实现实现图像卷积网
- tensorflow-1.9.0-cp37-cp37m-win_amd64.whl
- Faster-RCNN-TensorFlow-Python3.5-master
- python aiml web 聊天机器人
- NAO机器人学习笔记
评论
共有 条评论