大家好,我的聊天机器人有问题,该聊天机器人可用于文本到语音和语音到文本的工作。拒绝等待输入形式的麦克风,然后使用来自SQLite数据库的答案列表进行回答。但是,机器人仅获得我的第一个输入的答案,然后它继续在无限循环中一次又一次地发送我的第一个输入。
Chatboy正在使用pyaudio和pygame
在聊天机器人的while循环开始时,我从语音到文本代码导入响应。除非必须重复执行,否则它将运行。似乎的语音转文字部分不会第二次运行,只会完全跳过第一次输入中已经填好的答案。
同时为True:
print(('Bot: ' + B)) from speak import response #importing response from speech-to-text code print(('Host: ' + response)) if response == 'ukončiť': print('Program bol úspešne ukončený') break if response == 'vypnúť': print('Program bol úspešne ukončený') break words = get_words(B) words_length = sum([n * len(word) for word, n in words]) sentence_id = get_id('sentence', response) for word, n in words: word_id = get_id('word', word) weight = sqrt(n / float(words_length)) cursor.execute('INSERT INTO associations VALUES (?, ?, ?)', (word_id, sentence_id, weight)) connection.commit() cursor.execute('CREATE TEMPORARY TABLE results(sentence_id INT, sentence TEXT, weight REAL)') words = get_words(response) words_length = sum([n * len(word) for word, n in words]) for word, n in words: weight = sqrt(n / float(words_length)) cursor.execute('INSERT INTO results SELECT associations.sentence_id, sentences.sentence, ?*associations.weight/(4+sentences.used) FROM words INNER JOIN associations ON associations.word_id=words.rowid INNER JOIN sentences ON sentences.rowid=associations.sentence_id WHERE words.word=?', (weight, word,)) cursor.execute('SELECT sentence_id, sentence, SUM(weight) AS sum_weight FROM results GROUP BY sentence_id ORDER BY sum_weight DESC LIMIT 1') row = cursor.fetchone() cursor.execute('DROP TABLE results') tts = gTTS(text=str(row[1]), lang='sk') tts.save("B.mp3") mixer.music.load('B.mp3') mixer.music.play() if row is None: cursor.execute('SELECT rowid, sentence FROM sentences WHERE used = (SELECT MIN(used) FROM sentences) ORDER BY RANDOM() LIMIT 1') row = cursor.fetchone() B = row[1] cursor.execute('UPDATE sentences SET used=used+1 WHERE rowid=?', (row[0],))
这是将语音转换为文本的代码
import speech_recognition as sr from gtts import gTTS import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) from pygame import mixer mixer.init() while True: r = sr.Recognizer() with sr.Microphone() as source: r.adjust_for_ambient_noise(source, duration=1) print("Tell something!") audio = r.listen(source,phrase_time_limit=2) try: response = r.recognize_google(audio, language='sk-SK') break except sr.UnknownValueError: print("Google nemôže rozoznať audio") except sr.RequestError as e: print("Google error; {0}".format(e))
在第一个代码之前,有2个变量用于将答案放入数据库。我认为将其放在此处不是必需的
大家好,我的聊天机器人有问题,该聊天机器人可用于文本到语音和语音到文本的工作。拒绝等待输入形式的麦克风,然后使用......>
您使用的是import
错误。
第一次导入模块时,Python将执行该模块中的所有顶级内容,并记录所有它定义的功能,变量和其他内容。在随后的导入中,它只是从第一时间开始重复使用这些东西。