-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxiaofang.py
53 lines (40 loc) · 1.15 KB
/
xiaofang.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from urllib import response
import speech_recognition as sr
import pyttsx3
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
def speech_recognition():
listener = sr.Recognizer()
try:
with sr.Microphone() as source:
print('我在听')
voice = listener.listen(source)
#语音识别
command = listener.recognize_google(voice, language="zh-CN")
print(command)
return command
except:
print('我不理解')
return('我不理解')
#语音合成
def text_to_speech(response):
engine =pyttsx3.init()
engine.say(str(response))
engine.runAndWait()
def chatter(command):
if ('小张' in command):
text_to_speech('没错,小张就是笨蛋')
return chatbot.get_response(command)
def xiaofang():
#语音识别
command = speech_recognition()
#聊天
response = chatter(command)
#语音合成
text_to_speech(response)
chatbot = ChatBot('xiaofang')
#train
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train("chatterbot.courpus.chinese.ai")
while True:
xiaofang()