Skip to content

Commit db9f698

Browse files
author
yuanlei
committed
提交源码
0 parents  commit db9f698

20 files changed

+889
-0
lines changed

.idea/ChatRoom.iml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+74
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TrainChat.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
from chatterbot import ChatBot
4+
from chatterbot.trainers import ListTrainer
5+
from chatterbot.trainers import ChatterBotCorpusTrainer
6+
7+
8+
my_bot = ChatBot("Training demo",
9+
database="./db.sqlite3")
10+
11+
# 直接写语句训练
12+
my_bot.set_trainer(ListTrainer)
13+
14+
my_bot.train(["你叫什么名字?", "我叫小白兔!", ])
15+
my_bot.train([
16+
"Test1",
17+
"Test2",
18+
"Test3",
19+
"Test4",
20+
])
21+
22+
# 使用自定义语句训练它
23+
my_bot.set_trainer(ChatterBotCorpusTrainer)
24+
my_bot.train("chatterbot.corpus.mytrain")
25+
# while True:
26+
# print(my_bot.get_response(input("user:")))

__pycache__/chatbot.cpython-36.pyc

3.4 KB
Binary file not shown.

__pycache__/client.cpython-36.pyc

6.4 KB
Binary file not shown.

__pycache__/config.cpython-36.pyc

1.27 KB
Binary file not shown.

__pycache__/dealThread.cpython-36.pyc

740 Bytes
Binary file not shown.

__pycache__/recorder.cpython-36.pyc

1.72 KB
Binary file not shown.

chatbot.py

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import pygame
2+
from chatterbot import ChatBot
3+
import requests
4+
import json
5+
from config import *
6+
import time
7+
import os
8+
import random
9+
import urllib.request
10+
import base64
11+
12+
13+
# 初始化百度返回的音频文件地址,后面会变为全局变量,随需改变
14+
mp3_url = 'E:\Python_Doc\\voice_du\\voice_ss.mp3'
15+
16+
17+
# 播放Mp3文件
18+
def play_mp3():
19+
# 接受服务器的消息
20+
pygame.mixer.init()
21+
pygame.mixer.music.load(mp3_url)
22+
pygame.mixer.music.play()
23+
while pygame.mixer.music.get_busy():
24+
time.sleep(1)
25+
pygame.mixer.music.stop()
26+
pygame.mixer.quit()
27+
28+
29+
# 删除声音文件
30+
def remove_voice():
31+
path = r"E:\Python_Doc\voice_du"
32+
for i in os.listdir(path):
33+
path_file = os.path.join(path, i)
34+
try:
35+
os.remove(path_file)
36+
except:
37+
continue
38+
39+
40+
# 图灵自动回复
41+
def tuling(info):
42+
url = tuling_url + "?key=%s&info=%s" % (tuling_app_key, info)
43+
content = requests.get(url, headers=headers)
44+
answer = json.loads(content.text)
45+
return answer['text']
46+
47+
48+
# 聊天机器人回复
49+
def chatbot(info):
50+
my_bot = ChatBot("", read_only=True,
51+
database="./db.sqlite3")
52+
res = my_bot.get_response(info)
53+
return str(res)
54+
55+
56+
# 百度讲文本转为声音文件保存在本地 tts地址,无需token实时认证
57+
def baidu_api(answer):
58+
api_url = '{11}?idx={0}&tex={1}&cuid={2}&cod={3}&lan={4}&ctp={5}&pdt={6}&spd={7}&per={8}&vol={9}&pit={10}'\
59+
.format(baidu_api_set["idx"], answer, baidu_api_set["cuid"], baidu_api_set["cod"], baidu_api_set["lan"],
60+
baidu_api_set["ctp"], baidu_api_set["pdt"], baidu_api_set["spd"], baidu_api_set["per"],
61+
baidu_api_set["vol"], baidu_api_set["pit"], baidu_api_url)
62+
res = requests.get(api_url, headers=headers2)
63+
# 本地Mp3语音文件保存位置
64+
iname = random.randrange(1, 99999)
65+
global mp3_url
66+
mp3_url = 'E:\Python_Doc\\voices\\voice_tts' + str(iname) + '.mp3'
67+
with open(mp3_url, 'wb') as f:
68+
f.write(res.content)
69+
70+
71+
# 百度讲文本转为声音文件保存在本地 方法2 tsn地址
72+
def baidu_api2(answer):
73+
# 获取access_token
74+
token = getToken()
75+
get_url = baidu_api_url2 % (urllib.parse.quote(answer), "test", token)
76+
voice_data = urllib.request.urlopen(get_url).read()
77+
# 本地Mp3语音文件保存位置
78+
name = random.randrange(1, 99999)
79+
global mp3_url
80+
mp3_url = 'E:\Python_Doc\\voice_du\\voice_tsn' + str(name) + '.mp3'
81+
voice_fp = open(mp3_url, 'wb+')
82+
voice_fp.write(voice_data)
83+
voice_fp.close()
84+
return
85+
86+
87+
# 百度语音转文本
88+
def getText(filename):
89+
# 获取access_token
90+
token = getToken()
91+
data = {}
92+
data['format'] = 'wav'
93+
data['rate'] = 16000
94+
data['channel'] = 1
95+
data['cuid'] = str(random.randrange(123456, 999999))
96+
data['token'] = token
97+
wav_fp = open(filename, 'rb')
98+
voice_data = wav_fp.read()
99+
data['len'] = len(voice_data)
100+
data['speech'] = base64.b64encode(voice_data).decode('utf-8')
101+
post_data = json.dumps(data)
102+
# 语音识别的api url
103+
upvoice_url = 'http://vop.baidu.com/server_api'
104+
r_data = urllib.request.urlopen(upvoice_url, data=bytes(post_data, encoding="utf-8")).read()
105+
print(json.loads(r_data))
106+
err = json.loads(r_data)['err_no']
107+
if err == 0:
108+
return json.loads(r_data)['result'][0]
109+
else:
110+
return json.loads(r_data)['err_msg']
111+
112+
113+
# 获取百度API调用的认证,实时生成,因为有时间限制
114+
def getToken():
115+
# token认证的url
116+
api_url = "https://openapi.baidu.com/oauth/2.0/token?" \
117+
"grant_type=client_credentials&client_id=%s&client_secret=%s"
118+
token_url = api_url % (BaiDu_API_Key_GetVoi, BaiDu_Secret_Key_GetVoi)
119+
r_str = urllib.request.urlopen(token_url).read()
120+
token_data = json.loads(r_str)
121+
token_str = token_data['access_token']
122+
return token_str

0 commit comments

Comments
 (0)