虽然循环不适合聊天机器人在语音对文本的语音

时间:2019-10-21 12:55:22

标签: python python-3.x pygame pyaudio

大家好,我有一个聊天机器人问题,该聊天机器人应该可以在文本到语音和语音到文本之间工作。拒绝等待输入形式的麦克风,然后使用来自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个变量用于将答案放入数据库。我认为将其放在此处不是必需的

1 个答案:

答案 0 :(得分:0)

您使用import错误。

第一次导入模块时,Python将执行该模块中的所有顶级内容,并记下所有函数,变量及其定义的其他内容。在随后的导入中,它只是从第一次就重复使用了这些东西。

如果要多次执行某个模块中的代码,则必须将该代码放在可以调用的函数中。

您需要执行以下操作:

  1. speak模块中的大部分代码包装到一个函数中(我们将其命名为get_speech(),并使其返回response

  2. 在主模块顶部,执行import speakfrom speak import get_speech

  3. 您现在要在哪里from speak import response,而不是response = speak.get_speech()(或者只是response = get_speech(),这取决于您在顶部导入的内容)。