# 唤醒
def wakeUp(lst_wakeupKeyword,lst_reply,lst_endKeyword,p):
while True:
# 录音
autoRecord(setvoice)
# 格式转换
wav2pcm()
# 语音识别
newMessage=speech2writting(speech2writtingToken)
p.put('吴彦祖:'+newMessage)
# # 识别是否结束词
endKeyword=False
for eachEndKeyword in lst_endKeyword:
lst_word=[i for i in eachEndKeyword.split('|') if i != '']
count_word=0
for eachWord in lst_word:
if eachWord in newMessage:
count_word=count_word+1
if count_word == len(lst_word):
endKeyword=True
if endKeyword != False:
return 'bye'
# # 识别是否唤醒词
wakeupKeyword=False
for eachWakeupKeyword in lst_wakeupKeyword:
lst_word=[i for i in eachWakeupKeyword.split('|') if i != '']
count_word=0
for eachWord in lst_word:
if eachWord in newMessage:
count_word=count_word+1
if count_word == len(lst_word):
wakeupKeyword=True
if wakeupKeyword != False:
# 随机选择回应内容
repMessage=random.choice(lst_reply)
p.put('女朋友:'+repMessage)
# 语音合成
writting2speech(repMessage,PER,writting2speechToken)
# 播放生成的音频
playRecord()
return 'wakeup'
# 交流
def exchange(select_workbook,lst_endKeyword,p):
# 加载数据位置
wb_exchange=openpyxl.load_workbook(select_workbook+'.xlsx')
ws_sure=wb_exchange['确定']
ws_prologue=wb_exchange['引导作用语句']
#播放开场白
lst_prologue=[]
for eachPrologue in ws_prologue['A']:
if str(eachPrologue.value) != '开场白':
lst_prologue.append(str(eachPrologue.value))
# 随机选择回应内容
repMessage=random.choice(lst_prologue)
p.put('女朋友:'+repMessage)
# 语音合成
writting2speech(repMessage,PER,writting2speechToken)
# 播放生成的音频
playRecord()
#设定循环的基本参数
classEqualIntention=False
while True:
# 录音
autoRecord(setvoice)
# 格式转换
wav2pcm()
# 语音识别
newMessage=speech2writting(speech2writtingToken)
p.put('吴彦祖:'+newMessage)
# # 识别是否结束词
endKeyword=False
for eachEndKeyword in lst_endKeyword:
lst_word=[i for i in eachEndKeyword.split('|') if i != '']
count_word=0
for eachWord in lst_word:
if eachWord in newMessage:
count_word=count_word+1
if count_word == len(lst_word):
endKeyword=True
if endKeyword != False:
return 'bye'
# 第一步,在'确定'工作表查找符合的特征
intention=False
for eachFeature in ws_sure['D']:
if str(eachFeature.value) != '特征':
lst_featureKeyword=[i for i in str(eachFeature.value).split('|') if i != '']
for eachFeatureKeyword in lst_featureKeyword:
if eachFeatureKeyword in newMessage or str(ws_sure.cell(eachFeature.row,1).value) in newMessage:
# # 符合特征,查找符合的关键词
row_feature=eachFeature.row
for eachKeyword in ws_sure[str(row_feature)]:
if eachKeyword.column >= 5:
lst_word=[i for i in str(eachKeyword.value).split('|') if i != '']
count_word=0
for eachWord in lst_word:
if eachWord in newMessage:
count_word=count_word+1
# # # 符合关键词,返回意图
if count_word == len(lst_word):
# 随机选择回应内容
repMessage=random.choice([i for i in str(ws_sure.cell(row_feature,3).value).split('|') if i != ''])
p.put('女朋友:'+repMessage)
# 语音合成
writting2speech(repMessage,PER,writting2speechToken)
# 播放生成的音频
playRecord()
# 返回意图
intention=str(ws_sure.cell(row_feature,1).value)
return intention
# 第二步,可能符合特征,但一定不符合关键词的
if intention == False:
# # 获取'引导'工作表'分类'(key)和'意图'(value)的信息,存储成字典
dct_class={}
for eachClass in ws_sure['B']:
if str(eachClass.value) !='分类':
if str(eachClass.value) in dct_class.keys():
dct_class[str(eachClass.value)].append(str(ws_sure.cell(eachClass.row,1).value))
else:
dct_class[str(eachClass.value)]=[]
dct_class[str(eachClass.value)].append(str(ws_sure.cell(eachClass.row,1).value))
# # # 引导到分类语句(相当于第二步的开场白)
lst_leadClass=[]
for eachLeadClass in ws_prologue['B']:
if str(eachLeadClass.value) != '引导到类语句' and str(eachLeadClass.value) != 'None':
lst_leadClass.append(str(eachLeadClass.value))
# 随机选择回应引导到类语句
str_class=''
for eachClass in dct_class.keys():
str_class=str_class+eachClass+','
repMessage=random.choice(lst_leadClass)
repMessage=re.sub('\\{format\\}',str_class[:-1],repMessage)
p.put('女朋友:'+repMessage)
#判断'分类'是否包含在'意图''
for eachClass in dct_class.keys():
if eachClass in dct_class[eachClass]:
classEqualIntention=True
else:
classEqualIntention=False
break
# 语音合成
writting2speech(repMessage,PER,writting2speechToken)
# 播放生成的音频
playRecord()
if classEqualIntention==False:
# 录音
autoRecord(setvoice)
# 格式转换
wav2pcm()
# 语音识别
newMessage=speech2writting(speech2writtingToken)
p.put('吴彦祖:'+newMessage)
# # 识别是否结束词
endKeyword=False
for eachEndKeyword in lst_endKeyword:
lst_word=[i for i in eachEndKeyword.split('|') if i != '']
count_word=0
for eachWord in lst_word:
if eachWord in newMessage:
count_word=count_word+1
if count_word == len(lst_word):
endKeyword=True
if endKeyword != False:
return 'bye'
# 对比分类
for eachClass in dct_class.keys():
if eachClass in newMessage:
# # # 引导意图语句
lst_intention=[]
for eachLeadIntention in ws_prologue['C']:
if str(eachLeadIntention.value) != '引导到意图语句' and str(eachLeadIntention.value) != 'None':
lst_intention.append(str(eachLeadIntention.value))
str_intention=''
for eachIntetion in dct_class[eachClass]:
str_intention=str_intention+eachIntetion+','
repMessage=random.choice(lst_intention)
repMessage=re.sub('\\{format\\}',str_intention[:-1],repMessage)
p.put('女朋友:'+repMessage)
# 语音合成
writting2speech(repMessage,PER,writting2speechToken)
# 播放生成的音频
playRecord()
break
D:\Software\Python\python.exe D:/Py_Code/百度对话/闲聊机器人_语音聊天.py
2022-08-15 09:22:36 开始录音
2022-08-15 09:22:41 结束录音
2022-08-15 09:22:41 开始转换格式
'ffmpeg' �����ڲ����ⲿ���Ҳ���ǿ����еij���
���������ļ���
2022-08-15 09:22:41 结束转换格式
2022-08-15 09:22:41 开始语音识别
2022-08-15 09:22:42 结束语音识别
2022-08-15 09:22:42 识别结果: 女朋友在不在?那么回应内容。
2022-08-15 09:22:42 开始语音合成
Process Process-1:
Traceback (most recent call last):
File "D:\Software\Python\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "D:\Software\Python\lib\multiprocessing\process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "D:\Py_Code\百度对话\闲聊机器人_语音聊天.py", line 545, in start_exchange
intention=wakeUp(lst_wakeupKeyword,lst_reply,lst_endKeyword,p)
File "D:\Py_Code\百度对话\闲聊机器人_语音聊天.py", line 340, in wakeUp
playRecord()
File "D:\Py_Code\百度对话\闲聊机器人_语音聊天.py", line 101, in playRecord
wf = wave.open('reponse.wav', 'rb') # 读取音频文件数据
File "D:\Software\Python\lib\wave.py", line 510, in open
return Wave_read(f)
File "D:\Software\Python\lib\wave.py", line 164, in __init__
self.initfp(f)
File "D:\Software\Python\lib\wave.py", line 131, in initfp
raise Error('file does not start with RIFF id')
wave.Error: file does not start with RIFF id
2022-08-15 09:22:42 结束语音合成