Python 而在语音到文本的聊天机器人中,循环不能作为intendet工作

Python 而在语音到文本的聊天机器人中,循环不能作为intendet工作,python,python-3.x,pygame,pyaudio,Python,Python 3.x,Pygame,Pyaudio,大家好,我有一个聊天机器人的问题,它被支持使用文本到语音和语音到文本。支持等待输入表单,然后使用SQLite数据库中的答案列表进行回答。然而,bot只得到我的第一个输入,然后它继续在无限循环中一次又一次地发送我的第一个输入 聊天男孩正在使用pyaudio和pygame 在聊天机器人的Begining while循环中,我将响应从语音导入到文本代码。它运行,除非它必须重复自己。看起来,从语音到文本的部分并没有第二次运行,只是用第一次输入的已填充答案完全跳过了它 这是运行聊天机器人本身的代码, 尽管

大家好,我有一个聊天机器人的问题,它被支持使用文本到语音和语音到文本。支持等待输入表单,然后使用SQLite数据库中的答案列表进行回答。然而,bot只得到我的第一个输入,然后它继续在无限循环中一次又一次地发送我的第一个输入

聊天男孩正在使用pyaudio和pygame

在聊天机器人的Begining while循环中,我将响应从语音导入到文本代码。它运行,除非它必须重复自己。看起来,从语音到文本的部分并没有第二次运行,只是用第一次输入的已填充答案完全跳过了它

这是运行聊天机器人本身的代码, 尽管如此:

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],))
这是将语音转换为文本的代码
在第一个代码之前,有两个变量用于将答案放入数据库。我认为没有必要把它放在这里

您使用的是
导入
错误

第一次导入模块时,Python将执行该模块中的所有顶级内容,并记录所有函数、变量和它定义的其他内容。在随后的导入中,它只是重复使用第一次导入的内容

当您想多次执行模块中的代码时,必须将该代码放在可以调用的函数中

您需要这样做:

  • speak
    模块中的大部分代码封装在函数中(我们将其称为
    get\u speech()
    并使其返回
    response

  • 在主模块顶部,从speak import get_speech执行
    导入speak
    操作

  • 如果您现在正在从speak import response执行
    ,请改为执行
    response=speak.get_speech()
    (或者只执行
    response=get_speech()
    ,具体取决于您在顶部导入的内容)


  • 您使用的
    import
    错误

    第一次导入模块时,Python将执行该模块中的所有顶级内容,并记下它定义的所有函数、变量和其他内容。在后续导入时,它只会重新使用第一次导入的内容

    当您想多次执行模块中的代码时,必须将该代码放在可以调用的函数中

    您需要这样做:

  • speak
    模块中的大部分代码封装在函数中(我们将其称为
    get\u speech()
    并使其返回
    response

  • 在主模块顶部,从speak import get_speech执行
    导入speak
    操作

  • 如果您现在正在从speak import response执行
    ,请改为执行
    response=speak.get_speech()
    (或者只执行
    response=get_speech()
    ,具体取决于您在顶部导入的内容)

  • 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))